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

            Bug ID: 69266
           Summary: File Corruption when Modifying Page Setup in Existing
                    .xlsx Sheets
           Product: POI
           Version: unspecified
          Hardware: PC
                OS: Mac OS X 10.1
            Status: NEW
          Severity: normal
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: nikknp.du...@gmail.com
  Target Milestone: ---

When using Apache POI to modify the page setup of an existing .xlsx sheet,
specifically setting the “Fit to Page” options (setFitWidth and setFitHeight),
the resulting file becomes corrupted and cannot be opened by Excel. This issue
occurs when reading an existing .xlsx file, applying the “Fit to Page”
settings, and then saving the modified sheet to a new file. The corruption
seems to stem from the way POI handles the modification of print setup options
in sheets that already have pre-existing configurations, leading to invalid or
incomplete file structures in the output .xlsx file.
Steps to Reproduce:

        1.      Open an existing .xlsx file using Apache POI.
        2.      Access a sheet and modify the page setup by enabling “Fit to
Page” and setting both width and height to 1.
        3.      Save the modified sheet to a new .xlsx file.
        4.      Attempt to open the newly saved file in Excel.

Java Code - 
        String inputFilePath =
"/Users/username/Downloads/SpreadsheetSetFitToPageRead.xlsx";
        String outputFilePath =
"/Users/username/Downloads/SpreadsheetSetFitToPageReadTrue.xlsx";

        try (FileInputStream fis = new FileInputStream(new
File(inputFilePath));
             Workbook workbook = WorkbookFactory.create(fis)) {

            // Get the first sheet from the workbook (or specify the sheet
index/name)
            Sheet sheet = workbook.getSheetAt(0);

            // Set Fit to Page options
            sheet.setFitToPage(true);
            PrintSetup printSetup = sheet.getPrintSetup();
            printSetup.setFitWidth((short) 1);  // Fit to 1 page wide
            printSetup.setFitHeight((short) 1); // Fit to 1 page tall

            // Write the modified workbook to a new file
            try (FileOutputStream fos = new FileOutputStream(new
File(outputFilePath))) {
                workbook.write(fos);
            }

            System.out.println("Sheet modifications are saved successfully in "
+ outputFilePath);

        } catch (IOException e) {
            e.printStackTrace();
        }

-- 
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