2015-05-04 169 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