0

I am studying POI and I want to find a simple way to write in column B and in each row, certain values.

Right now I have the code below, which reads the spreadsheet, but does not write.

public static void main(String[] args) throws IOException, InvalidFormatException {

        File CaminhoArquivo = new File("tools\\cnpj.xlsx");

        XSSFWorkbook Planilha = new XSSFWorkbook(CaminhoArquivo);

        XSSFSheet sheet = Planilha.getSheetAt(0);

        int QuantidadeDeLinhas = sheet.getLastRowNum();
        System.out.println("Quant. de LInhas: " + QuantidadeDeLinhas);
        
        
        for (int i = 1; i <= QuantidadeDeLinhas; i++) {
            
            Row coluna = sheet.getRow(i);
            Cell resultado = coluna.getCell(1);
        
            //Write Line
            resultado.setCellValue("10");
            
        }

        Planilha.close();       
        
        FileOutputStream outFile = new FileOutputStream(new File("tools\\cnpj.xlsx"));
        Planilha.write(outFile);
        outFile.close();
        System.out.println("Arquivo Excel editado com sucesso!");
        
    }

ERROR

Exception in thread "main" java.lang.NullPointerException at lendoXlsx.xlsx.ReadExcel.main(ReadExcel.java:35)

//Write Line
    resultado.setCellValue("10");

sHEET

1

2 Answers 2

2

Cells have to exist before you can set a cell value for them. getCell(1) returns null because it hasn't been created yet. You can try getCell(1, MissingCellPolicy.CREATE_NULL_AS_BLANK) or use createCell(1) if you don't care that you override the cell.

When using POI i highly recommend to use the interfaces and not the implementations of XSSFWorkbook (Interface is Workbook) and XSSFSheet (Interface is Sheet). (For row and cell you are already using the interfaces) Instead of creating your workbook with a constructor you can use WorkbookFactory.create. This way you're code will work for xls and xlsx. There are some cases where you need the implementation instead of the interface, but 99% of the time you don't.

Sign up to request clarification or add additional context in comments.

Comments

1

You have three issues in your code. First, the cell you are writing to does not exist, so you have to create it first by calling the createCell() method:

for (int i = 1; i <= QuantidadeDeLinhas; i++) {
    Row coluna = sheet.getRow(i);
    
    coluna.createCell(1);
    Cell resultado = coluna.getCell(1);

    //Write Line
    resultado.setCellValue("10");
    
}

Next, you are closing the XSSFWorkbook before trying to write it out.

Finally, you are writing out when the document was created from a File instead of an InputStream. So, you must either write out to a separate file, or open it from an InputStream.

Here I have moved the workbook close and am writing out to a new file.

//Planilha.close();       

FileOutputStream outFile = new FileOutputStream(new File("tools\\cnpj_new.xlsx"));
Planilha.write(outFile);
outFile.close();
Planilha.close();
System.out.println("Arquivo Excel editado com sucesso!");

1 Comment

Perfect, thank you so much for your help and especially for detailing what was wrong with my code, so I learn more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.