• 【Java】ExcelWriter自适应宽度工具类(支持中文)


    工具类

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellType;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    
    /**
     * Excel工具类
     *
     * @author xiaoming
     * @date 2023/11/17 10:40
     */
    public class ExcelUtils {
    
    	/**
         * 自适应宽度(支持中文)
         *
         * @param sheet 表单
         * @param size  因为for循环从0开始,size值为 列数-1
         */
        public static void setSizeColumn(Sheet sheet, int size) {
            for (int columnNum = 0; columnNum <= size; columnNum++) {
                int columnWidth = sheet.getColumnWidth(columnNum) / 256;
                for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                    Row currentRow;
                    // 当前行未被使用过
                    if (sheet.getRow(rowNum) == null) {
                        currentRow = sheet.createRow(rowNum);
                    } else {
                        currentRow = sheet.getRow(rowNum);
                    }
    
                    if (currentRow.getCell(columnNum) != null) {
                        Cell currentCell = currentRow.getCell(columnNum);
                        if (currentCell.getCellType() == CellType.STRING) {
                            int length = currentCell.getStringCellValue().getBytes().length;
                            if (columnWidth < length) {
                                columnWidth = length;
                            }
                        }
                    }
                }
                sheet.setColumnWidth(columnNum, columnWidth * 256);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    工具类使用方法

    import cn.hutool.core.collection.CollUtil;
    import cn.hutool.core.io.IoUtil;
    import cn.hutool.http.HttpUtil;
    import cn.hutool.poi.excel.ExcelUtil;
    import cn.hutool.poi.excel.ExcelWriter;
    import com.alibaba.fastjson.JSONObject;
    import com.xxx.utils.ExcelUtils;
    import com.xxx.entity.Entity;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.springframework.web.bind.annotation.*;
    
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.List;
    
    /**
     * 文件接口
     *
     * @author xiaoming
     * @date 2023/11/17 10:50
     */
    @RestController
    @RequestMapping("/file")
    public class FileController {
    
    	@PostMapping("exportExcel")
    	public void exportExcel(HttpServletResponse response) {
    		// 假设这里有个List是要导出用的
    		List<Entity> list = ...;
    
    		ExcelWriter writer = ExcelUtil.getWriter(true);
    		writer.addHeaderAlias("field1", "fieldValue1")
    				.addHeaderAlias("field2", "fieldValue2")
    				.addHeaderAlias("field3", "fieldValue3");
    		
    		if (CollUtil.isNotEmpty(list)) {
    			writer.write(list, true);
    
    			// 就只有下面两步操作,根据上面addHeaderAlias的个数减一就行,上面是三个,所以填二
    			Sheet sheet = writer.getSheet();
    			ExcelUtils.setSizeColumn(sheet, 2);
    
    			ServletOutputStream out = null
    			try {
    				response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
    				response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("xxx.xlsx", "UTF-8"));
    				out = response.getOutputStream();
    				writer.flush(out, true);
    			} catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    writer.close();
                    IoUtil.close(out);
                }
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
  • 相关阅读:
    【Unity】线性代数基础:矩阵、矩阵乘法、转置矩阵、逆矩阵、正交矩阵等
    酷快讯:Eminem和Snoop Dogg无聊猿新MV加持后ApeCoin上涨22%
    0/1分数规划专题
    Python怎么打印彩色字符串
    Spring Boot的Web开发之Thymeleaf模板引擎的解析及使用(Thymeleaf的基础语法以及常用属性)
    vue项目中,多个数据并发请求
    java项目-第159期ssm超市管理系统_ssm毕业设计_计算机毕业设计
    成都优优聚公司是靠谱的吗?
    JFLASH基本使用总结
    站点服务ISiteService
  • 原文地址:https://blog.csdn.net/weixin_50223520/article/details/134457671