springboot + poi导出(excel)表格,支持中文自适应列宽

本文介绍了如何在SpringBoot项目中使用POI库优化导出Excel表格,特别关注了中文内容的列宽自适应问题,提供了一个工具类和服务层的关键代码示例。
摘要由CSDN通过智能技术生成

前言

优化springboot+poi导出excel表格,支持中文自适应的工具类.

工具类

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

public class ExportEncodeUtil {
   
    public static void HeaderCode(HttpServletRequest request, HttpServletResponse response, String sheetName, HSSFWorkbook wb) throws IOException {
   
        // 针对IE或者以IE为内核的浏览器:
        String userAgent = request.getHeader("User-Agent").toLowerCase();
        String fileName = sheetName + ".xlsx";
        if (userAgent.contains("msie") || userAgent.contains("trident")) {
   
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } else {
   
            // 非IE浏览器的处理:
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("success"<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值