之前我们也写了一个Excel的简单导出,甚至可以不依赖poi,还扩展了纯前端导出Excel!详情请戳:《POI导出Excel》,遗憾的是这些导出并不支持复杂表头
dependency>
groupId>org.apache.poigroupId>
artifactId>poiartifactId>
version>5.2.3version>
dependency>
dependency>
groupId>org.apache.poigroupId>
artifactId>poi-ooxmlartifactId>
version>5.2.3version>
dependency>
dependency>
groupId>cn.hutoolgroupId>
artifactId>hutool-allartifactId>
version>5.7.4version>
dependency>
title 标题
key key
width 宽度
align 对齐方式
background-color 背景颜色(POI的IndexedColors)
color 字体颜色(POI的IndexedColors)
children 子级表头
//获取HExcel实例
HExcel hExcel1 = HExcel.newInstance();
//数据,一般是查数据库,经过数据处理生成
List
//需要设置title与key的关系
JSONObject headerTitleKey = new JSONObject("" +
"{n" +
" "姓名":"user_name",n" +
" "语文":"yu_wen",n" +
" "数学":"shu_xue",n" +
" "总分":"total",n" +
"}" +
"");
//根据Excel文件,获取HExcel实例
HExcel hExcel2 = HExcel.newInstance(new File("C:UsersXFT UserDesktop学生成绩单复杂表头导出测试.xls"));
//根据title-key关系,读取指定位置的sheet数据
List
package cn.huanzi.qch.util;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
/**
* HExcel,一个简单通用的导入导出Excel工具类
* 1、支持复杂表头导出(支持表头单元格水平合并、垂直合并,支持表头单元格个性化样式)
* 2、支持导入读取sheet数据(只需要提供title与key的关系,不需要管列的顺序)
*
* PS:依赖 poi 以及 hutool
*
* 详情请戳:https://www.cnblogs.com/huanzi-qch/p/17797355.html
*/
public class HExcel {
/**
* 获取一个HExcel实例,并初始化空Workbook对象
*/
public static HExcel newInstance(){
HExcel hExcelUtil = new HExcel();
hExcelUtil.hSSFWorkbook = new HSSFWorkbook();
return hExcelUtil;
}
/**
* 获取一个HExcel实例,并根据excelFile初始化Workbook对象
*/
public static HExcel newInstance(File excelFile){
HExcel hExcelUtil = new HExcel();
try {
hExcelUtil.hSSFWorkbook = new HSSFWorkbook(new FileInputStream(excelFile));
} catch (IOException e) {
throw new RuntimeException("【HExcel】 根据excelFile初始化Workbook对象异常",e);
}
return hExcelUtil;
}
/**
* 导入并读取Excel
*
* @param sheetIndex 需要读取的sheet下标
* @param firstDataRow 数据起始行
* @param headerTitleKey title与key的关系json对象
* @return 返回数据集合
*/
public List
View Code
public static void main(String[] args) {
//获取HExcel实例
HExcel hExcel1 = HExcel.newInstance();
//数据,一般是查数据库,经过数据处理生成
List> dataList = new ArrayList();
HashMap date1 = new HashMap();
date1.put("user_name","张三");
date1.put("sex","男");
date1.put("age",20);
date1.put("yu_wen",90);
date1.put("ying_yu",0);
date1.put("shu_xue",85);
date1.put("wu_li",80);
date1.put("total",255);
dataList.add(date1);
HashMap date2 = new HashMap();
date2.put("user_name","李四");
date2.put("sex","女");
date2.put("age",18);
date2.put("yu_wen",81);
date2.put("ying_yu",0);
date2.put("shu_xue",90);
date2.put("wu_li",70);
date2.put("total",241);
dataList.add(date2);
//如果是固定表头数据,可以在项目资源文件夹下面新建个json文件夹,用来保存表头json数据,方便读、写
//JSONArray header = JSONUtil.parseArray(ResourceUtil.readUtf8Str("json/header.json"));
//如果是动态表头数据,直接把json字符串写在代码里,方便动态生成表头数据
//表头
String sheetName = "学生成绩单";
JSONArray headers = JSONUtil.parseArray("" +
"[n" +
" {n" +
" "title":""+sheetName+"",n" +
" "children":[n" +
" {n" +
" "title":"日期:"+DateUtil.today()+"",n" +
" "align":"right",n" +
" "children":[n" +
" {n" +
" "title":"姓名",n" +
" "key":"user_name",n" +
" },n" +
" {n" +
" "title":"语文",n" +
" "key":"yu_wen",n" +
" },n" +
" {n" +
" "title":"数学",n" +
" "key":"shu_xue",n" +
" },n" +
" {n" +
" "title":"总分",n" +
" "key":"total",n" +
" "background-color":17,n" +
" "color":10,n" +
" "width":30,n" +
" },n" +
" ]n" +
" },n" +
" ]n" +
" },n" +
"]" +
"");
//生成sheet
hExcel1.buildSheet(sheetName, headers, dataList);
//表头
JSONArray headers2 = JSONUtil.parseArray("" +
"[n" +
" {n" +
" "title":"姓名",n" +
" "key":"user_name",n" +
" },n" +
" {n" +
" "title":"学科成绩",n" +
" "children":[n" +
" {n" +
" "title":"语文",n" +
" "key":"yu_wen",n" +
" },n" +
" {n" +
" "title":"数学",n" +
" "key":"shu_xue",n" +
" },n" +
" ]n" +
" },n" +
" {n" +
" "title":"总分",n" +
" "key":"total",n" +
" "align":"right",n" +
" "background-color":17,n" +
" "color":10,n" +
" "width":30n," +
" },n" +
"]" +
"");
//生成sheet
hExcel1.buildSheet("学生成绩单2", headers2, dataList);
//表头
JSONArray headers3 = JSONUtil.parseArray("" +
"[n" +
" {n" +
" "title":"姓名",n" +
" "key":"user_name"n" +
" },n" +
" {n" +
" "title":"性别",n" +
" "key":"sex"n" +
" },n" +
" {n" +
" "title":"年龄",n" +
" "key":"age"n" +
" },n" +
" {n" +
" "title":"学科成绩",n" +
" "children":[n" +
" {n" +
" "title":"语言类",n" +
" "children":[n" +
" {n" +
" "title":"语文",n" +
" "key":"yu_wen",n" +
" "background-color":7,n" +
" "color":5,n" +
" },n" +
" ]n" +
" },n" +
" {n" +
" "title":"科学类",n" +
" "background-color":10,n" +
" "children":[n" +
" {n" +
" "title":"数学",n" +
" "key":"shu_xue"n" +
" },n" +
" {n" +
" "title":"物理",n" +
" "key":"wu_li"n" +
" }n" +
" ]n" +
" },n" +
" ]n" +
" },n" +
" {n" +
" "title":"总分",n" +
" "key":"total",n" +
" "align":"right",n" +
" "background-color":17,n" +
" "color":10,n" +
" "width":30n," +
" },n" +
"]"+
"");
//生成sheet
hExcel1.buildSheet("学生成绩单3", headers3, dataList);
//表头
JSONArray headers4 = JSONUtil.parseArray("" +
"[n" +
" {n" +
" "title":"姓名",n" +
" "key":"user_name"n" +
" },n" +
" {n" +
" "title":"性别",n" +
" "key":"sex"n" +
" },n" +
" {n" +
" "title":"年龄",n" +
" "key":"age"n" +
" },n" +
" {n" +
" "title":"学科成绩",n" +
" "children":[n" +
" {n" +
" "title":"语文",n" +
" "key":"yu_wen",n" +
" },n" +
" {n" +
" "title":"科学类",n" +
" "background-color":10,n" +
" "children":[n" +
" {n" +
" "title":"数学",n" +
" "key":"shu_xue"n" +
" },n" +
" {n" +
" "title":"物理",n" +
" "key":"wu_li"n" +
" }n" +
" ]n" +
" },n" +
" {n" +
" "title":"英语",n" +
" "key":"ying_yu",n" +
" },n" +
" ]n" +
" },n" +
" {n" +
" "title":"总分",n" +
" "key":"total",n" +
" "align":"right",n" +
" "background-color":17,n" +
" "color":10,n" +
" "width":30n" +
" n" +
" }n" +
"]"+
"");
//生成sheet
hExcel1.buildSheet("学生成绩单4", headers4, dataList);
//保存成File文件
hExcel1.toFile("C:UsersXFT UserDesktop学生成绩单复杂表头导出测试.xls");
System.out.println("导出完成!n");
//关闭对象
hExcel1.close();
//导入
//需要设置title与key的关系
JSONObject headerTitleKey = new JSONObject("" +
"{n" +
" "姓名":"user_name",n" +
" "语文":"yu_wen",n" +
" "数学":"shu_xue",n" +
" "总分":"total",n" +
"}" +
"");
//根据Excel文件,获取HExcel实例
HExcel hExcel2 = HExcel.newInstance(new File("C:UsersXFT UserDesktop学生成绩单复杂表头导出测试.xls"));
//根据title-key关系,读取指定位置的sheet数据
List> sheetList = hExcel2.readSheet(2, 3, headerTitleKey);
//打印sheetList数据
System.out.println("导入完成!");
for (Map map : sheetList) {
System.out.println(map.toString());
}
//关闭对象
hExcel2.close();
}