Excel导出--POI

参考链接:

https://blog.csdn.net/weixin_38842707/article/details/80638819

https://blog.csdn.net/zhufei463738313/article/details/77426499

目录             ​

POI简介:

基本功能

HSSFWorkbook和XSSFWorkbook  

POI EXCEL文档结构类:

一个简单的demo:

效果呈现: 

代码的实现:

总结

 

          

POI简介:

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。

基本功能

HSSF - 提供读写Microsoft Excel格式档案的功能。

XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。

HWPF - 提供读写Microsoft Word格式档案的功能。

HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

HDGF - 提供读写Microsoft Visio格式档案的功能。

HSSFWorkbook和XSSFWorkbook  

POI 提供了对2003版本的Excel的支持 ---- HSSFWorkbook

            POI 提供了对2007版本以及更高版本的支持 ---- XSSFWorkbook

 POI EXCEL文档结构类

            HSSFWorkbook excel -> 文档对象

            HSSFSheet -> excel的sheet

            HSSFRow -> excel的行

            HSSFCell -> excel的单元格

            HSSFFont -> excel字体

            HSSFCellStyle cell -> 样式

            HSSFName -> 名称

            HSSFDataFormat -> 日期格式

            HSSFHeader sheet -> 头

            HSSFFooter sheet -> 尾

            HSSFDateUtil -> 日期

            HSSFPrintSetup -> 打印

            HSSFErrorConstants -> 错误信息表

一个简单的demo:

效果呈现:

代码的实现:

  1. 创建一个spring boot项目
  2. 添加poipoi-ooxmlpoi-ooxml-schemas依赖(版本,我是参照别的作者用3.15,读者可以大胆尝试其他版本试试)
  3. 新建一个类,运行可以在自己设定的路径下看到导出的excel文件
package com.demo.demo;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileOutputStream;

