Mise à jour du fichier Excel avec Apache POI

J’essaie de mettre à jour un fichier Excel existant en utilisant Apache POI. Chaque fois que je lance mon code, je reçois une erreur, comme indiqué ci-dessous. J’ai aussi essayé le truc FileInputStreamNewFile.

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) 

Veuillez trouver le code ci-dessous. Apprécier ton aide.

 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(Ssortingng[] args) throws IOException { // TODO Auto-generated method stub writeXLSXFile(3, 3); } } 

Si vous remplacez

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

Avec

 //Resortingeve 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"); 

Ça va marcher!

Merci Jelle Heuzel pour avoir un bon exemple.
Je voulais juste append le code de travail résultant pour que d’autres puissent l’intégrer plus rapidement dans leur code.

J’ai aussi dû utiliser XSSFRow au lieu de HSSFRow mais à part ça ça marche bien pour moi.

 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-parsingbeheer\\Test-Files\\exceltemplates\\template.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet = workbook.getSheetAt(0); Cell cell = null; //Resortingeve 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-parsingbeheer\\Test-Files\\exceltemplates\\Output.xlsx")); workbook.write(outFile); outFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(Ssortingng[] args) throws IOException { // TODO Auto-generated method stub writeXLSXFile(3, 3); } } 

J’ai essayé avec ça et je travaille pour XLSX et 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; //Resortingeve 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(Ssortingng[] args) throws IOException { // TODO Auto-generated method stub System.out.println("inicio"); writeXLSXFile(1, 14); System.out.println("terminado"); } }