如何从一个工作表中读取并写入同一Excel工作表中的另一工作表

时间:2019-02-07 17:27:38

标签: excel apache-poi

我的代码中出现以下错误- CopyOfPMCTest.main(CopyOfPMCTest.java:91)处的java.lang.NullPointerException

第91行的代码是sheet1.getRow(rowNo).createCell(0).setCellValue(vNo);

我调试了代码,并且sheet1对象为null,这导致了错误。

// Reading of Excel sheet
    FileInputStream fis = new FileInputStream("C:\\Jayesh\\PMC\\PMCAutomation\\PMCDemo\\TestData\\TestSheet.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheetAt(0);// This is for reading V numbers in excelsheet -Sheet1
    XSSFSheet sheet1 = workbook.getSheetAt(1);// This is for writing the result in same excelsheet - Sheet2
    XSSFRow row;
    int initialRow=0;

    for (int i = 1; i <= sheet.getLastRowNum(); i++) {
        row = (XSSFRow) sheet.getRow(i);
        int vNo = (int) row.getCell(0).getNumericCellValue();
        // Owner Details
        String ownerName = "Jayesh B";
        String ownerAddress = "Add1, Add2, India";

        List<Object> Measurement = new ArrayList<Object>();
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");
        Measurement.add(1);
        Measurement.add("Test");

        // Below code is to write the VNo, Owner Name, OwnerAddress, and Measurement details
        int iterator = 0;
        int totalRows = 2;
        System.out.println("Total Rows: "+totalRows);
        int totalCols = 12;
        System.out.println("Total Cols: "+totalCols);
        int colNo;
        for (int rowNo = initialRow+1; rowNo <= initialRow+totalRows; rowNo++) {
            sheet1.getRow(rowNo).createCell(0).setCellValue(vNo);
            sheet1.getRow(rowNo).createCell(1).setCellValue(ownerName);
            sheet1.getRow(rowNo).createCell(2).setCellValue(ownerAddress);
                for (colNo = 3; colNo <= totalCols + 1; colNo++) {
                sheet1.getRow(rowNo).createCell(colNo)
                        .setCellValue(Measurement.get(iterator).toString());
                iterator = iterator + 1;
    }
            iterator = iterator + 1;

    }
        initialRow=initialRow+totalRows;
    }

    fis.close();
    FileOutputStream fos = new FileOutputStream("C:\\Jayesh\\PMC\\PMCAutomation\\PMCDemo\\TestData\\TestSheet.xlsx");
    workbook.write(fos);
    fos.close();

}

我希望代码从第一张纸上读取vNo,然后将其与ownerName,地址和测量详细信息一起写在同一张excelsheet的第二张纸上。

1 个答案:

答案 0 :(得分:0)

在第91行中进行以下更改后修复了异常错误- sheet1。 createRow (rowNo).createCell(0).setCellValue(vNo);

调试代码后,我发现对象不是null,所以问题出在我使用该对象调用的方法上。使用getRow()方法无法找到行,因此我添加了createRow()方法来创建行,然后在特定的单元格位置设置值。这种变化对我有用。我多次运行该代码,没有发现进一步的问题。