在读取CSV转换的单元格数据时,Apache POI的Java空指针异常

时间:2015-05-24 05:01:22

标签: java excel apache apache-poi xls

我试图为这个应用程序做两件事:一个是在调用类时,它将从上到下运行两个,我可以调用公共方法" xlxTOcsvConverter"并提供三个需要的参数以备将来使用...即,如果运行该类,将输入4个文件,并将写入4个文件。前三个工作得很漂亮,但最后一个,带注释的那个,似乎因给定错误而失败。现在我遇到的问题是,数据是由外部公司维护的,我们只有读权限,所以我所能做的就是要求他们维护一组单元格格式,但这可能不会成功。我注意到失败的单元格,它被格式化为日期,但是为空。所有其他空单元格都是通用格式;它实际上是它周围唯一的格式。我猜这就是它失败的原因。如果是cell_types,我会错过其他一些吗?

/*
* To change this license header, choose License Headers in Project     Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package datarefresh;

import java.io.FileInputStream;
import java.io.FileWriter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/**
*
* @author null
*/
public class DataRefresh {

public static String EOL = System.getProperty("line.separator"); //Finding out OS specific EOL and setting it to a string named EOL

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
    String StrInfoXLS = "\\\\path\\Dashboard.xls";
    String POSPwXLS = "\\\\path\\POS Passwords.xls";
    String NetPwXLS = "\\\\path\\Dashboard Data.xls";
    String SnLPwXLS = "\\\\path\\AccessVia ID & Passcodes - Helpdesk.xls";
    String StrInfoCSV = "\\\\path\\StoreInfoTst.csv";
    String POSPwCSV = "\\\\path\\POS PasswordsTst.csv";
    String NetPwCSV = "\\\\path\\Dashboard DataTst.csv";
    String SnLPwCSV = "\\\\path\\AccessVia ID & Passcodes - HelpdeskTst.csv";       
    String StrInfoSht = "Store List";
    String POSPwSht = "Sheet1";
    String NetPwSht = "Network";
    String SnLPwSht = "S & L Store List w Passcode";
    xlxTOcsvConverter(StrInfoXLS,StrInfoSht,StrInfoCSV);
    xlxTOcsvConverter(POSPwXLS,POSPwSht,POSPwCSV);
    xlxTOcsvConverter(NetPwXLS,NetPwSht,NetPwCSV);
    xlxTOcsvConverter(SnLPwXLS,SnLPwSht,SnLPwCSV);   //THIS ONE IS NOT WORKING
}

public static void xlxTOcsvConverter(String inFile, String inSheet, String outFile) {
    DataRefresh GetIt = new DataRefresh();
    String data = "";
    try{
        FileWriter output = new FileWriter(outFile);
        Workbook wb = WorkbookFactory.create(new FileInputStream(inFile));
        Sheet sheet = wb.getSheet(inSheet);
        Row row = null;
        for(int i = 0; i<sheet.getLastRowNum(); i++){
            row = sheet.getRow(i);
            for(int j = 0; j<row.getLastCellNum(); j++){
                Cell cell = row.getCell(j);
                data = GetIt.getCellValue(cell);
                output.write(data + ";");
            }
            output.write(EOL);
        }
        output.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}

private String getCellValue(Cell cell){
    String data = "";
    if(cell == null){
        return "";
    }
    if(cell.getCellType() == Cell.CELL_TYPE_BLANK){
        return "";
    }
    if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
        switch (cell.getCachedFormulaResultType()){
            case Cell.CELL_TYPE_NUMERIC:
                if(DateUtil.isCellDateFormatted(cell)){
                    data = String.valueOf(cell.getDateCellValue());
                    return data;
                }else{
                    double temp = cell.getNumericCellValue();
                    data = String.format("%.0f", temp);
                    return data;
                }
            case Cell.CELL_TYPE_STRING:
                data = cell.getStringCellValue();
                return data;
        }
    }
    if(cell.getCellType() == Cell.CELL_TYPE_STRING){
        data = cell.getStringCellValue();
        return data;
    }
    if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
        if(DateUtil.isCellDateFormatted(cell)){
            data = String.valueOf(cell.getDateCellValue());
            return data;
        }else{
            double temp = cell.getNumericCellValue();
            data = String.format("%.0f", temp);
            return data;
        }
    }
    return "";
}

}

这是它的堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:214)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:186)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:173)
    at jdk.nashorn.internal.objects.Global.checkObject(Global.java:1500)
    at jdk.nashorn.internal.objects.NativeError.printStackTrace(NativeError.java:187)
    at datarefresh.DataRefresh.xlxTOcsvConverter(DataRefresh.java:68)
    at datarefresh.DataRefresh.main(DataRefresh.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)

我将捕获更改为:

    catch(IOException | InvalidFormatException e){
        printStackTrace(e);
    }

现在堆栈跟踪返回:

Exception in thread "main" java.lang.NullPointerException
    at datarefresh.DataRefresh.xlxTOcsvConverter(DataRefresh.java:60)
    at datarefresh.DataRefresh.main(DataRefresh.java:47)
Java Result: 1

1 个答案:

答案 0 :(得分:1)

如果第0行和第sheet.getLastRowNum()行之间存在完全空行,则row之后row = sheet.getRow(i)将为NULL。这是因为完全空行不会物理存储在XLS文件中。

你应该考虑这个。例如:

public static void xlxTOcsvConverter(String inFile, String inSheet, String outFile) {
    DataRefresh GetIt = new DataRefresh();
    String data = "";
    try {
        FileWriter output = new FileWriter(outFile);
        Workbook wb = WorkbookFactory.create(new FileInputStream(inFile));
        Sheet sheet = wb.getSheet(inSheet);

        Row row = null;
        for(int i = 0; i<=sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            if (row == null) {
                data = "empty row";
                output.write(data);
            } else {
                for(int j = 0; j<row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    data = GetIt.getCellValue(cell);
                    output.write(data + ((j<row.getLastCellNum()-1) ? ";" : ""));
                }
            }
            output.write(EOL);
        }


        output.close();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}