https://bz.apache.org/bugzilla/show_bug.cgi?id=63463

            Bug ID: 63463
           Summary: Corrupt excel file using POI api ver 4.1
           Product: POI
           Version: 4.0.0-FINAL
          Hardware: PC
            Status: NEW
          Severity: critical
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: adil_ras...@visionetsystems.com
  Target Milestone: ---

Created attachment 36599
  --> https://bz.apache.org/bugzilla/attachment.cgi?id=36599&action=edit
Zip file contains Non corrupt file created using POI 3.17 and corrupt file
created using POI 4.1

We were successfully able to generate XLSX file using an XLSX template with POI
3.17. We upgraded our development environment to 4.1 and now it is creating
corrupt XLSX file. We are using POI api to insert rows dynamically and then
writing data in the cells.

Issue: We are adding dynamic rows and writing data in cells but the cells are
empty and file is corrupt.

I have attached non-corrupt excel file created in POI 3.17 and corrupt excel
file created using POI 4.1. It might help figuring out the issue.

//Function adds a row in sheet
public static void addRow(Sheet worksheet, int rowNum) {

              Row newRow = worksheet.getRow(rowNum);
              Row sourceRow = worksheet.getRow(rowNum - 1);

              // If the row exist in destination, push down all rows by 1 else
create a new row
              if (newRow != null) {
                     worksheet.shiftRows(rowNum, worksheet.getLastRowNum() + 1,
1);
                     newRow = worksheet.createRow(rowNum);
              } else {
                     newRow = worksheet.createRow(rowNum);
              }

              // Loop through source columns to add to new row
              for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
                     Cell oldCell = sourceRow.getCell(i);
                     Cell newCell = newRow.createCell(i);

                     if (oldCell == null) {
                           newCell = null;
                           continue;
                     }

                     newCell.setCellStyle(oldCell.getCellStyle());
                     newCell.setCellType(oldCell.getCellTypeEnum());
              }

              // If there are are any merged regions in the source row, copy to
new row
              for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
                     CellRangeAddress cellRangeAddress =
worksheet.getMergedRegion(i);
                     if (cellRangeAddress.getFirstRow() ==
sourceRow.getRowNum()) {
                           CellRangeAddress newCellRangeAddress = new
CellRangeAddress(
                                         newRow.getRowNum(),
                                         (newRow.getRowNum() +
(cellRangeAddress.getLastRow() - cellRangeAddress
                                                       .getFirstRow())),
cellRangeAddress
                                                       .getFirstColumn(),
cellRangeAddress
                                                       .getLastColumn());
                           worksheet.addMergedRegion(newCellRangeAddress);
                     }
              }
       }


Thanks

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to