2015-05-04 224 views
-1
package com.testCases; 

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Date; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFCellStyle; 
import org.apache.poi.hssf.usermodel.HSSFDataFormat; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.hssf.util.HSSFColor; 
import org.apache.poi.ss.usermodel.Cell; 
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 PoiWriteExcelFile { 

    public static void main(String[] args) throws IOException { 
      FileOutputStream fileOut = new FileOutputStream(
        "D:\\User\\ExecutionResults.xlsx"); 
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet worksheet = workbook.getSheet("Sheet1"); 
      for (int i = 0; i <= 5; i++) { 
       Cell cell=null; 
       cell=worksheet.getRow(i).getCell(0); 
       cell.setCellValue("Keyword" +i); 
       cell=worksheet.getRow(i).getCell(1); 
       cell.setCellValue("PASS" +i); 
       workbook.write(fileOut); 
      } 
     } 

} 

引发异常。什么不顺心这里...使用Apache POI编写Excel表格

异常线程 “main” 显示java.lang.NullPointerException 在com.testCases.PoiWriteExcelFile.main(PoiWriteExcelFile.java:30)

+0

您可以包括一个问题,说明你正在试图做什么,发生了什么事,而不是只是一个代号提取错误,的描述? –

+0

嗨,安德鲁,我给了..你可以检查现在..? – ChanGan

+1

你正在得到一个NullPointerException,因为你解引用一个'null'值。将这些'getRow()'和'getCell()'调出分隔线以便更易于调试,并参考[POI文档](https://poi.apache.org/apidocs/org/apache/ poi/xssf/usermodel/XSSFSheet.html)来查看这些调用何时可能返回null以及如何处理它。 –

回答

1

这是为我工作。你可以试试吗?

`import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 

public class Test { 

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

     String filePath = "D:\\xecutionResults.xlsx"; 
     Workbook workbook; 
     FileInputStream fis; 
     FileOutputStream fos; 
     try { 
      fis = new FileInputStream(filePath); 

      workbook = WorkbookFactory.create(fis); 

      org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0); 

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

       Row row = sheet.getRow(i); 

       if(row == null) 
        row = sheet.createRow(i); 

       Cell cell=row.getCell(0); 

       if(cell == null) 
        cell = row.createCell(0, Cell.CELL_TYPE_STRING); 

       cell.setCellValue("Keyword" +i); 

       cell=row.getCell(1); 

       if(cell == null) 
        cell = row.createCell(1, Cell.CELL_TYPE_STRING); 
       cell.setCellValue("PASS" +i); 

       System.out.println("1"); 
      } 
      fis.close(); 
      fos = new FileOutputStream(filePath); 
      workbook.write(fos); 
      fos.close(); 
     } catch (InvalidFormatException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }finally{ 
      //close out/in streams 
     } 

    } 

}` 

resulted file

+0

没有运气..它没有添加我想要的.. – ChanGan

+0

没有让你...是它增加一些东西......但不是你想要的东西? – Garry

+0

使用'XSSFWorkbook(java.io.File文件)'或'XSSFWorkbook(java.io.InputStream is)'来加载你的Excel表 – Garry