NiklasMelznerExxcellent opened a new pull request, #541:
URL: https://github.com/apache/poi/pull/541

   The XSSFSheet#autoSizeColumn method doesn't work properly for some specific 
cell values and font metrics.
   
   The following code snippet is an example for this behavior:
   ```java
   String sheetName = "Sheet 1";
   String cellText = "Auto sizing incorrect! :(";
   
   try (XSSFWorkbook workbook = new XSSFWorkbook()) {
   
       final XSSFSheet sheet = workbook.createSheet(sheetName);
   
       final XSSFCell cell = sheet.createRow(0).createCell(0);
   
       cell.setCellValue(cellText);
   
       final XSSFFont font = workbook.createFont();
       font.setFontName("Arial");
       font.setFontHeight(15);
       font.setBold(true);
   
       final XSSFCellStyle style = workbook.createCellStyle();
       style.setFont(font);
       cell.setCellStyle(style);
   
       sheet.autoSizeColumn(0, false);
   
       try (FileOutputStream fOS = new FileOutputStream("./test-excel.xlsx")) {
           workbook.write(fOS);
       }
   }
   ```
   
   Generated excel file:
   
![image](https://github.com/apache/poi/assets/149682193/bcc52217-4da9-44e1-b095-56d560636793)
   
   The bug is caused by a rounding error in SheetUtil#getDefaultCharWidth:
   
https://github.com/apache/poi/blob/5533d35cefd15e98fd988a458ac41c5e8aad06f7/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java#L301-L302
   
   Rounding is not necessary at all, removing the operation and converting the 
return type of getDefaultCharWidth() to double solves the issue.
   
   The column width calculation that causes the error (and uses a value 
returned by SheetUtil#getDefaultCharWidth) occurs in SheetUtil#getCellWidth:
   
https://github.com/apache/poi/blob/5533d35cefd15e98fd988a458ac41c5e8aad06f7/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java#L244


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to