6:基于Excel的数据展现(生成报表)#私藏项目实操分享#

2021年11月24日 阅读数:4
这篇文章主要向大家介绍6:基于Excel的数据展现(生成报表)#私藏项目实操分享#,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

简介

采用阿里的Easy-Excel生成报表html

添加依赖

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>3.0.2</version>
    </dependency>

Servlet

@WebServlet(urlPatterns = "/goods")
public class GoodsServlet extends HttpServlet {
    private static final long serialVersionUID = 4116681024313896922L;

    private GoodsService goodsService = new GoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        final String op = request.getParameter("op");
        switch (op) {
            case "genExcel":
                try {
                    genExcel();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    private void genExcel() throws SQLException {
        final List<Goods> arrayList = goodsService.listAllGoods();

        String fileName = "D:/demo" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName, Goods.class).sheet("商品信息").doWrite(arrayList);
    }

}

前端页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>生成报表</title>
</head>
<body>
<a rel="nofollow" href="goods?op=genExcel">生成Excel报表</a>
</body>
</html>

当用户单击页面中的超链接时,会在D盘生成Excel文件:
在这里插入图片描述前端