2020import cn .hutool .poi .excel .BigExcelWriter ;
2121import cn .hutool .poi .excel .ExcelUtil ;
2222import me .zhengjie .exception .BadRequestException ;
23- import org .apache .poi .ss .usermodel .CellType ;
2423import org .apache .poi .util .IOUtils ;
25- import org .apache .poi .xssf .streaming .SXSSFCell ;
26- import org .apache .poi .xssf .streaming .SXSSFRow ;
2724import org .apache .poi .xssf .streaming .SXSSFSheet ;
2825import org .slf4j .Logger ;
2926import org .slf4j .LoggerFactory ;
3027import org .springframework .web .multipart .MultipartFile ;
31-
3228import javax .servlet .ServletOutputStream ;
3329import javax .servlet .http .HttpServletRequest ;
3430import javax .servlet .http .HttpServletResponse ;
@@ -217,8 +213,6 @@ public static void downloadExcel(List<Map<String, Object>> list, HttpServletResp
217213 sheet .trackAllColumnsForAutoSizing ();
218214 //列宽自适应
219215 writer .autoSizeColumnAll ();
220- //列宽自适应支持中文单元格
221- sizeChineseColumn (sheet , writer );
222216 //response为HttpServletResponse对象
223217 response .setContentType ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" );
224218 //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
@@ -231,34 +225,6 @@ public static void downloadExcel(List<Map<String, Object>> list, HttpServletResp
231225 IoUtil .close (out );
232226 }
233227
234- /**
235- * 自适应宽度(中文支持)
236- */
237- private static void sizeChineseColumn (SXSSFSheet sheet , BigExcelWriter writer ) {
238- for (int columnNum = 0 ; columnNum < writer .getColumnCount (); columnNum ++) {
239- int columnWidth = sheet .getColumnWidth (columnNum ) / 256 ;
240- for (int rowNum = 0 ; rowNum < sheet .getLastRowNum (); rowNum ++) {
241- SXSSFRow currentRow ;
242- if (sheet .getRow (rowNum ) == null ) {
243- currentRow = sheet .createRow (rowNum );
244- } else {
245- currentRow = sheet .getRow (rowNum );
246- }
247- if (currentRow .getCell (columnNum ) != null ) {
248- SXSSFCell currentCell = currentRow .getCell (columnNum );
249- if (currentCell .getCellTypeEnum () == CellType .STRING ) {
250- int length = currentCell .getStringCellValue ().getBytes ().length ;
251- // 如果长度大于最大值 65280,那就取 length
252- if (columnWidth < length || (columnWidth * 256 > 65280 )) {
253- columnWidth = length ;
254- }
255- }
256- }
257- }
258- sheet .setColumnWidth (columnNum , columnWidth * 256 );
259- }
260- }
261-
262228 public static String getFileType (String type ) {
263229 String documents = "txt doc pdf ppt pps xlsx xls docx" ;
264230 String music = "mp3 wav wma mpa ram ra aac aif m4a" ;
0 commit comments