Workbook workbook = new Workbook();
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
cells.setColumnWidthPixel(0, 170);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Base64OutputStream base64OutputStream = new Base64OutputStream(baos);
workbook.save(base64OutputStream, SaveFormat.XLSX);
After saving the workbook using the code above and running it on different server environments, the column widths in the converted HTML file are not the same as the previously set widths,one is 170 and the other is 146.What could be causing this?
setColumnWidthPixel stores the width as an internal Excel column‑width value that is later converted back to pixels when the workbook is saved as HTML.
Because the conversion uses runtime information such as screen DPI and the default font metrics, differences in those settings between server environments can lead to slightly different column widths in the generated HTML.
Possible reasons for the discrepancy
The JVM on each server may report a different DPI (e.g., a headless environment or a custom display‑scale setting).
The default font used by Aspose.Cells (or the system default font if none is set) can vary between operating‑system installations, affecting the width of the “0” character that Excel uses for its column‑width unit.
Different versions of Aspose.Cells may implement the pixel‑to‑column conversion with minor variations.
What you can try
Verify that the same Aspose.Cells version is installed on all servers.
Ensure the JVM reports the same DPI on each machine (for example, run with a consistent -Djava.awt.headless=true setting).
Explicitly set the workbook’s default font and size before calling setColumnWidthPixel, e.g.:
If exact pixel precision is required, consider using the HTML‑save options (if available) to export column widths directly as pixels.
If the problem persists, please provide the Aspose.Cells version you are using and details about the JVM/DPI configuration on the servers so the issue can be investigated further.
the version is aspose-cells-java-20.12 and before calling setColumnWidthPixel, we set the workbook’s default font and size.
Style defaultStyle = workbook.getDefaultStyle();
Font font = defaultStyle.getFont();
font.setName("Times New Roman");
font.setSize(10);
workbook.setDefaultStyle(defaultStyle);
How Aspose Cells Java determines the width passed to setColumnWidthPixel
Internal storage – When you call
cells.setColumnWidthPixel(colIndex, pixelValue);
the library does not keep the pixel value directly. It converts the pixel count to the internal Excel column‑width unit (the same unit that Excel uses for the “width” property) and stores that value in the workbook. The conversion back to pixels is performed later, e.g., when the workbook is saved as HTML [1].
What the conversion uses – The pixel‑to‑column‑width conversion depends on runtime information:
Screen DPI reported by the JVM (headless environments may report a different DPI) [1].
Default‑font metrics – the width of the “0” character in the workbook’s default font is the basis of Excel’s column‑width unit. If the default font (name or size) differs between machines, the same stored column‑width value will be rendered as a different pixel count [1].
Maximum allowed width – Excel caps a column at 1790 pixels, which corresponds to 255 points. Aspose Cells enforces the same limit, so any pixel value you pass must be ≤ 1790 px (or ≤ 255 points) [4][5].
Steps to obtain a reliable pixel width
Step
Action
Reason
1
Set the workbook’s default font and size before calling setColumnWidthPixel (as you already do).
Guarantees that the same font metrics are used on every server, eliminating one source of variation [1].
2
Ensure the JVM reports the same DPI on all machines (e.g., run in a consistent headless mode).
The conversion uses the DPI reported by the JVM; a uniform DPI yields identical pixel results [1].
3
Use the same Aspose Cells version on all machines (e.g., Aspose‑Cells‑Java‑20.12).
Different library versions may implement the conversion algorithm with minor variations [1].
4
Stay within the limits – do not request a pixel width larger than 1790 px (or 255 points). Attempting to do so will raise “Column width must be between 0 and 255” [5].
Following these steps will give you deterministic column widths across different environments. If a mismatch still occurs after applying the checklist, it is likely a library‑specific issue that can be investigated further with the exact JVM/DPI configuration.
@xuerui
We can obtain the correct results by testing on the latest version v25.12 using the following sample code. Please refer to the attachment. result.zip (32.6 KB)
Workbook workbook = new Workbook();
Style defaultStyle = workbook.getDefaultStyle();
Font font = defaultStyle.getFont();
font.setName("Times New Roman");
font.setSize(10);
workbook.setDefaultStyle(defaultStyle);
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
cells.setColumnWidthPixel(0, 170);
workbook.save(filePath + "out_java.html");
If you still have questions, please provide your complete and runnable sample code, result files, testing environment information, including but not limited to operating system, JDK version, etc. We will check it soon.