一、背景
导出excel,某列数据太长
二、解决
/*** 自动调整列宽** @param sheet* @param columnNumber*/private static void autoSizeColumns(Sheet sheet, int columnNumber) {for (int i = 0; i < columnNumber; i++) {int orgWidth = sheet.getColumnWidth(i);sheet.autoSizeColumn(i, true);int newWidth = (int) (sheet.getColumnWidth(i) + 100);int maxWith = 256*255;//限制下最大宽度if(newWidth > maxWith) {sheet.setColumnWidth(i, maxWith);}else if (newWidth > orgWidth) {sheet.setColumnWidth(i, newWidth);} else {sheet.setColumnWidth(i, orgWidth);}}}
宽度超过256*255就不要再往大设置了,使其自动折叠即可