Spring Boot导出EXCEL 文件

主要功能:实现java导出excel到本地

JDK版本:openJDK 20.0.1

依赖pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.joker</groupId>
    <artifactId>export</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>export</name>
    <description>export</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

主要核心代码

java 复制代码
package com.joker.export.Controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.Iterator;
import java.util.List;

import java.util.Map;

@RestController
public class ExportExcelController {

    @RequestMapping("/")
    public String home(){
        System.out.println("hello world");
        return "Hello World for Spring Boot by Joker!";
    }
    @RequestMapping("/Export2Excel")
    public String Export2Excel() throws IOException {
        // you should get the filename from the input data
        String filename = "ExportDataByJoker"  +".xlsx";


        // Parse the JSON string into a JSON object.
        String jsonData = "[{\"ZHC_ZZ\":\"20001118\",\"ZHR_ID_JD\":\"20001118\",\"ZHC_ZZ_DESC\":\"\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":0,\"ZHC_PLAN_AVAILABLE_AC\":0,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":0,\"ZHC_PLAN_AVAILABLE_AD\":0,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"\"},{\"ZHC_ZZ\":\"20001342\",\"ZHR_ID_JD\":\"20001342\",\"ZHC_ZZ_DESC\":\"测试组织20230915\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":0,\"ZHC_PLAN_AVAILABLE_AC\":0,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":0,\"ZHC_PLAN_AVAILABLE_AD\":0,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/测试组织20230915\"},{\"ZHC_ZZ\":\"10000127\",\"ZHR_ID_JD\":\"01010603\",\"ZHC_ZZ_DESC\":\"长沙区\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":374,\"ZHC_PLAN_AVAILABLE_AC\":-374,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":2,\"ZHC_PLAN_AVAILABLE_AD\":-372,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/长沙区\"},{\"ZHC_ZZ\":\"10006019\",\"ZHR_ID_JD\":\"0154\",\"ZHC_ZZ_DESC\":\"南昌区\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":2,\"ZHC_PLAN_AVAILABLE_AC\":-2,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":1,\"ZHC_PLAN_AVAILABLE_AD\":-1,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/南昌区\"},{\"ZHC_ZZ\":\"10000009\",\"ZHR_ID_JD\":\"01010602\",\"ZHC_ZZ_DESC\":\"武汉区\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":333,\"ZHC_PLAN_AVAILABLE_AC\":-333,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":3,\"ZHC_PLAN_AVAILABLE_AD\":-330,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/武汉区\"},{\"ZHC_ZZ\":\"20000365\",\"ZHR_ID_JD\":\"20000365\",\"ZHC_ZZ_DESC\":\"电商部\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":36,\"ZHC_PLAN_AVAILABLE_AC\":-36,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":0,\"ZHC_PLAN_AVAILABLE_AD\":-36,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/电商部\"},{\"ZHC_ZZ\":\"20000366\",\"ZHR_ID_JD\":\"20000366\",\"ZHC_ZZ_DESC\":\"业务支持部\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":28,\"ZHC_PLAN_AVAILABLE_AC\":-28,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":1,\"ZHC_PLAN_AVAILABLE_AD\":-27,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/业务支持部\"},{\"ZHC_ZZ\":\"20000367\",\"ZHR_ID_JD\":\"20000367\",\"ZHC_ZZ_DESC\":\"市场品牌部\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":15,\"ZHC_PLAN_AVAILABLE_AC\":-15,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":0,\"ZHC_PLAN_AVAILABLE_AD\":-15,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/市场品牌部\"},{\"ZHC_ZZ\":\"20000559\",\"ZHR_ID_JD\":\"20000559\",\"ZHC_ZZ_DESC\":\"成本中心测试\",\"ZHC_ZZ_LEVEL\":\"21\",\"ZHC_ZZ_LEVEL_DESC\":\"二级部门\",\"ZHC_PLAN\":0,\"ZHC_PLAN_ACTUAL\":0,\"ZHC_PLAN_AVAILABLE_AC\":0,\"ZHC_PLAN_RATE\":0.0000,\"ZHC_PLAN_RATE_CHAR\":\"\",\"ZHC_PLAN_LOCK\":0,\"ZHC_PLAN_RELEASE\":0,\"ZHC_PLAN_AVAILABLE_AD\":0,\"ZHC_FREEZE\":\"\",\"ZORG_FULLNAME\":\"华中区\\/成本中心测试\"}]";

        // Convert JSON to a list of maps
        ObjectMapper mapper = new ObjectMapper();
        List<Map<String, Object>> dataList = mapper.readValue(jsonData, new TypeReference<List<Map<String, Object>>>() {});

        // Create an Excel workbook
        Workbook workbook = new XSSFWorkbook();

        // Create a sheet
        Sheet sheet = workbook.createSheet("Sheet1");

        // Create a header row
        Row headerRow = sheet.createRow(0);

        // Get the column names from the first map entry,
        // get the column names from the input data is better
        Map<String, Object> firstData = dataList.get(0);
        Iterator<Map.Entry<String, Object>> iterator = firstData.entrySet().iterator();
        int columnCount = 0;
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String columnName = entry.getKey();
            headerRow.createCell(columnCount).setCellValue(columnName);
            columnCount++;
        }

        // Create data rows
        int rowIndex = 1;
        for (Map<String, Object> data : dataList) {
            Row dataRow = sheet.createRow(rowIndex);
            int dataColumnCount = 0;
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                Object value = entry.getValue();
                Cell cell = dataRow.createCell(dataColumnCount);
                if (value instanceof String) {
                    cell.setCellValue((String) value);
                } else if (value instanceof Number) {
                    cell.setCellValue(((Number) value).doubleValue());
                } else {
                    cell.setCellValue(value.toString());
                }
                dataColumnCount++;
            }
            rowIndex++;
        }

        // Auto-size the columns
        for (int i = 0; i < columnCount; i++) {
            sheet.autoSizeColumn(i);
        }

        // Write the workbook to a file
        try (FileOutputStream out = new FileOutputStream(filename)) {
            workbook.write(out);
        }

        // change the return to file stream
        return "download file is ready";

    }
}

执行效果:

相关推荐
小码哥_常1 天前
Spring Boot 中JWT登录授权+无感刷新,看这篇就够了!
后端
李昊哲小课1 天前
Python办公自动化教程 - 第7章 综合实战案例 - 企业销售管理系统
开发语言·python·数据分析·excel·数据可视化·openpyxl
码农BookSea1 天前
深度解析Skills:从Prompt到能力复用的技术革命
后端·ai编程
计算机毕设指导61 天前
基于SpringBoot校园学生健康监测管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea
mysuking1 天前
springboot与springcloud对应版本
java·spring boot·spring cloud
希望永不加班1 天前
SpringBoot 数据库连接池配置(HikariCP)最佳实践
java·数据库·spring boot·后端·spring
夕颜1111 天前
写 SIP 服务后台前,先把 SIP 和 PSTN 搞清楚
后端
码农BookSea1 天前
为什么ChatGPT能听懂你说的话?Embedding技术揭秘
后端·openai
黑牛儿1 天前
MySQL 索引实战详解:从创建到优化,彻底解决查询慢问题
服务器·数据库·后端·mysql
程序员飞哥1 天前
到底Java 适不适合做 AI 呢?
后端·程序员·全栈