当前位置: 首页 > news >正文

【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);}}
}

工具类使用方法

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;
import java.net.URLEncoder;/*** 文件接口** @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 = nulltry {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);}}}
}
http://www.lryc.cn/news/237446.html

相关文章:

  • C++二分查找算法:132模式枚举3简洁版
  • Map 和 WeakMap:JavaScript 中的键值对集合
  • linux rsyslog综合实战1
  • redis+python 建立免费http-ip代理池;验证+留接口
  • 虚幻C++ day5
  • C#中的DateTime类
  • Flutter笔记:Matrix4矩阵变换与案例
  • 数字IC前端学习笔记:时钟切换电路
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • 表内容的操作(增删查改)【MySQL】
  • C++快速入门 - 2(几分钟让你快速入门C++)
  • Excel自定义函数提取超链接
  • 计算矩阵边缘元素之和
  • 回归预测 | Matlab实现HPO-ELM猎食者算法优化极限学习机的数据回归预测
  • Flutter笔记:目录与文件存储以及在Flutter中的使用(下)
  • 机器学习笔记 - Ocr识别中的CTC算法原理概述
  • 系列二、Lock接口
  • JVM虚拟机:通过日志学习PS+PO垃圾回收器
  • 从0开始学习JavaScript--JavaScript使用Promise
  • 使用契约的链上限价订单
  • Iceberg学习笔记(1)—— 基础知识
  • springboot中动态api如何设置
  • Java —— 抽象类和接口
  • 数字IC前端学习笔记:异步复位,同步释放
  • Linux内核移植之网络驱动更改说明一
  • 邮件|gitpushgithub报错|Lombok注解
  • 【前端知识】Node——events模块的相关方法
  • 广州华锐互动VRAR | VR课件内容编辑器解决院校实践教学难题
  • Wireshark抓包:理解TCP三次握手和四次挥手过程
  • 网络工程师-HCIA网课视频学习