使用poi创建excel并返回response

` impl
@Override
public void exportingUserInfo(HttpServletResponse response) throws IOException {

    WritingFileUtils writingFileUtils = new WritingFileUtils();

    String[] nameArray = {"区号","手机号","设备号"};
    Map<String, List<String>> map = new HashMap<>();
    String filePath ="客户表测试";

    List<AccountBound> accountBounds = accountBoundMapper.getAccountBound();


    for (AccountBound accountBound : accountBounds) {
        ExportingUserPO exportingUserPO = new ExportingUserPO();
        BeanUtils.copyProperties(accountBound,exportingUserPO);
        List<String> list = new ArrayList<>();
        list.add(0,exportingUserPO.getMobileType().toString());
        list.add(1,exportingUserPO.getMobile());
        list.add(2,exportingUserPO.getPushToken());
        map.put(UUID.randomUUID().toString(),list);
    }
    writingFileUtils.createExcel(response,map,nameArray,filePath);


}`

WritingFileUtils工具类
生成excel
`
public void createExcel(HttpServletResponse response, Map<String, List> map, String[] strArray, String filePath) throws IOException {

    OutputStream out = null;

    // 下面几行是为了解决文件名乱码的问题
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");

    //文件名
    String oriFileName = "客户表";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode(oriFileName , "UTF-8").replaceAll("\\+", "%20");
    //生成xlsx格式
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    //生成xls格式
    //response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
    //表头行
    response.setDateHeader("Expires", 0);
    out = response.getOutputStream();

    // 第一步,创建一个webbook,对应一个Excel文件
    HSSFWorkbook wb = new HSSFWorkbook();
    // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
    HSSFSheet sheet = wb.createSheet("sheet1");
    sheet.setDefaultColumnWidth(20);// 默认列宽
    // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
    HSSFRow row = sheet.createRow((int) 0);
    // 第四步,创建单元格,并设置值表头 设置表头居中
    HSSFCellStyle style = wb.createCellStyle();
    // 创建一个居中格式
    style.setAlignment(HorizontalAlignment.CENTER);
    //style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    // 添加excel title
    //表头内容
    HSSFCell cell = null;
    for (int i = 0; i < strArray.length; i++) {
        cell = row.createCell((short) i);
        cell.setCellValue(strArray[i]);
        cell.setCellStyle(style);
    }

    // 第五步,写入实体数据 实际应用中这些数据从数据库得到,list中字符串的顺序必须和数组strArray中的顺序一致
    int i = 0;
    for (String str : map.keySet()) {
        row = sheet.createRow((int) i + 1);
        List<String> list = map.get(str);

        // 第四步,创建单元格,并设置值
        for (int j = 0; j < strArray.length; j++) {
            String value;
            if (j >= list.size()){
                value = "";
            }else {
                value = list.get(j);
            }
            row.createCell((short) j).setCellValue(value);
        }
        i++;
    }

    // 第六步,将文件存到指定位置
    try {
        wb.write(out);//将Excel用response返回
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}`
posted @   遍地都是月光  阅读(233)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示