J'essaie de mettre à jour un fichier Excel existant à l'aide d'Apache POI. Chaque fois que j'exécute mon code, je reçois une erreur comme indiqué ci-dessous. J'ai également essayé la chose 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(String[] 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
//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");
Ça va marcher!
Merci Jelle Heuzel pour avoir donné un bon exemple.
Je voulais juste ajouter le code de travail résultant afin que d'autres puissent l'intégrer plus rapidement dans leur code.
J'ai également dû utiliser XSSFRow au lieu de HSSFRow mais à part cela, cela fonctionne 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-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);
}
}
j'ai essayé avec cela 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;
//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");
}
}