POI自動調整列寬支持中文


 1 /**
 2      * @Description:表格自適應寬度(中文支持)
 3      * @Author: 
 4      * @param sheet sheet
 5      * @param columnLength 列數
 6      */
 7     private static void setSizeColumn(HSSFSheet sheet, int columnLength) {
 8         for (int columnNum = 0; columnNum <= columnLength; columnNum++) {
 9             int columnWidth = sheet.getColumnWidth(columnNum) / 256;
10             for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
11                 HSSFRow currentRow; // 當前行未被使用過
12                 if (sheet.getRow(rowNum) == null) {
13                     currentRow = sheet.createRow(rowNum);
14                 } else {
15                     currentRow = sheet.getRow(rowNum);
16                 }
17                 if (currentRow.getCell(columnNum) != null) {
18                     HSSFCell currentCell = currentRow.getCell(columnNum);
19                     if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
20                         int length = currentCell.getStringCellValue().getBytes().length;
21                         if (columnWidth < length) {
22                             columnWidth = length;
23                         }
24                     }
25                 }
26             }
27             sheet.setColumnWidth(columnNum, columnWidth * 256);
28         }
29     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM