参考链接:
https://blog.youkuaiyun.com/weixin_38842707/article/details/80638819
https://blog.youkuaiyun.com/zhufei463738313/article/details/77426499
目录
POI EXCEL文档结构类:
总结
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:
效果呈现:
代码的实现:
- 创建一个spring boot项目
- 添加poi,poi-ooxml,poi-ooxml-schemas依赖(版本,我是参照别的作者用3.15,读者可以大胆尝试其他版本试试)
- 新建一个类,运行可以在自己设定的路径下看到导出的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。
接下来就是解决一些项目中会遇到的问题
- 当需要导出数据较大时,会发生OOM。
- 当需要导出数据较大时,时间问题。
- 导出成其他形式,txt,xml等文件格式
- 将数据导入