已解决
【Java】ExcelWriter自适应宽度工具类(支持中文)
来自网友在路上 195895提问 提问时间:2023-11-19 04:34:28阅读次数: 95
最佳答案 问答题库958位专家为你答疑解惑
工具类
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;/*** 文件接口** @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);}}}
}
查看全文
99%的人还看了
相似问题
- 怎么让百度快速收录,百度SEO收录工具
- 树莓派镜像安装 + 设置 + 镜像批量化操作 - 自动化烧写工具 (四)
- Linux CentOS+宝塔面板工具结合内网穿透实现网站发布至公网可访问
- Ps:裁剪工具 - 裁剪预设的应用
- 轻量级压测工具Apache Bench实战
- 3DEXPERIENCE许可管理工具:掌控设计软件许可,提高企业竞争力
- python自动化标注工具+自定义目标P图替换+深度学习大模型(代码+教程+告别手动标注)
- linux驱动开发.之spi测试工具spidev_test源码(一)
- 计算机网络实用工具之fping
- 推荐一个windows上传linux服务器/linux服务器的docker镜像的工具,摆脱docker cp,以及解决常见问题。
猜你感兴趣
版权申明
本文"【Java】ExcelWriter自适应宽度工具类(支持中文)":http://eshow365.cn/6-38912-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 单词故事嵌入:通过自然语言处理解开叙事
- 下一篇: 轻量化网络--MobileNet V1