lamcpp
lamcpp

Reputation: 79

ApachePOI + JAVA + Excel Add values in cell doesn't work

I have the following code to write value named "yes" to Excel Cell using ApachePoi with Java. This does'n work and I don't know why?

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Testy {
    public static void main(String[] args) {
    String nazwaPliku = "Test.xlsx";
    try(OPCPackage pkg = OPCPackage.open(nazwaPliku)){
        Workbook skoroszyt = WorkbookFactory.create(pkg);
        Sheet arkusz = skoroszyt.getSheetAt(0);

        for(int i = arkusz.getFirstRowNum()+1; i <= arkusz.getLastRowNum(); i++){
            Row wiersz = arkusz.getRow(i);
            Cell komorka = wiersz.getCell(0);
            komorka.setCellValue("yes");
        }

    }
    catch(IOException | InvalidFormatException e){
        System.err.println("Bug" + nazwaPliku);
        System.exit(1);
    }
}

}

Upvotes: 0

Views: 284

Answers (1)

DHerls
DHerls

Reputation: 877

You need to write the worksheet to file.

FileOutputStream fileOut = new FileOutputStream("Test.xlsx");
skoroszyt.write(fileOut);
fileOut.close();

This website should help with other questions you have: https://poi.apache.org/spreadsheet/quick-guide.html

Upvotes: 1

Related Questions