自定义excel通过poi导出下载

news/2024/7/21 5:44:18 标签: excel

1:配置pom下的依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.23</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>9.0.68</version>
        </dependency>

2:编写请求下载类

package com.example.poi;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xu
 * @create 2023/7/5 01
 */

@RequestMapping("/custom")
public class Demo {
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) {

        List<Map<String, Object>> studentList = new ArrayList<>();
        Map<String, Object> student1 = new HashMap<>();
        student1.put("姓名", "张三");
        student1.put("年龄", 18);
        student1.put("成绩", 99);

        Map<String, Object> student2 = new HashMap<>();
        student2.put("姓名", "李四");
        student2.put("年龄", 20);
        student2.put("成绩", 100);

        studentList.add(student1);
        studentList.add(student2);

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("学生信息");


        // 创建标题行
        Row titleRow = sheet.createRow(0);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("这是标题");

        // 合并标题行的单元格,重点这里的参数,可以选择合并的情况,然后再设置字体样式都可以
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 2));

        // 设置标题样式
        CellStyle titleStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        titleStyle.setFont(font);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        titleCell.setCellStyle(titleStyle);

        // 写入表头
        Row headerRow = sheet.createRow(1);
        int columnIndex = 0;
        for (String key : studentList.get(0).keySet()) {
            Cell cell = headerRow.createCell(columnIndex++);
            cell.setCellValue(key);
        }

        // 写入数据
        int rowIndex = 1;
        for (Map<String, Object> student : studentList) {
            Row dataRow = sheet.createRow(rowIndex++);
            columnIndex = 0;
            for (Object value : student.values()) {
                Cell cell = dataRow.createCell(columnIndex++);
                cell.setCellValue(value.toString());
            }
        }

        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("学生信息表12.xlsx", "UTF-8"));
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (
                IOException e) {
            e.printStackTrace();
        }
    }
}


http://www.niftyadmin.cn/n/1728938.html

相关文章

numpy中视图和副本的区别

副本(深拷贝)是一个数据的完整的拷贝&#xff0c;如果我们对其进行修改&#xff0c;它不会影响到原始数据&#xff0c;物理内存(id( ))不在同一位置。 实现方式&#xff1a; Python 序列的切片操作;Python调用copy.deepCopy()函数(详见上一篇博客)&#xff1b;调用 ndarray 的…

numpy中的转置Transpose和.T以及轴对换swapaxis

numpy中Transpose和.T以及swapaxis都能对ndarray在不同维度上进行转置&#xff0c;交换操作&#xff0c;下面分别介绍这三个函数的用法&#xff0c;相互对比以加深影响。 1.Transpose 这个函数如果括号内不带参数&#xff0c;和.T效果一样,见下面例子&#xff1a; import nu…

numpy中flatten()和ravel()

在numpy包中&#xff0c;flatten()和ravel()函数都能将多维数组降为一维&#xff0c;区别在于numpy.flatten()返回是拷贝&#xff0c;对拷贝所做的修改不会影响原始矩阵&#xff0c;而numpy.ravel()返回的是视图,修改视图&#xff0c;原始矩阵也会受到影响&#xff0c;详见前面…

Python文件IO

参考文章&#xff1a; https://www.cnblogs.com/ymjyqsx/p/6554817.html https://blog.csdn.net/sxingming/article/details/52164249 https://blog.csdn.net/lucyxu107/article/details/82728266 更详细信息见python标准库&#xff1a; https://docs.python.org/zh-cn/3.7/lib…

Linux中硬连接(hard link)与软连接(symbolic link)

参考文章&#xff1a; https://blog.csdn.net/gxzc936733992/article/details/49340429 https://www.cnblogs.com/jackhub/p/3779917.html https://blog.csdn.net/bitboss/article/details/53940236 在Linux系统&#xff08;Ubuntu&#xff09;的ext4文件系统中&#xff0c;根据…

python中shutil模块用法介绍

参考文章地址&#xff1a; https://segmentfault.com/a/1190000016689023 文章翻译了python标准库中shutil的介绍&#xff1a; https://docs.python.org/zh-cn/3.7/library/shutil.html shutil模块提供了一些针对文件和目录的高级操作&#xff0c;主要是拷贝、移动。对于单个文…

Markdown写作常用公式总结

参见&#xff1a; https://blog.csdn.net/silver1225/article/details/89036250 https://blog.csdn.net/u010416101/article/details/54633268 1.数学公式变红 居中&#xff08;单独占一行&#xff09; <font color"red">$$公式内容$$</font>公式内容公…

矩阵的 SVD 分解方法,几何意义

转自&#xff1a; https://liam.page/2017/11/22/SVD-for-Human-Beings/ 更多信息请读者移步原文阅读。 推荐中国台湾周志成老师的线性代数博客 https://ccjou.wordpress.com/ 以及书籍《矩阵分析及应用》-- 张贤达 还可参考&#xff1a; https://www.cnblogs.com/endlesscodin…