写入excel工作簿时出现空指针异常

时间:2015-06-03 20:05:57

标签: java excel apache-poi

Exception in thread "main" java.lang.NullPointerException
    at add.copy(add.java:69)
    at add.main(add.java:33)

大家好,这是我提出的第一个问题所以如果我做错了,我很抱歉。我目前正在制作一个程序,需要读取Excel工作表并将其添加到已包含一个工作表的Excel工作簿中。由于堆大小错误,我正在分段进行读写。我现在可以成功地读取和写入每个部分,直到最后一部分。当它试图做最后一节我得到上面看到的错误。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class add {

    public static void main(String[] args) throws IOException {

        XSSFSheet sheet1=get();
        int fRow = 0;
        int lRow = 0;
        int rownum = 0;
        int addon = 0;
        int num = 0;
        int numi = 100/20;
        num = numi + numi;
        fRow = sheet1.getFirstRowNum();
        lRow = 553;
        copy(fRow,lRow,sheet1);
        System.out.println("5%");
        rownum = 129000;
        addon = rownum/20;

        for(int i=0;i<20;i++){
            System.out.println(num + "%");
            fRow = lRow;
            lRow = lRow + addon;
            copy(fRow,lRow,sheet1);
            num = num + numi;
        }
        //System.out.println("100%");
    }
    public static void copy(int fRow, int lRow, XSSFSheet sheet1) throws IOException{

        File excel2 =  new File ("C:/Users/Colm/Documents/windmill_proj/excels/testing.xlsx");
        FileInputStream bis = new FileInputStream(excel2);
        XSSFWorkbook workbook = null;
        workbook = new XSSFWorkbook(bis);
        XSSFSheet mySheet = null;
        XSSFRow row1 = null;
        XSSFRow row2 = null;
        XSSFCell cell2 = null;
        XSSFCell cell1 = null;
        int fCell = 0;
        int lCell = 0;
        String sheetname1 = sheet1.getSheetName();
        int sindex = workbook.getSheetIndex(sheetname1);


        if(sindex==-1){
            mySheet = workbook.createSheet(sheet1.getSheetName());
            System.out.println("sheet made");
        }
        else{
            mySheet = workbook.getSheetAt(1);
        }


        for (int iRow = fRow; iRow <= lRow; iRow++)
        {
            row1 = sheet1.getRow(iRow);
            row2 = mySheet.createRow(iRow);

            fCell = row1.getFirstCellNum();
            lCell = row1.getLastCellNum();

            for (int iCell = fCell; iCell < lCell; iCell++)
            {
                cell2 = row1.getCell(iCell);
                cell1 = row2.createCell(iCell);

                cell1.setCellType(cell2.getCellType());

                // Set the cell data value
                switch (cell2.getCellType()) {
                    case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK:
                        cell1.setCellValue(cell2.getStringCellValue());
                        break;
                    case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
                        cell1.setCellValue(cell2.getBooleanCellValue());
                        break;
                    case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR:
                        cell1.setCellErrorValue(cell2.getErrorCellValue());
                        break;
                    case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:
                        cell1.setCellFormula(cell2.getCellFormula());
                        break;
                    case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
                        cell1.setCellValue(cell2.getNumericCellValue());
                        break;
                    case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
                        cell1.setCellValue(cell2.getRichStringCellValue());
                        break;

                }
            }
        }

         FileOutputStream out = new FileOutputStream("C:/Users/Colm/Documents/windmill_proj/excels/testing.xlsx"); 
         workbook.write(out);
         out.flush();
         out.close();
         workbook.close();
         bis.close();
    }
    public static XSSFSheet get() throws IOException{
        File fn =  new File ("C:/Users/Colm/Documents/windmill_proj/excels/testing2.xlsx");
        FileInputStream biss = new FileInputStream(fn);
        XSSFWorkbook workbook = null;
        workbook = new XSSFWorkbook(biss);
        biss.close();
        XSSFWorkbook myWorkBook = new XSSFWorkbook();
        XSSFSheet sheet = null;
        XSSFRow row = null;
        XSSFCell cell = null;
        XSSFSheet mySheet = null;
        XSSFRow myRow = null;
        XSSFCell myCell = null;
        int sheets = 1;
        int fCell = 0;
        int lCell = 0;
        int fRow = 0;
        int lRow = 0;
        for (int iSheet = 0; iSheet < sheets; iSheet++) {
            sheet = workbook.getSheetAt(iSheet);
            if (sheet != null) {
                mySheet = myWorkBook.createSheet(sheet.getSheetName());
                fRow = sheet.getFirstRowNum();
                lRow = sheet.getLastRowNum();
                for (int iRow = fRow; iRow <= lRow; iRow++) {
                    row = sheet.getRow(iRow);
                    myRow = mySheet.createRow(iRow);
                    if (row != null) {
                        fCell = row.getFirstCellNum();
                        lCell = row.getLastCellNum();
                        for (int iCell = fCell; iCell < lCell; iCell++) {
                            cell = row.getCell(iCell);
                            myCell = myRow.createCell(iCell);
                            if (cell != null) {
                                myCell.setCellType(cell.getCellType());
                                switch (cell.getCellType()) {
                                case XSSFCell.CELL_TYPE_BLANK:
                                    myCell.setCellValue("");
                                    break;

                                case XSSFCell.CELL_TYPE_BOOLEAN:
                                    myCell.setCellValue(cell.getBooleanCellValue());
                                    break;

                                case XSSFCell.CELL_TYPE_ERROR:
                                    myCell.setCellErrorValue(cell.getErrorCellValue());
                                    break;

                                case XSSFCell.CELL_TYPE_FORMULA:
                                    myCell.setCellFormula(cell.getCellFormula());
                                    break;

                                case XSSFCell.CELL_TYPE_NUMERIC:
                                    myCell.setCellValue(cell.getNumericCellValue());
                                    break;

                                case XSSFCell.CELL_TYPE_STRING:
                                    myCell.setCellValue(cell.getStringCellValue());
                                    break;
                                default:
                                    myCell.setCellFormula(cell.getCellFormula());
                                }
                            }
                        }
                    }
                }
            }
        }
        myWorkBook.close();
        workbook.close();
        System.out.println("done");
        return mySheet;
    }
}

如果有人可以提出可能存在的问题,或者我可以通过哪种方式测试以找到问题,我将非常感激。 谢谢!

0 个答案:

没有答案