Excel(.xlsx)写不使用java

时间:2017-03-09 14:08:32

标签: java excel nullpointerexception apache-poi office-2016

我无法使用我的java代码更新Excel文件(.xlsx)。你可以告诉我在哪里弄错了。它会抛出一个NullPointerException。使用POI Apache jar。使用Office 2016 for Excel

public class test_extra1 {
    public static void main(String[] args) throws Exception {
        //excel code
        File src=new File("C:\\test.xlsx");
        FileInputStream fis=new FileInputStream(src);
        XSSFWorkbook wb=new XSSFWorkbook(fis);
        XSSFSheet sh1= wb.getSheetAt(0);

        String[] field_name = {"field1","field2"};

        String isChecked ="Checked",temp;

        int i;
        for(i=0;i<2;i++){

            System.out.println(i);
            System.out.println("Checkbox not checked--"+isChecked);
            temp = field_name[i];
            System.out.println("temp----"+temp);
            //error comes at below line
            sh1.getRow(i+1).getCell(i+3).setCellValue("No");
            sh1.getRow(i+1).createCell(i+3).setCellValue("No");
            sh1.getRow(i+1).createCell(i+2).setCellValue(temp);

        }
        FileOutputStream fout=new FileOutputStream(new File("C:\\test1.xlsx"));
        wb.write(fout);
        fout.close();

    }
}

0 个答案:

没有答案