2015-08-28 178 views
4

我想使用Apache POI更新現有的Excel文件。每次運行我的代碼時,都會收到如下所示的錯誤消息。我也試過FileInputStreamNewFile的東西。使用Apache POI更新Excel文件

Exception in thread "main" java.lang.NullPointerException 
    at com.gma.test.WriteExcelTest.writeXLSXFile(WriteExcelTest.java:26) 
    at com.gma.test.WriteExcelTest.main(WriteExcelTest.java:44) 

請找到下面的代碼。感謝你的幫助。

package com.gma.test; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

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 WriteExcelTest { 

    public static void writeXLSXFile(int row, int col) throws IOException { 
     try { 
      FileInputStream file = new FileInputStream("C:\\Anuj\\Data\\Data.xlsx"); 

      XSSFWorkbook workbook = new XSSFWorkbook(file); 
      XSSFSheet sheet = workbook.getSheetAt(0); 
      Cell cell = null; 

      //Update the value of cell 
      cell = sheet.getRow(row).getCell(col); 
      cell.setCellValue("Pass"); 

      file.close(); 

      FileOutputStream outFile =new FileOutputStream(new File("C:\\Anuj\\Data\\Data.xlsx")); 
      workbook.write(outFile); 
      outFile.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     writeXLSXFile(3, 3); 
    } 

} 
+0

檢查sheet.getRow(行)不爲空,然後如果你問一個行單元格不設置值 –

+0

前空|沒有定義的單元格,你會得到一個空值! –

回答

6

如果更換

//Update the value of cell 
cell = sheet.getRow(row).getCell(col); 
cell.setCellValue("Pass"); 

隨着

//Retrieve the row and check for null 
HSSFRow sheetrow = sheet.getRow(row); 
if(sheetrow == null){ 
    sheetrow = sheet.createRow(row); 
} 
//Update the value of cell 
cell = sheetrow.getCell(col); 
if(cell == null){ 
    cell = sheetrow.createCell(col); 
} 
cell.setCellValue("Pass"); 

很有效!

+1

如果sheet.getRow(row)返回Null,該怎麼辦? –

+0

在行上做類似檢查 –

+0

是的,行和單元格都是從0開始的,所以在這個例子中,如果他沒有D4的值,他會得到一個NullPointerException。 –

3

謝謝Jelle Heuzel爲您舉個好榜樣。
我只是想添加生成的工作代碼,以便其他人可以更快地將它合併到代碼中。

我也不得不使用XSSFRow而不是HSSFRow,但除此之外它對我來說工作正常。

package stackoverflow.appachePOI; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class WriteExcelTest { 

    public static void writeXLSXFile(int row, int col) throws IOException { 
     try { 
      FileInputStream file = new FileInputStream("C:\\Users\\Sam\\files\\Masterproef lca\\lca-analysebeheer\\Test-Files\\exceltemplates\\template.xlsx"); 

      XSSFWorkbook workbook = new XSSFWorkbook(file); 
      XSSFSheet sheet = workbook.getSheetAt(0); 
      Cell cell = null; 

      //Retrieve the row and check for null 
      XSSFRow sheetrow = sheet.getRow(row); 
      if(sheetrow == null){ 
       sheetrow = sheet.createRow(row); 
      } 
      //Update the value of cell 
      cell = sheetrow.getCell(col); 
      if(cell == null){ 
       cell = sheetrow.createCell(col); 
      } 
      cell.setCellValue("Pass"); 

      file.close(); 

      FileOutputStream outFile =new FileOutputStream(new File("C:\\Users\\Sam\\files\\Masterproef lca\\lca-analysebeheer\\Test-Files\\exceltemplates\\Output.xlsx")); 
      workbook.write(outFile); 
      outFile.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     writeXLSXFile(3, 3); 
    } 

} 
+0

這裏給出空指針異常XSSFSheet sheet = workbook.getSheetAt(0);這裏有人嗎? –

+0

第一次猜測是在文件流中出錯,並且無法創建工作簿。 – turoni

1

我想這個工作對XLSX和XSSF

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class TestStackOver { 

    public static void writeXLSXFile(int row, int col) throws IOException { 
     try { 
      FileInputStream file = new FileInputStream(Constante.ruta); 
      XSSFWorkbook workbook = new XSSFWorkbook(file); 
      XSSFSheet sheet = workbook.getSheetAt(0); 
      Cell cell = null; 
      //Retrieve the row and check for null 
      XSSFRow sheetrow = sheet.getRow(row); 
      if(sheetrow == null){ 
       sheetrow = sheet.createRow(row); 
      } 
      //Update the value of cell 
      cell = sheetrow.getCell(col); 
      if(cell == null){ 
       cell = sheetrow.createCell(col); 
      } 
      cell.setCellValue("Pass"); 

      file.close(); 


      FileOutputStream outFile =new FileOutputStream(new File(Constante.ruta_salida)); 
      workbook.write(outFile); 
      outFile.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 
     System.out.println("inicio"); 
     writeXLSXFile(1, 14); 
     System.out.println("terminado"); 
    } 

}