SpringBoot2实现写Excel文件

SpringBoot2实现写Excel文件

本文主要记录基于SpringBoot2实现通过POI写Excel文件的操作。

Maven依赖

<!--office文档依赖-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16-beta1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

POI工具类

package com.ictpaas.consumerservice.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.Region;

import java.io.*;
import java.util.ArrayList;
import java.util.Map;

public class POIUtil {

    public static boolean writeIntoExcel(String filePath, String tableTitle, ArrayList<String> tableHeader, Map<String, Object> rowData) {
        // 创建文件
        File file = new File(filePath);
        // 判断目标文件所在的目录是否存在,不在则创建
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                System.out.println("文件所在目录不存在,创建失败");
                return false;
            }
        }
        // 创建工作簿
        HSSFWorkbook wb;
        // 创建表对象
        HSSFSheet sheet;
        // 创建行
        HSSFRow row;
        // 输入输出流
        FileInputStream fis = null;
        OutputStream out = null;

        try {
            if (file.isFile()) {
                // 存在,则直接追加数据
                fis = new FileInputStream(file);
                POIFSFileSystem ps = new POIFSFileSystem(fis);
                // 1.获取excel的book对象
                wb = new HSSFWorkbook(ps);
                // 2.获取sheet
                sheet = wb.getSheetAt(0);
                // 3.获取插入数据的行
                row = sheet.createRow((short)(sheet.getLastRowNum() + 1));
            } else {
                // 不存在,则新建excel
                // 1.获取excel的book对象
                wb = new HSSFWorkbook();
                // 2.创建sheet
                sheet = wb.createSheet(tableTitle);
                // 居中样式
                HSSFCellStyle style = wb.createCellStyle();
                style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
                // 字体样式
                HSSFFont font = wb.createFont();
                font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
                font.setItalic(true);
                font.setFontHeight((short)200);
                font.setColor(HSSFFont.COLOR_NORMAL);
                style.setFont(font);
                // 3.创建第一行,作为标题
                HSSFRow row0 = sheet.createRow(0);
                sheet.addMergedRegion(new Region(0, (short)0, 0, (short)tableHeader.size()));
                HSSFCell row0cell0 = row0.createCell(0);
                row0cell0.setCellValue(tableTitle);
                // 4.创建第二行,作为表头
                HSSFRow row1 = sheet.createRow(1);
                for (int i=0; i<tableHeader.size(); ++i) {
                    HSSFCell row1cell = row1.createCell(i);
                    row1cell.setCellValue(tableHeader.get(i));
                    row1cell.setCellStyle(style);
                }
                // 5.创建第三行,用于插入数据
                row = sheet.createRow(2);
            }

            // 创建数据行,插入数据
            for (int i=0; i<tableHeader.size(); ++i) {
                row.createCell(i).setCellValue((String) rowData.get(tableHeader.get(i)));
            }

            // 写文件
            out = new FileOutputStream(file);
            out.flush();
            wb.write(out);

            return true;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

            if(fis != null){
                try {
                    fis.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值