private void setAutoColumnWidth(String[] titles, HSSFSheet sheet) throws Exception {
for (int columnNum = 0; columnNum <= titles.length; columnNum++) {
int columnWidth = sheet.getColumnWidth(columnNum) / 512;
for (int rowNum = 1; rowNum < sheet.getLastRowNum() - 2; rowNum++) {
HSSFRow row = null;
// 当前行未被使用过
if (sheet.getRow(rowNum) == null) {
row = sheet.createRow(rowNum);
} else {
row = sheet.getRow(rowNum);
}
if (row.getCell(columnNum) != null) {
HSSFCell cell = row.getCell(columnNum);
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = cell.getStringCellValue().getBytes("GBK").length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
sheet.setColumnWidth(columnNum, columnWidth * 512);
}
}
POI根据列中元素的宽度设置列宽
最新推荐文章于 2023-01-05 16:39:05 发布