public class ExportDemo {
    public void Test(){
        /**
         * 开发步骤
         * 1.生成一个工作簿,并创建一个sheet
         * 2.设置这个sheet的指定列的宽度
         * 3.设置字体的样式
         * 4.设置单元格的样式
         * 5.插入对应的元素:主标题,列标题,数据
         * 6.设置页脚
         * 7.设置文件的导出
         */
        //--------------生成excel----------------
        XSSFWorkbook workbook = new XSSFWorkbook();//创建出一个工作簿
        XSSFSheet sheet = workbook.createSheet();//创建一个sheet

        for (int i=0; i<5;i++){//表中设置5列
            int setColumnWidth = 4500;//每一列的宽度为4500
            sheet.setColumnWidth(i,setColumnWidth);
        }
        //--------------设置主标题字体样式----------------
        XSSFFont font = workbook.createFont();//设置字体大小
        font.setBold(true);//加粗
        font.setColor(HSSFColor.RED.index);
        font.setCharSet(1);
        font.setFontHeight(30);
        //--------------设置主单元格的样式-----------
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderTop(BorderStyle.THICK);//上下左右边框的样式
        cellStyle.setBorderBottom(BorderStyle.THICK);
        cellStyle.setBorderLeft(BorderStyle.THICK);
        cellStyle.setBorderRight(BorderStyle.THICK);
        cellStyle.setTopBorderColor(HSSFColor.BLACK.index);//以及边框的颜色
        cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
        cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
        cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
        cellStyle.setFont(font);//引入字体样式
        //--------------插入主标题------------------
        sheet.addMergedRegion( new CellRangeAddress( (int)0, (int)2, (int)0, (int)4 ) );
       //firtlow,lastlow,firstcol,lastcol 得到该位置的所有区域 一般使用short类型,这里尝试用int玩一下
        Row row;//主标题的行
        Cell cell;//主标题的列
        for (int i=0; i<3; i++){
            row = sheet.createRow(i);//通过表来创建行,在0-2行之间插入主标题
            for (int j=0; j<5; j++){//主标题占的列数
                cell = row.createCell(j);//通过行来创建列,5列
                cell.setCellType(CellType.STRING);
                cell.setCellStyle(cellStyle);
                cell.setCellValue("***这是一个主标题***");
            }
        }
        //--------------设置列标题字体样式----------------
        XSSFFont font1 = workbook.createFont();//设置字体大小
        font1.setBold(true);//加粗
        font1.setColor(HSSFColor.GREY_80_PERCENT.index);
        font1.setFontHeight(15);
        //--------------设置列标题单元格的样式-----------
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setBorderTop(BorderStyle.NONE);//上下左右边框的样式
        cellStyle1.setBorderBottom(BorderStyle.THICK);
        cellStyle1.setBorderLeft(BorderStyle.THICK);
        cellStyle1.setBorderRight(BorderStyle.THICK);
        cellStyle1.setTopBorderColor(HSSFColor.BLUE.index);//以及边框的颜色
        cellStyle1.setBottomBorderColor(HSSFColor.BLUE.index);
        cellStyle1.setLeftBorderColor(HSSFColor.BLUE.index);
        cellStyle1.setRightBorderColor(HSSFColor.BLUE.index);
        cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        cellStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
        cellStyle1.setFont(font1);//引入字体样式
        //--------------插入列标题------------------
        Row rowTitle;
        Cell cellTitle;
        for (int i=3; i<4; i++){//在第3行插入列标题
            rowTitle = sheet.createRow(i);
            for (int j = 0; j<5; j++){
                cellTitle = rowTitle.createCell(j);
                cellTitle.setCellType(CellType.STRING);
                cellTitle.setCellStyle(cellStyle1);
                cellTitle.setCellValue("*这是列标题*");
            }
        }
        //--------------设置列标题字体样式----------------
        XSSFFont font2 = workbook.createFont();//设置字体大小
        font2.setBold(false);//加粗
        font2.setColor(HSSFColor.BLACK.index);
        font2.setCharSet(1);
        font2.setFontHeight(10);
        //--------------设置列标题单元格的样式-----------
        CellStyle cellStyle2 = workbook.createCellStyle();
        cellStyle2.setBorderTop(BorderStyle.THIN);//上下左右边框的样式
        cellStyle2.setBorderBottom(BorderStyle.THIN);
        cellStyle2.setBorderLeft(BorderStyle.THIN);
        cellStyle2.setBorderRight(BorderStyle.THIN);
        cellStyle2.setTopBorderColor(HSSFColor.GREEN.index);//以及边框的颜色
        cellStyle2.setBottomBorderColor(HSSFColor.GREEN.index);
        cellStyle2.setLeftBorderColor(HSSFColor.GREEN.index);
        cellStyle2.setRightBorderColor(HSSFColor.GREEN.index);
        cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
        cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 上下居中
        cellStyle2.setFont(font2);//引入字体样式
        //---------------插入数据--------------------
        Row insertRow;
        Cell insertCell;
        for (int i=4; i<1000; i++){
            insertRow = sheet.createRow(i);
            for (int j=0; j<5; j++){
                insertCell = insertRow.createCell(j);
                insertCell.setCellStyle(cellStyle2);
                insertCell.setCellValue("数据");
            }
        }
        //---------------设置页脚--------------------
        workbook.setSheetName(0,"页脚一");
        //---------------设置导出--------------------
        File file = new File("P:\\document\\excel\\test.xlsx");//将数据导出到电脑的指定位置
        try {//捕获异常
            FileOutputStream fileOutputStream = new FileOutputStream(file);//创建文件读入
            workbook.write(fileOutputStream);//将excel写进excel
            fileOutputStream.close();//释放
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
    }

    public static void main(String[] args) {
        new ExportDemo().Test();//调用导出excel方法
    }
}

总结

简单的实现了将数据导出成excel表格的简单呈现。

熟悉了整个导出流程,了解一些里面的API。

接下来就是解决一些项目中会遇到的问题

  1. 当需要导出数据较大时,会发生OOM。
  2. 当需要导出数据较大时,时间问题。
  3. 导出成其他形式,txt,xml等文件格式
  4. 将数据导入

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用CSDN开发的poi-tl库来导出Excel文件。poi-tl是一个基于Apache POI的Java模板引擎,它可以帮助你通过填充模板数据来生成Excel文件。 首先,你需要引入poi-tl库的依赖。你可以在你的项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-tl</artifactId> <version>1.9.0</version> </dependency> ``` 然后,你可以按照以下步骤使用poi-tl导出Excel文件: 1. 创建一个Excel模板文件,可以使用Microsoft Excel或Apache POI创建一个带有占位符的模板文件。占位符可以是任意字符,用于标记需要填充的数据位置。 2. 在Java代码中,使用poi-tl读取Excel模板并进行数据填充。下面是一个简单的示例: ```java import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.InputStream; public class ExcelExportExample { public static void main(String[] args) { try (InputStream template = ExcelExportExample.class.getResourceAsStream("template.xlsx"); FileOutputStream outputStream = new FileOutputStream("output.xlsx")) { Workbook workbook = WorkbookFactory.create(template); // 填充数据 workbook.getSheetAt(0).getRow(1).getCell(0).setCellValue("John Doe"); workbook.getSheetAt(0).getRow(1).getCell(1).setCellValue(25); // 保存为新的Excel文件 workbook.write(outputStream); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们从模板文件"template.xlsx"中读取Excel模板,并在第一个工作表的第二行填充了一些数据。然后,我们将填充后的工作簿保存为"output.xlsx"文件。 这只是poi-tl库的基本用法,你可以根据自己的需求进行更复杂的操作。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值