/**
* 自动调节列宽
*/
public static void autoWidth(Sheet sheet) throws IOException {
int lastRowNum = sheet.getLastRowNum();
if(lastRowNum<10)lastRowNum=10;
Row title = sheet.getRow(0);
short lastCellNum = title.getLastCellNum();
if(lastCellNum<10)lastCellNum=10;
for (int i = 0; i <= lastCellNum; i++) {
int width = sheet.getColumnWidth(i) / 256;
for (int j = 0; j <= lastRowNum; j++) {
Row row = sheet.getRow(j);
if(row!=null){
Cell cell = row.getCell(i);
if (cell != null) {
String cellValue = getCellValue(cell);
int gbkLength = cellValue.getBytes("gbk").length;
if (gbkLength > width) {
width=gbkLength;
}
}
}
}
width=width+4;
sheet.setColumnWidth(i, (width>50?50:width) * 256);
}
}
Excel根据文字内容自动调节列宽
最新推荐文章于 2022-08-05 08:56:56 发布