Apache POI 4.0.1版本写入普通Excel文件(兼容 xls 和 xlsx)(四)

2021-03-05 09:26

阅读:751

标签:load   static   ota   framework   boot   方式   http   cache   writer   

一般在公司写项目,写入Excel时会有这样的场景:前台页面上有一个导出按钮,点击后将后台某张表里的数据以Excel的形式导出,导出的Excel文件通过浏览器下载。

基于最新的Apache POI 4.0.1版本来总结一下写入Excel的过程。

代码前准备:

添加4.0.1 poi maven依赖


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0com.springbootemaildemo
    springboot-email-demo
    0.0.1-SNAPSHOTjarspringboot-email-demoDemo project for Spring Bootorg.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASEUTF-8UTF-81.8org.springframework.boot
            spring-boot-starter-web
        org.springframework.boot
            spring-boot-starter-test
            testorg.springframework.boot
            spring-boot-starter-mail
        org.projectlombok
            lombok
            trueio.springfox
            springfox-swagger2
            2.7.0io.springfox
            springfox-swagger-ui
            2.7.0org.jsoup
            jsoup
            1.11.3com.alibaba
            fastjson
            1.2.41org.apache.commons
            commons-lang3
            3.10commons-beanutils
            commons-beanutils
            1.8.3javax.persistence
            persistence-api
            1.0
     org.apache.poi
            poi
            4.0.1org.apache.poi
            poi-ooxml
            4.0.1

    org.springframework.boot
                spring-boot-maven-plugin
            

DataVo(数据封装类)

package com.springbootemaildemo.excel.b;

/**
 * 读取Excel时,封装读取的每一行的数据
 */
public class DataVo {
    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

ExcelWriter(写入excel文件主要代码类)

package com.springbootemaildemo.excel.b;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExcelWriter {
    private static List CELL_HEADS; //列头

    static {
        // 类装载时就载入指定好的列头信息,如有需要,可以考虑做成动态生成的列头
        CELL_HEADS = new ArrayList();
        CELL_HEADS.add("姓名");
        CELL_HEADS.add("年龄");
    }

    /**
     * 生成Excel并写入数据信息
     *
     * @param dataList 数据列表
     * @return 写入数据后的工作簿对象
     */
    public static Workbook exportData(List dataList) {
        // 生成xlsx的Excel
        Workbook workbook = new SXSSFWorkbook();

        // 如需生成xls的Excel,请使用下面的工作簿对象,注意后续输出时文件后缀名也需更改为xls
        //Workbook workbook = new HSSFWorkbook();

        // 生成Sheet表,写入第一行的列头
        Sheet sheet = buildDataSheet(workbook);
        //构建每行的数据内容
        int rowNum = 1;
        for (Iterator it = dataList.iterator(); it.hasNext(); ) {
            DataVo data = it.next();
            if (data == null) {
                continue;
            }
            //输出行数据
            Row row = sheet.createRow(rowNum++);
            convertDataToRow(data, row);
        }
        return workbook;
    }

    /**
     * 生成sheet表,并写入第一行数据(列头)
     *
     * @param workbook 工作簿对象
     * @return 已经写入列头的Sheet
     */
    private static Sheet buildDataSheet(Workbook workbook) {
        Sheet sheet = workbook.createSheet();
        // 设置列头宽度
        for (int i = 0; i ) {
            sheet.setColumnWidth(i, 4000);
        }
        // 设置默认行高
        sheet.setDefaultRowHeight((short) 400);
        // 构建头单元格样式
        CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook());
        // 写入第一行各列的数据
        Row head = sheet.createRow(0);
        for (int i = 0; i ) {
            Cell cell = head.createCell(i);
            cell.setCellValue(CELL_HEADS.get(i));
            cell.setCellStyle(cellStyle);
        }
        return sheet;
    }

    /**
     * 设置第一行列头的样式
     *
     * @param workbook 工作簿对象
     * @return 单元格样式对象
     */
    private static CellStyle buildHeadCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        //对齐方式设置
        style.setAlignment(HorizontalAlignment.CENTER);
        //边框颜色和宽度设置
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下边框
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左边框
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右边框
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边框
        //设置背景颜色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //粗体字设置
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }

    /**
     * 将数据转换成行
     *
     * @param data 源数据
     * @param row  行对象
     * @return
     */
    private static void convertDataToRow(DataVo data, Row row) {
        int cellNum = 0;
        Cell cell;
        // 姓名
        cell = row.createCell(cellNum++);
        cell.setCellValue(null == data.getName() ? "" : data.getName());
        // 年龄
        cell = row.createCell(cellNum++);
        if (null != data.getAge()) {
            cell.setCellValue(data.getAge());
        } else {
            cell.setCellValue("");
        }
    }
}

Controller

package com.springbootemaildemo.excel.b;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/excel/b")
@Api("导出Excel")
public class ExcelFileController {
    private static final Logger logger =LoggerFactory.getLogger(ExcelFileController.class);
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        Workbook workbook = null;
        OutputStream out = null;
        try {
            // todo 根据业务需求获取需要写入Excel的数据列表 dataList
            List dataList = new ArrayList();
            //把数据加载到dataList中即可
            // 生成Excel工作簿对象并写入数据
            workbook = ExcelWriter.exportData(dataList);
            // 写入Excel文件到前端
            if (null != workbook) {
                String excelName = "export-01";
                String fileName = excelName + System.currentTimeMillis() + ".xlsx";
                fileName = new String(fileName.getBytes("UTF-8"), "iso8859-1");
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                response.setContentType("application/x-download");
                response.setCharacterEncoding("UTF-8");
                response.addHeader("Pargam", "no-cache");
                response.addHeader("Cache-Control", "no-cache");
                response.flushBuffer();
                out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            }
        } catch (Exception e) {
            logger.warn("写入Excel过程出错!错误原因:" + e.getMessage());
        } finally {
            try {
                if (null != workbook) {
                    workbook.close();
                }
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                logger.warn("关闭workbook或outputStream出错!");
            }
        }
    }
}

 

Apache POI 4.0.1版本写入普通Excel文件(兼容 xls 和 xlsx)(四)

标签:load   static   ota   framework   boot   方式   http   cache   writer   

原文地址:https://www.cnblogs.com/weigy/p/12907987.html


评论


亲,登录后才可以留言!