Auto size height for rows in Apache POI

后端 未结 10 978
闹比i
闹比i 2020-12-08 18:47

I am inputting values into a spreadsheet using Apache POI. These values have newlines, and I was able to use this code successfully:

CellStyle style = cell.g         


        
10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 19:28

    Workaround for “LibreOffice Calc“ and “WPS Spreadsheet” with auto height for merged sells.

    I add a column out to the right of a main document (In my case it was 32 column) Set width as all merged cells with same text. Set style WrapText to true Set style to Align Top Copy content which will be displayed in the merged cells Set that column to be hidden Set a row height = -1

    A sample of code:

       private void applyRowHightWorkaroundForMergedCells(HSSFCell cell0) {
           HSSFSheet sheet = cell0.getSheet();
           HSSFRow row = cell0.getRow();
        
           String value = cell0.getStringCellValue();
           HSSFCell cell = row.createCell(32);
           sheet.setColumnWidth(32, 32000);
           cell.getCellStyle().setWrapText(true);
           cell.getCellStyle().setVerticalAlignment(VerticalAlignment.TOP);
           cell.setCellValue(value);
           sheet.setColumnHidden(32, true);
           row.setHeight((short) -1);
       }
    

提交回复
热议问题