0
votes

I dont know why I cannot write 2 cell value with setCellValue() , it only update only 1 value in x1 cell and x2 cell not. I have a file excel and I want update value for this file with data from arraylist listThoaiThang, tt.getNoiBo() and tt.getNgoaiMang() is a string and not null when log.

Code are below, thank you.

public void writeFile(ArrayList<LLThoaiNoiHat> llList) {
    FileInputStream fis = null;
    try {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date1 = dateFormat.format(new Date()).toString();
        File tongHop = new File("Thongke_LL_NOIHAT_VTT_" + date1 + ".xlsx");
        copyFileUsingApacheCommonsIO(new File("Mau_Thongke_LL_NOIHAT_VTT.xlsx"), tongHop);  //copy sample file before edit data
        fis = new FileInputStream(tongHop);
        BufferedInputStream bis = new BufferedInputStream(fis);
        XSSFWorkbook  workbook = new XSSFWorkbook(fis);
        Sheet sheet = workbook.getSheetAt(0);
        fis.close();
        int rowNum = 9;
        Row row;

        ArrayList<LLThoaiThang> listThoaiThang;
        for (LLThoaiNoiHat tnh : llList) {
            row = sheet.getRow(rowNum++);
            int colNum = 2;
            listThoaiThang =tnh.getListThoaiThang();
            for (LLThoaiThang tt :listThoaiThang ) {
                int aa = tt.getThang() / 2+3;
                Cell x1 = row.getCell(aa);
                x1.setCellValue(tt.getNoiBo()); //only first cell ok
                Cell x2 = row.getCell(aa+1);
                x2.setCellValue(tt.getNgoaiMang());//this cell cannot change value and all cell after cannot change value
            }

        }

        FileOutputStream outFile = new FileOutputStream(tongHop);
        workbook.write(outFile);
        outFile.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
1
Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. - Joe C

1 Answers

0
votes

I was found problem here is if you set a numberic value as string it will not update after cells, I think it is a issue by apache POI so the answer is:

for (LLThoaiThang tt :listThoaiThang ) {
                int aa = tt.getThang() / 2+3;
                Cell x1 = row.getCell(aa);
                if(!tt.getNoiBo().isEmpty())
                    x1.setCellValue(Double.parseDouble(tt.getNoiBo())); //only this cell ok
                Cell x2 = row.getCell(aa+1);
                if(!tt.getNgoaiMang().isEmpty())
                    x2.setCellValue(Double.parseDouble(tt.getNgoaiMang()));;
}