Updating excel file using Apache POI
If you replace
//Update the value of cellcell = sheet.getRow(row).getCell(col);cell.setCellValue("Pass");
With
//Retrieve the row and check for nullHSSFRow sheetrow = sheet.getRow(row);if(sheetrow == null){ sheetrow = sheet.createRow(row);}//Update the value of cellcell = sheetrow.getCell(col);if(cell == null){ cell = sheetrow.createCell(col);}cell.setCellValue("Pass");
It will work!
Thanks Jelle Heuzel for getting a good example.
I just wanted to add the resulting working code so other can incorporate it in their code faster.
I also had to use XSSFRow instead of HSSFRow but other than that it works fine for me.
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); }}
i tried with this and work for XLSX and 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"); }}