在写入工作簿时获取java.lang.NullPointerException WorkBook.write(out)Apache POI

时间:2014-11-27 11:44:17

标签: java excel apache-poi

我在写入输出流时收到java.lang.NullPointerException

workbook.write(new FileOutputStream("test1.xslx"));

例外是:

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)

这里我有两个课程:CompareExcel课程和WriteOutputIntoExcel

我想比较两张Excel工作表Excel1.xslxExcel2.xslx,并将结果重新放回Result.xslx

我不希望将所有内容都放在Result.xslx中,我只想在Excel1Excel2.中放置不匹配的行

这是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();
        }
    }
}

现在我有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();
        }
    }
}

我还想突出显示Excel1中与Excel2不同的单元格。

请帮助我实现这一目标。

1 个答案:

答案 0 :(得分:0)

解决此类问题的最佳方法是将库的源代码(在本例中为POI)附加到IDE,这样您就可以快速查找库中第201行的内容。请注意,您需要确保库lib版本与src版本相同,否则您可以查看错误的行。

好的,很可能你的addRow参数之一是null(迭代器)。所以先检查一下(println)。

相关问题