将信息附加到现有Excel文件时出现java.lang.NullPointerException [英] java.lang.NullPointerException when appending information to an existing Excel file

查看:144
本文介绍了将信息附加到现有Excel文件时出现java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将信息追加到现有的Excel文件中,但我一直收到java.lang.NullPointerException.请参阅下面的代码和异常消息.

I'm trying to append information to an existing Excel file, but I keep receiving java.lang.NullPointerException. Please refer to the code and exception message below.

代码:

private WritableSheet appendingSheet;
private static File report;

public void AppendToDoc (String path) throws IOException, WriteException, BiffException {

    this.inputFile = path;
    report = new File(inputFile);

    Workbook appendingWorkbook = Workbook.getWorkbook(new File(inputFile));
    WritableWorkbook copy = Workbook.createWorkbook(new File("output.xls"),appendingWorkbook);
    appendingSheet = copy.getSheet("Sheet 1");      
}

public void WriteToDoc (int option, String testName, String execDate, String time, boolean status) throws RowsExceededException, WriteException, IOException, BiffException{        


    int startingRow = NumOfRows(this.inputFile) + 1; //I have a function which finds the number of existing rows in the existing document. It works. 
    Label label = new Label(1, startingRow, "hello", times);    
    appendingSheet.addCell(label);

}

呼叫代码:

AppendToDoc("C:/Users/smith/ExcelTestFile.xls");
WriteToDoc(2, "This is a test", "executed", timeStamp, true);

异常消息:

线程主"中的异常java.lang.NullPointerException在jxl.write.biff.Styles.getFormat(Styles.java:214)在jxl.write.biff.CellValue.addCellFormat(CellValue.java:468)在jxl.write.biff.CellValue.setCellDetails(CellValue.java:282)在jxl.write.biff.LabelRecord.setCellDetails(LabelRecord.java:216)在jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:1199)在ExcelOperations.WriteToDoc(ExcelOperations.java:149)在ChequeImage.main(ChequeImage.java:174)

Exception in thread "main" java.lang.NullPointerException at jxl.write.biff.Styles.getFormat(Styles.java:214) at jxl.write.biff.CellValue.addCellFormat(CellValue.java:468) at jxl.write.biff.CellValue.setCellDetails(CellValue.java:282) at jxl.write.biff.LabelRecord.setCellDetails(LabelRecord.java:216) at jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:1199) at ExcelOperations.WriteToDoc(ExcelOperations.java:149) at ChequeImage.main(ChequeImage.java:174)

注意:该异常似乎发生在"appendingSheet.addCell(label);"行.

NOTE: The exception seems to be happening at the line "appendingSheet.addCell(label);".

感谢您的帮助.

编辑1 :下图显示了在执行"appendingSheet.addCell(label)"之前的appendingSheet的内容.我不确定它代表什么,但是从某种意义上说,它似乎不是空的.

EDIT 1: The below image shows the contents of appendingSheet right before "appendingSheet.addCell(label)" is executed. I'm not exactly sure what it represents, but it certianly doesn't seem to be null.

推荐答案

使用JXL API将数据写入XLS文件时,我遇到以下异常.

I have face the following Exception while writing the data into an XLS file using JXL API.

Exception in thread "main" java.lang.NullPointerException
    at jxl.write.biff.Styles.getFormat(Styles.java:214)
    at jxl.write.biff.CellValue.addCellFormat(CellValue.java:468)
    at jxl.write.biff.CellValue.setCellDetails(CellValue.java:282)
    at jxl.write.biff.LabelRecord.setCellDetails(LabelRecord.java:216)
    at jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:1199)
    at com.java.report.XLS_JXLReport.main(XLS_JXLReport.java:31)

我观察到由于 WritableFont 为空而发生以下错误.引发异常的源代码是:

I have observed the following error is occurring due to WritableFont is empty. Source code from where the exception is raised is:

Styles.java:214
    // Do the same with the statically shared fonts
    if (format.getFont() == WritableWorkbook.ARIAL_10_PT) // Line 214
    {
      format.setFont(getArial10Pt());
    }

为避免此错误,请确保提供字体名称.

使用 Java Excel API»2.6.12

public class XLS_JXLReport {
    static String filePath = "C:/Yash/";
    public static void main(String[] args) throws IOException, JXLException {
        //Creates a writable workbook with the given file name
        jxl.write.WritableWorkbook workbook = jxl.Workbook.createWorkbook(new File(filePath+"CopyCell.xls"));
        WritableSheet sheet = workbook.createSheet("My Sheet", 0);
        
        // Create cell font and format
        WritableFont cellFont = new jxl.write.WritableFont(WritableFont.TIMES, 16);
            cellFont.setColour(jxl.format.Colour.BLUE);
        WritableCellFormat cellFormat = new WritableCellFormat(cellFont);
            cellFormat.setBackground(jxl.format.Colour.GRAY_25);
        
        //cellFormat = null; // To get the NullPointerException at Styles.java:214. Uncomment this line of code.
        
        // Create the label, specifying content and format
        Label label = new Label(1, 2, "ABCD", cellFormat);
        sheet.addCell(label);
        
        //Create copies of cell in a loop
        WritableCell copiedCell = null;
        for (int i = 0 ; i < 4 ; i ++) {
            copiedCell = label.copyTo(1, 4 + i); //Deep copy cell
            sheet.addCell(copiedCell);
        }
        
        closeWorkbook(workbook);
    }
    public static void closeWorkbook(WritableWorkbook workbook) throws IOException, JXLException {
        if (workbook == null)
            return;
        if (workbook.getNumberOfSheets() == 0) {
            workbook.createSheet("No data", 0); // otherwise pointer error
        }
        //Writes out the data held in this workbook in Excel format
        workbook.write(); 
        //Close and free allocated memory 
        workbook.close(); 
    }
}


JXL API Wiki 允许用户阅读在运行时在Excel( .xls )工作簿中编写,创建,修改工作表.它不支持 .xlsx 格式.


JXL APIwiki allows users to read, write, create, and modify sheets in an Excel(.xls) workbook at runtime. It doesn't support .xlsx format.

  • JXL API支持版本为 Excel 95、97、2000,XP和2003 的Excel文档.这些文档的扩展名为 .xls
  • 对于 .xlsx ,使用 Apache POI .
  • JXL API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documents hold the extension .xls
  • For .xlsx use Apache POI.

这篇关于将信息附加到现有Excel文件时出现java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