我正在尝试将信息附加到现有的Excel文件中,但我一直在接收java.lang.NullPointerException。请参阅下面的代码和异常消息。
代码:
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);
异常消息:
线程“main”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) at jxl.write.biff.WritableSheetImpl.addCell(WritableSheetImpl.java:1199) 在ExcelOperations.WriteToDoc(ExcelOperations.java:149) 在ChequeImage.main(ChequeImage.java:174)
注意:例外情况似乎发生在“appendingSheet.addCell(label);”这一行。
感谢您的帮助。
编辑1 :下图显示了执行“appendingSheet.addCell(label)”之前的appendingSheet的内容。我不确定它代表什么,但它似乎并不是空的。
答案 0 :(得分:0)
我不能说我熟悉Biff,但POI似乎也以类似的方式运作。您好像已经创建了一个工作簿:
WritableWorkbook copy = Workbook.createWorkbook(new File(“output.xls”),appendingWorkbook);
但是你没有创建你引用的工作表:
appendingSheet = copy.getSheet(“Sheet 1”);
我猜你正在获得NPE,因为“copy.getSheet(”Sheet 1“);”返回null。与在excel中打开工作簿时不同,API可能不会为您创建默认工作表。我敢打赌,在你能够添加单元格之前,你必须创建工作表对象。
答案 1 :(得分:0)
所以......我设法解决了这个问题。
我无法解释原因,但在我将“复制”和“ appendingWorkbook ”(不是appendingSheet)重新定义为全局变量而不是本地变量之后AppendToDoc()强>;除了添加 copy.write()这一行外,该程序还有效......
感谢大家的帮助。对此,我真的非常感激。
答案 2 :(得分:0)
使用JXL API将数据写入XLS文件时,我遇到以下异常。
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
为空而发生以下错误。引发异常的源代码是:
Styles.java:214
// Do the same with the statically shared fonts
if (format.getFont() == WritableWorkbook.ARIAL_10_PT) // Line 214
{
format.setFont(getArial10Pt());
}
的示例代码为避免此错误,请确保提供字体名称。
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
格式。
Excel 95, 97, 2000, XP, and 2003
的Excel文档。这些文档的扩展名为.xls
.xlsx
,请使用Apache POI。