[英]Getting java.lang.NullPointerException while writing into workbook WorkBook.write(out) Apache POI
I am getting java.lang.NullPointerException
while writing in to an output stream: 我在写入输出流时得到
java.lang.NullPointerException
:
workbook.write(new FileOutputStream("test1.xslx"));
The exception is: 例外是:
Exception:
java.lang.NullPointerException
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:201)
at ExcelCompare.WriteOutputIntoExcel.addRow(WriteExcel.java:124)
at ExcelCompare.CompareExcel.main(MainClassExcelCompare.java:113)
Here I have two class: CompareExcel
class and WriteOutputIntoExcel
这里有两个类:
CompareExcel
类和WriteOutputIntoExcel
I want to compare two excel sheets Excel1.xslx
and Excel2.xslx
and put result back in to Result.xslx
. 我想比较两个Excel工作表
Excel1.xslx
和Excel2.xslx
并将结果放回Result.xslx
。
I don't want put everything in Result.xslx
, I just want to put the rows which don't match in both Excel1
and Excel2.
我不想将所有内容都放在
Result.xslx
,我只想在Excel1
和Excel2.
中都Excel1
不匹配的行Excel2.
Here is the Main
class 这是
Main
班
package ExcelCompare;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CompareExcel {
public static void main(String[] args) {
try {
int temp;
File excel1 = new File("C://Users/ckothakapax076037/Desktop/Demo1.xlsx");
FileInputStream fis1 = new FileInputStream(excel1);
XSSFWorkbook book1 = new XSSFWorkbook(fis1);
XSSFSheet sheet1 = book1.getSheetAt(0);
File excel2 = new File("C://Users/ckothakapax076037/Desktop/Demo2.xlsx");
FileInputStream fis2 = new FileInputStream(excel2);
XSSFWorkbook book2 = new XSSFWorkbook(fis2);
XSSFSheet sheet2 = book2.getSheetAt(0);
WriteExcel obj1 = new WriteExcel();
obj1.setOutputFile("C://Users/ckothakapax076037/Desktop/Result.xlsx");
//Get iterator to all the rows in current sheet
Iterator<Row> itr1 = sheet1.iterator();
Iterator<Row> itr2 = sheet2.iterator();
// Iterating through all cells row by row
while (itr1.hasNext() && itr2.hasNext()) {
temp = 0;
int j = 0;
Row row1 = itr1.next();
Row row2 = itr2.next();
//Get iterator to all cells of current row
Iterator<Cell> cellIterator1 = row1.cellIterator();
Iterator<Cell> cellIterator2 = row2.cellIterator();
CellStyle style = book1.createCellStyle();
style = book1.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
while (cellIterator1.hasNext() && cellIterator2.hasNext()) {
Cell cell1 = cellIterator1.next();
Cell cell2 = cellIterator2.next();
switch (cell1.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell1.getStringCellValue() + "\t");
System.out.print(cell2.getStringCellValue() + "\t");
if (!cell1.getStringCellValue().equalsIgnoreCase(cell2.getStringCellValue())) {
temp++;
cell1.setCellStyle(style);
}
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell1.getNumericCellValue() + "\t");
System.out.print(cell2.getNumericCellValue() + "\t");
if (cell1.getNumericCellValue() != cell2.getNumericCellValue()) {
temp++;
cell1.setCellStyle(style);
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell1.getBooleanCellValue() + "\t");
System.out.print(cell2.getBooleanCellValue() + "\t");
break;
case Cell.CELL_TYPE_BLANK:
System.out.print(cell1.getNumericCellValue() + "\t");
System.out.print(cell2.getNumericCellValue() + "\t");
if (cell2.getStringCellValue() != " ") {
temp++;
cell1.setCellStyle(style);
}
break;
default:
}
j++;
}
System.out.print("\n");
System.out.print("Flag value:" + temp);
System.out.print("\n");
if (temp >= 1) {
obj1.addRow(row1.cellIterator(), row2.cellIterator());
}
}
book1.close();
fis1.close();
book2.close();
fis2.close();
obj1.closerActivity();
} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
And now I have WriteOutputIntoExcel
class to put result back in to Result.xslx 现在我有了
WriteOutputIntoExcel
类,可以将结果放回Result.xslx
package ExcelCompare;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteOutputIntoExcel {
private static String OutputFile;
private static XSSFWorkbook myWorkBook = new XSSFWorkbook();
private static XSSFSheet mySheet = myWorkBook.createSheet("Report");
public static int i = 0;
public void setOutputFile(String OutputFile1) {
OutputFile = OutputFile1;
}
public void addRow(Iterator<Cell> cellIterator1, Iterator<Cell> cellIterator2) {
try {
XSSFRow row = mySheet.createRow(i++);
CellStyle style = myWorkBook.createCellStyle();
style = myWorkBook.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
FileOutputStream out = new FileOutputStream(OutputFile);
System.out.print("Writing result from Sheet1");
System.out.print("\n");
while (cellIterator1.hasNext()) {
int j = 0;
Cell cell1 = cellIterator1.next();
switch (cell1.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell1.getStringCellValue() + "\t");
row.createCell(j).setCellValue(cell1.getStringCellValue());
row.createCell(j).setCellStyle(style);
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell1.getNumericCellValue() + "\t");
row.createCell(j).setCellValue(cell1.getNumericCellValue());
row.createCell(j).setCellStyle(style);
break;
case Cell.CELL_TYPE_BLANK:
System.out.print(cell1.getStringCellValue() + "\t");
row.createCell(j).setCellValue(cell1.getStringCellValue());
row.createCell(j).setCellStyle(style);
break;
default:
System.out.print(cell1.getStringCellValue() + "\t");
row.createCell(j).setCellValue(cell1.getStringCellValue());
row.createCell(j).setCellStyle(style);
}
j++;
}
System.out.print("\n");
System.out.print("Writing result from Sheet2");
System.out.print("\n");
while (cellIterator2.hasNext()) {
int j = 0;
Cell cell2 = cellIterator2.next();
switch (cell2.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell2.getStringCellValue() + "\t");
row.createCell(j).setCellValue(cell2.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell2.getNumericCellValue() + "\t");
row.createCell(j).setCellValue(cell2.getNumericCellValue());
break;
case Cell.CELL_TYPE_BLANK:
System.out.print(cell2.getStringCellValue() + "\t");
row.createCell(j).setCellValue(cell2.getStringCellValue());
break;
default:
System.out.print(cell2.getStringCellValue() + "\t");
row.createCell(j).setCellValue(cell2.getStringCellValue());
}
j++;
}
System.out.print("\n");
myWorkBook.write(out);
out.close();
myWorkBook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void closerActivity() {
try {
System.out.println(" Hi i am in close");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I also want to highlight the cells in Excel1 which are not same as Excel2
. 我还想突出显示Excel1中与
Excel2
的单元格。
Please help me to achieve this. 请帮助我实现这一目标。
The best way to tackle this kind of problems is also attaching the sourcecode of your library (in this case POI) to your IDE, so you can quickly lookup what is happening at line 201 in the library. 解决此类问题的最佳方法也是将库的源代码(在本例中为POI)附加到IDE,以便您可以快速查找库中第201行的情况。 Please note that you need to be sure that your library lib version is the same as the src version, because else things you can look at the wrong line.
请注意,您需要确保您的库库版本与src版本相同,否则您会发现错误的行。
Ok, most likely one of your arguments to addRow is null (iterators). 好的,您对addRow的参数很可能为null(迭代器)。 So first check that (println).
因此,首先检查(println)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.