Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SXSSF: support setting an arbitrary extra width value for column widths #657

Closed
wants to merge 1 commit into from

Conversation

pjfanning
Copy link
Contributor

see #541

It's hard to calculate a default width for SXSSF columns. The current calculated widths might be too small. This setting lets users add an arbitrary extra width to the generated column widths. It is one value for all columns.

There might be an argument to allow this setting to be set for individual columns.

  • The value must be set before you start adding rows the sheet for it to be properly applied.
  • Negative values are allowed but not encouraged

@pjfanning
Copy link
Contributor Author

merged with 41453f3

@pjfanning pjfanning closed this Aug 21, 2024
@pjfanning pjfanning deleted the extra-width branch August 21, 2024 11:36
@jkmcl
Copy link

jkmcl commented Jan 10, 2025

@pjfanning May I ask why using the new method setArbitraryExtraWidth introduced by this PR makes no difference in the code below?

private static void createFile(Path path, boolean extraWidth) throws IOException {
    try (var workbook = new SXSSFWorkbook()) {
        var sheet = workbook.createSheet();
        if (extraWidth) {
            // *** Call the new method here ***
            sheet.setArbitraryExtraWidth(SheetUtil.getDefaultCharWidthAsFloat(workbook) * 2);
        }
        sheet.trackColumnForAutoSizing(0);

        var row = sheet.createRow(0);
        row.createCell(0).setCellValue("This is a very long text that will exceed default column width for sure.");
        sheet.autoSizeColumn(0);

        try (var stream = Files.newOutputStream(path)) {
            workbook.write(stream);
        }
    }
}

@Test
void mainTest() throws IOException {
    var outDir = Path.of("target/test-output");
    Files.createDirectories(outDir);

    createFile(outDir.resolve("no_extra_width.xlsx"), false);
    createFile(outDir.resolve("extra_width.xlsx"), true);
}

The above @Test method creates two files with almost the same code path except that setArbitraryExtraWidth is not called when creating no_extra_width.xlsx. I was expecting the width of the first column in this file to be shorter but it turned out to be the same in both files:

image

Does some condition need to be met for the extra width to be honored?

FYI, I ran the code on a Windows 10 machine with Eclipse Temurin JDK 17.0.13. The complete Maven project containing the code can be found here: https://github.com/jkmcl/poi-sample

Thanks.

@pjfanning
Copy link
Contributor Author

pjfanning commented Jan 11, 2025

@jkmcl It looks like the 5.4.0 arbitrary extra width code doesn't work. I reimplemented it in 9eda246

Would you be able to try that change?

The original 5.4.0 change was never really completed or reviewed.

You could also try:

sheet.setColumnWidth(0, sheet.getColumnWidth(0) + 100);

If you want to widen a column after calling autoSizeColumn(0).

@jkmcl
Copy link

jkmcl commented Jan 12, 2025

@pjfanning Yes, my test code created a file with a wider column when setArbitraryExtraWidth was called using 9eda246

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants