|
|
|
@ -1,82 +1,83 @@
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
|
|
|
|
|
*/
|
|
|
|
|
package com.yf.exam.core.utils.excel;
|
|
|
|
|
package com.yf.exam.core.utils.excel; // 包声明
|
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import com.yf.exam.core.utils.Reflections;
|
|
|
|
|
import com.yf.exam.core.utils.excel.annotation.ExcelField;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
|
|
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Comment;
|
|
|
|
|
import org.apache.poi.ss.usermodel.DataFormat;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Font;
|
|
|
|
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import com.google.common.collect.Lists; // 导入Lists类
|
|
|
|
|
import com.yf.exam.core.utils.Reflections; // 导入Reflections工具类
|
|
|
|
|
import com.yf.exam.core.utils.excel.annotation.ExcelField; // 导入ExcelField注解
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; // 导入StringUtils工具类
|
|
|
|
|
import org.apache.poi.ss.usermodel.Cell; // 导入Cell类
|
|
|
|
|
import org.apache.poi.ss.usermodel.CellStyle; // 导入CellStyle类
|
|
|
|
|
import org.apache.poi.ss.usermodel.Comment; // 导入Comment类
|
|
|
|
|
import org.apache.poi.ss.usermodel.DataFormat; // 导入DataFormat类
|
|
|
|
|
import org.apache.poi.ss.usermodel.Font; // 导入Font类
|
|
|
|
|
import org.apache.poi.ss.usermodel.IndexedColors; // 导入IndexedColors类
|
|
|
|
|
import org.apache.poi.ss.usermodel.Row; // 导入Row类
|
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet; // 导入Sheet类
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook; // 导入Workbook类
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress; // 导入CellRangeAddress类
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook; // 导入SXSSFWorkbook类
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; // 导入XSSFClientAnchor类
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString; // 导入XSSFRichTextString类
|
|
|
|
|
import org.slf4j.Logger; // 导入Logger接口
|
|
|
|
|
import org.slf4j.LoggerFactory; // 导入LoggerFactory类
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; // 导入HttpServletResponse类
|
|
|
|
|
import java.io.IOException; // 导入IOException类
|
|
|
|
|
import java.io.OutputStream; // 导入OutputStream类
|
|
|
|
|
import java.lang.reflect.Field; // 导入Field类
|
|
|
|
|
import java.lang.reflect.Method; // 导入Method类
|
|
|
|
|
import java.net.URLEncoder; // 导入URLEncoder类
|
|
|
|
|
import java.util.Collections; // 导入Collections类
|
|
|
|
|
import java.util.Comparator; // 导入Comparator接口
|
|
|
|
|
import java.util.Date; // 导入Date类
|
|
|
|
|
import java.util.HashMap; // 导入HashMap类
|
|
|
|
|
import java.util.List; // 导入List接口
|
|
|
|
|
import java.util.Map; // 导入Map接口
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导出Excel文件(导出“XLSX”格式,支持大数据量导出 @see org.apache.poi.ss.SpreadsheetVersion)
|
|
|
|
|
* @author jeeplus
|
|
|
|
|
* @version 2016-04-21
|
|
|
|
|
*/
|
|
|
|
|
public class ExportExcel {
|
|
|
|
|
public class ExportExcel { // ExportExcel类定义
|
|
|
|
|
|
|
|
|
|
private static Logger log = LoggerFactory.getLogger(ExportExcel.class);
|
|
|
|
|
private static Logger log = LoggerFactory.getLogger(ExportExcel.class); // 日志记录器
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 工作薄对象
|
|
|
|
|
*/
|
|
|
|
|
private SXSSFWorkbook wb;
|
|
|
|
|
private SXSSFWorkbook wb; // 工作薄对象
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 工作表对象
|
|
|
|
|
*/
|
|
|
|
|
private Sheet sheet;
|
|
|
|
|
private Sheet sheet; // 工作表对象
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 样式列表
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, CellStyle> styles;
|
|
|
|
|
private Map<String, CellStyle> styles; // 样式列表
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当前行号
|
|
|
|
|
*/
|
|
|
|
|
private int rownum;
|
|
|
|
|
private int rownum; // 当前行号
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注解列表(Object[]{ ExcelField, Field/Method })
|
|
|
|
|
*/
|
|
|
|
|
List<Object[]> annotationList = Lists.newArrayList();
|
|
|
|
|
List<Object[]> annotationList = Lists.newArrayList(); // 注解列表
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param title 表格标题,传“空值”,表示无标题
|
|
|
|
|
* @param cls 实体对象,通过annotation.ExportField获取标题
|
|
|
|
|
*/
|
|
|
|
|
public ExportExcel(String title, Class<?> cls){
|
|
|
|
|
this(title, cls, 1);
|
|
|
|
|
public ExportExcel(String title, Class<?> cls){ // 构造函数
|
|
|
|
|
this(title, cls, 1); // 调用另一个构造函数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -86,123 +87,123 @@ public class ExportExcel {
|
|
|
|
|
* @param type 导出类型(1:导出数据;2:导出模板)
|
|
|
|
|
* @param groups 导入分组
|
|
|
|
|
*/
|
|
|
|
|
public ExportExcel(String title, Class<?> cls, int type, int... groups){
|
|
|
|
|
public ExportExcel(String title, Class<?> cls, int type, int... groups){ // 构造函数
|
|
|
|
|
// Get annotation field
|
|
|
|
|
Field[] fs = cls.getDeclaredFields();
|
|
|
|
|
for (Field f : fs){
|
|
|
|
|
ExcelField ef = f.getAnnotation(ExcelField.class);
|
|
|
|
|
if (ef != null && (ef.type()==0 || ef.type()==type)){
|
|
|
|
|
if (groups!=null && groups.length>0){
|
|
|
|
|
boolean inGroup = false;
|
|
|
|
|
for (int g : groups){
|
|
|
|
|
if (inGroup){
|
|
|
|
|
break;
|
|
|
|
|
Field[] fs = cls.getDeclaredFields(); // 获取类的所有字段
|
|
|
|
|
for (Field f : fs){ // 遍历字段
|
|
|
|
|
ExcelField ef = f.getAnnotation(ExcelField.class); // 获取ExcelField注解
|
|
|
|
|
if (ef != null && (ef.type()==0 || ef.type()==type)){ // 检查注解类型
|
|
|
|
|
if (groups!=null && groups.length>0){ // 检查分组
|
|
|
|
|
boolean inGroup = false; // 初始化分组标志
|
|
|
|
|
for (int g : groups){ // 遍历分组
|
|
|
|
|
if (inGroup){ // 如果已经在分组中
|
|
|
|
|
break; // 退出循环
|
|
|
|
|
}
|
|
|
|
|
for (int efg : ef.groups()){
|
|
|
|
|
if (g == efg){
|
|
|
|
|
inGroup = true;
|
|
|
|
|
annotationList.add(new Object[]{ef, f});
|
|
|
|
|
break;
|
|
|
|
|
for (int efg : ef.groups()){ // 遍历注解中的分组
|
|
|
|
|
if (g == efg){ // 如果匹配
|
|
|
|
|
inGroup = true; // 设置标志
|
|
|
|
|
annotationList.add(new Object[]{ef, f}); // 添加到注解列表
|
|
|
|
|
break; // 退出循环
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
annotationList.add(new Object[]{ef, f});
|
|
|
|
|
}else{ // 如果没有分组
|
|
|
|
|
annotationList.add(new Object[]{ef, f}); // 添加到注解列表
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Get annotation method
|
|
|
|
|
Method[] ms = cls.getDeclaredMethods();
|
|
|
|
|
for (Method m : ms){
|
|
|
|
|
ExcelField ef = m.getAnnotation(ExcelField.class);
|
|
|
|
|
if (ef != null && (ef.type()==0 || ef.type()==type)){
|
|
|
|
|
if (groups!=null && groups.length>0){
|
|
|
|
|
boolean inGroup = false;
|
|
|
|
|
for (int g : groups){
|
|
|
|
|
if (inGroup){
|
|
|
|
|
break;
|
|
|
|
|
Method[] ms = cls.getDeclaredMethods(); // 获取类的所有方法
|
|
|
|
|
for (Method m : ms){ // 遍历方法
|
|
|
|
|
ExcelField ef = m.getAnnotation(ExcelField.class); // 获取ExcelField注解
|
|
|
|
|
if (ef != null && (ef.type()==0 || ef.type()==type)){ // 检查注解类型
|
|
|
|
|
if (groups!=null && groups.length>0){ // 检查分组
|
|
|
|
|
boolean inGroup = false; // 初始化分组标志
|
|
|
|
|
for (int g : groups){ // 遍历分组
|
|
|
|
|
if (inGroup){ // 如果已经在分组中
|
|
|
|
|
break; // 退出循环
|
|
|
|
|
}
|
|
|
|
|
for (int efg : ef.groups()){
|
|
|
|
|
if (g == efg){
|
|
|
|
|
inGroup = true;
|
|
|
|
|
annotationList.add(new Object[]{ef, m});
|
|
|
|
|
break;
|
|
|
|
|
for (int efg : ef.groups()){ // 遍历注解中的分组
|
|
|
|
|
if (g == efg){ // 如果匹配
|
|
|
|
|
inGroup = true; // 设置标志
|
|
|
|
|
annotationList.add(new Object[]{ef, m}); // 添加到注解列表
|
|
|
|
|
break; // 退出循环
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
annotationList.add(new Object[]{ef, m});
|
|
|
|
|
}else{ // 如果没有分组
|
|
|
|
|
annotationList.add(new Object[]{ef, m}); // 添加到注解列表
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Field sorting
|
|
|
|
|
Collections.sort(annotationList, new Comparator<Object[]>() {
|
|
|
|
|
Collections.sort(annotationList, new Comparator<Object[]>() { // 对注解列表进行排序
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(Object[] o1, Object[] o2) {
|
|
|
|
|
return new Integer(((ExcelField)o1[0]).sort()).compareTo(
|
|
|
|
|
public int compare(Object[] o1, Object[] o2) { // 比较方法
|
|
|
|
|
return new Integer(((ExcelField)o1[0]).sort()).compareTo( // 比较排序值
|
|
|
|
|
new Integer(((ExcelField)o2[0]).sort()));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// Initialize
|
|
|
|
|
List<String> headerList = Lists.newArrayList();
|
|
|
|
|
for (Object[] os : annotationList){
|
|
|
|
|
String t = ((ExcelField)os[0]).title();
|
|
|
|
|
List<String> headerList = Lists.newArrayList(); // 初始化表头列表
|
|
|
|
|
for (Object[] os : annotationList){ // 遍历注解列表
|
|
|
|
|
String t = ((ExcelField)os[0]).title(); // 获取标题
|
|
|
|
|
// 如果是导出,则去掉注释
|
|
|
|
|
if (type==1){
|
|
|
|
|
String[] ss = StringUtils.split(t, "**", 2);
|
|
|
|
|
if (ss.length==2){
|
|
|
|
|
t = ss[0];
|
|
|
|
|
if (type==1){ // 如果是导出数据
|
|
|
|
|
String[] ss = StringUtils.split(t, "**", 2); // 分割标题
|
|
|
|
|
if (ss.length==2){ // 如果有注释
|
|
|
|
|
t = ss[0]; // 去掉注释
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
headerList.add(t);
|
|
|
|
|
headerList.add(t); // 添加标题到表头列表
|
|
|
|
|
}
|
|
|
|
|
initialize(title, headerList);
|
|
|
|
|
initialize(title, headerList); // 初始化工作薄
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 初始化函数
|
|
|
|
|
* @param title 表格标题,传“空值”,表示无标题
|
|
|
|
|
* @param headerList 表头列表
|
|
|
|
|
*/
|
|
|
|
|
private void initialize(String title, List<String> headerList) {
|
|
|
|
|
this.wb = new SXSSFWorkbook(500);
|
|
|
|
|
this.sheet = wb.createSheet("Export");
|
|
|
|
|
this.styles = createStyles(wb);
|
|
|
|
|
private void initialize(String title, List<String> headerList) { // 初始化方法
|
|
|
|
|
this.wb = new SXSSFWorkbook(500); // 创建工作薄
|
|
|
|
|
this.sheet = wb.createSheet("Export"); // 创建工作表
|
|
|
|
|
this.styles = createStyles(wb); // 创建样式
|
|
|
|
|
// Create title
|
|
|
|
|
if (StringUtils.isNotBlank(title)){
|
|
|
|
|
Row titleRow = sheet.createRow(rownum++);
|
|
|
|
|
titleRow.setHeightInPoints(30);
|
|
|
|
|
Cell titleCell = titleRow.createCell(0);
|
|
|
|
|
titleCell.setCellStyle(styles.get("title"));
|
|
|
|
|
titleCell.setCellValue(title);
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(),
|
|
|
|
|
if (StringUtils.isNotBlank(title)){ // 如果标题不为空
|
|
|
|
|
Row titleRow = sheet.createRow(rownum++); // 创建标题行
|
|
|
|
|
titleRow.setHeightInPoints(30); // 设置行高
|
|
|
|
|
Cell titleCell = titleRow.createCell(0); // 创建单元格
|
|
|
|
|
titleCell.setCellStyle(styles.get("title")); // 设置单元格样式
|
|
|
|
|
titleCell.setCellValue(title); // 设置单元格值
|
|
|
|
|
sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), // 合并单元格
|
|
|
|
|
titleRow.getRowNum(), titleRow.getRowNum(), headerList.size()-1));
|
|
|
|
|
}
|
|
|
|
|
// Create header
|
|
|
|
|
if (headerList == null){
|
|
|
|
|
throw new RuntimeException("headerList not null!");
|
|
|
|
|
if (headerList == null){ // 如果表头列表为空
|
|
|
|
|
throw new RuntimeException("headerList not null!"); // 抛出异常
|
|
|
|
|
}
|
|
|
|
|
Row headerRow = sheet.createRow(rownum++);
|
|
|
|
|
headerRow.setHeightInPoints(16);
|
|
|
|
|
for (int i = 0; i < headerList.size(); i++) {
|
|
|
|
|
Cell cell = headerRow.createCell(i);
|
|
|
|
|
cell.setCellStyle(styles.get("header"));
|
|
|
|
|
String[] ss = StringUtils.split(headerList.get(i), "**", 2);
|
|
|
|
|
if (ss.length==2){
|
|
|
|
|
cell.setCellValue(ss[0]);
|
|
|
|
|
Comment comment = this.sheet.createDrawingPatriarch().createCellComment(
|
|
|
|
|
Row headerRow = sheet.createRow(rownum++); // 创建表头行
|
|
|
|
|
headerRow.setHeightInPoints(16); // 设置行高
|
|
|
|
|
for (int i = 0; i < headerList.size(); i++) { // 遍历表头列表
|
|
|
|
|
Cell cell = headerRow.createCell(i); // 创建单元格
|
|
|
|
|
cell.setCellStyle(styles.get("header")); // 设置单元格样式
|
|
|
|
|
String[] ss = StringUtils.split(headerList.get(i), "**", 2); // 分割表头
|
|
|
|
|
if (ss.length==2){ // 如果有注释
|
|
|
|
|
cell.setCellValue(ss[0]); // 设置单元格值
|
|
|
|
|
Comment comment = this.sheet.createDrawingPatriarch().createCellComment( // 创建注释
|
|
|
|
|
new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6));
|
|
|
|
|
comment.setString(new XSSFRichTextString(ss[1]));
|
|
|
|
|
cell.setCellComment(comment);
|
|
|
|
|
}else{
|
|
|
|
|
cell.setCellValue(headerList.get(i));
|
|
|
|
|
comment.setString(new XSSFRichTextString(ss[1])); // 设置注释内容
|
|
|
|
|
cell.setCellComment(comment); // 设置单元格注释
|
|
|
|
|
}else{ // 如果没有注释
|
|
|
|
|
cell.setCellValue(headerList.get(i)); // 设置单元格值
|
|
|
|
|
}
|
|
|
|
|
sheet.autoSizeColumn(i);
|
|
|
|
|
sheet.autoSizeColumn(i); // 自动调整列宽
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < headerList.size(); i++) {
|
|
|
|
|
int colWidth = sheet.getColumnWidth(i)*2;
|
|
|
|
|
sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth);
|
|
|
|
|
for (int i = 0; i < headerList.size(); i++) { // 遍历表头列表
|
|
|
|
|
int colWidth = sheet.getColumnWidth(i)*2; // 计算列宽
|
|
|
|
|
sheet.setColumnWidth(i, colWidth < 3000 ? 3000 : colWidth); // 设置列宽
|
|
|
|
|
}
|
|
|
|
|
log.debug("Initialize success.");
|
|
|
|
|
log.debug("Initialize success."); // 记录初始化成功日志
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -210,73 +211,73 @@ public class ExportExcel {
|
|
|
|
|
* @param wb 工作薄对象
|
|
|
|
|
* @return 样式列表
|
|
|
|
|
*/
|
|
|
|
|
private Map<String, CellStyle> createStyles(Workbook wb) {
|
|
|
|
|
Map<String, CellStyle> styles = new HashMap<>(16);
|
|
|
|
|
private Map<String, CellStyle> createStyles(Workbook wb) { // 创建样式方法
|
|
|
|
|
Map<String, CellStyle> styles = new HashMap<>(16); // 初始化样式列表
|
|
|
|
|
|
|
|
|
|
CellStyle style = wb.createCellStyle();
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_CENTER);
|
|
|
|
|
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
|
|
|
|
Font titleFont = wb.createFont();
|
|
|
|
|
titleFont.setFontName("Arial");
|
|
|
|
|
titleFont.setFontHeightInPoints((short) 16);
|
|
|
|
|
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
|
|
style.setFont(titleFont);
|
|
|
|
|
styles.put("title", style);
|
|
|
|
|
CellStyle style = wb.createCellStyle(); // 创建样式
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_CENTER); // 设置水平对齐方式
|
|
|
|
|
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 设置垂直对齐方式
|
|
|
|
|
Font titleFont = wb.createFont(); // 创建字体
|
|
|
|
|
titleFont.setFontName("Arial"); // 设置字体名称
|
|
|
|
|
titleFont.setFontHeightInPoints((short) 16); // 设置字体大小
|
|
|
|
|
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // 设置字体加粗
|
|
|
|
|
style.setFont(titleFont); // 设置样式字体
|
|
|
|
|
styles.put("title", style); // 添加样式到列表
|
|
|
|
|
|
|
|
|
|
style = wb.createCellStyle();
|
|
|
|
|
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
|
|
|
|
style.setBorderRight(CellStyle.BORDER_THIN);
|
|
|
|
|
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
|
style.setBorderLeft(CellStyle.BORDER_THIN);
|
|
|
|
|
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
|
style.setBorderTop(CellStyle.BORDER_THIN);
|
|
|
|
|
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
|
style.setBorderBottom(CellStyle.BORDER_THIN);
|
|
|
|
|
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
|
Font dataFont = wb.createFont();
|
|
|
|
|
dataFont.setFontName("Arial");
|
|
|
|
|
dataFont.setFontHeightInPoints((short) 10);
|
|
|
|
|
style.setFont(dataFont);
|
|
|
|
|
styles.put("data", style);
|
|
|
|
|
style = wb.createCellStyle(); // 创建样式
|
|
|
|
|
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 设置垂直对齐方式
|
|
|
|
|
style.setBorderRight(CellStyle.BORDER_THIN); // 设置右边框
|
|
|
|
|
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); // 设置右边框颜色
|
|
|
|
|
style.setBorderLeft(CellStyle.BORDER_THIN); // 设置左边框
|
|
|
|
|
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); // 设置左边框颜色
|
|
|
|
|
style.setBorderTop(CellStyle.BORDER_THIN); // 设置上边框
|
|
|
|
|
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); // 设置上边框颜色
|
|
|
|
|
style.setBorderBottom(CellStyle.BORDER_THIN); // 设置下边框
|
|
|
|
|
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); // 设置下边框颜色
|
|
|
|
|
Font dataFont = wb.createFont(); // 创建字体
|
|
|
|
|
dataFont.setFontName("Arial"); // 设置字体名称
|
|
|
|
|
dataFont.setFontHeightInPoints((short) 10); // 设置字体大小
|
|
|
|
|
style.setFont(dataFont); // 设置样式字体
|
|
|
|
|
styles.put("data", style); // 添加样式到列表
|
|
|
|
|
|
|
|
|
|
style = wb.createCellStyle();
|
|
|
|
|
style.cloneStyleFrom(styles.get("data"));
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_LEFT);
|
|
|
|
|
styles.put("data1", style);
|
|
|
|
|
style = wb.createCellStyle(); // 创建样式
|
|
|
|
|
style.cloneStyleFrom(styles.get("data")); // 克隆数据样式
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_LEFT); // 设置左对齐
|
|
|
|
|
styles.put("data1", style); // 添加样式到列表
|
|
|
|
|
|
|
|
|
|
style = wb.createCellStyle();
|
|
|
|
|
style.cloneStyleFrom(styles.get("data"));
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_CENTER);
|
|
|
|
|
styles.put("data2", style);
|
|
|
|
|
style = wb.createCellStyle(); // 创建样式
|
|
|
|
|
style.cloneStyleFrom(styles.get("data")); // 克隆数据样式
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_CENTER); // 设置居中对齐
|
|
|
|
|
styles.put("data2", style); // 添加样式到列表
|
|
|
|
|
|
|
|
|
|
style = wb.createCellStyle();
|
|
|
|
|
style.cloneStyleFrom(styles.get("data"));
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_RIGHT);
|
|
|
|
|
styles.put("data3", style);
|
|
|
|
|
style = wb.createCellStyle(); // 创建样式
|
|
|
|
|
style.cloneStyleFrom(styles.get("data")); // 克隆数据样式
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_RIGHT); // 设置右对齐
|
|
|
|
|
styles.put("data3", style); // 添加样式到列表
|
|
|
|
|
|
|
|
|
|
style = wb.createCellStyle();
|
|
|
|
|
style.cloneStyleFrom(styles.get("data"));
|
|
|
|
|
// style.setWrapText(true);
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_CENTER);
|
|
|
|
|
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
|
|
|
|
Font headerFont = wb.createFont();
|
|
|
|
|
headerFont.setFontName("Arial");
|
|
|
|
|
headerFont.setFontHeightInPoints((short) 10);
|
|
|
|
|
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
|
|
headerFont.setColor(IndexedColors.WHITE.getIndex());
|
|
|
|
|
style.setFont(headerFont);
|
|
|
|
|
styles.put("header", style);
|
|
|
|
|
style = wb.createCellStyle(); // 创建样式
|
|
|
|
|
style.cloneStyleFrom(styles.get("data")); // 克隆数据样式
|
|
|
|
|
// style.setWrapText(true); // 设置文本换行
|
|
|
|
|
style.setAlignment(CellStyle.ALIGN_CENTER); // 设置居中对齐
|
|
|
|
|
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); // 设置前景色
|
|
|
|
|
style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置填充模式
|
|
|
|
|
Font headerFont = wb.createFont(); // 创建字体
|
|
|
|
|
headerFont.setFontName("Arial"); // 设置字体名称
|
|
|
|
|
headerFont.setFontHeightInPoints((short) 10); // 设置字体大小
|
|
|
|
|
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // 设置字体加粗
|
|
|
|
|
headerFont.setColor(IndexedColors.WHITE.getIndex()); // 设置字体颜色
|
|
|
|
|
style.setFont(headerFont); // 设置样式字体
|
|
|
|
|
styles.put("header", style); // 添加样式到列表
|
|
|
|
|
|
|
|
|
|
return styles;
|
|
|
|
|
return styles; // 返回样式列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加一行
|
|
|
|
|
* @return 行对象
|
|
|
|
|
*/
|
|
|
|
|
public Row addRow(){
|
|
|
|
|
return sheet.createRow(rownum++);
|
|
|
|
|
public Row addRow(){ // 添加行方法
|
|
|
|
|
return sheet.createRow(rownum++); // 创建新行并返回
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -287,8 +288,8 @@ public class ExportExcel {
|
|
|
|
|
* @param val 添加值
|
|
|
|
|
* @return 单元格对象
|
|
|
|
|
*/
|
|
|
|
|
public Cell addCell(Row row, int column, Object val){
|
|
|
|
|
return this.addCell(row, column, val, 0, Class.class);
|
|
|
|
|
public Cell addCell(Row row, int column, Object val){ // 添加单元格方法
|
|
|
|
|
return this.addCell(row, column, val, 0, Class.class); // 调用重载方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -299,104 +300,104 @@ public class ExportExcel {
|
|
|
|
|
* @param align 对齐方式(1:靠左;2:居中;3:靠右)
|
|
|
|
|
* @return 单元格对象
|
|
|
|
|
*/
|
|
|
|
|
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){
|
|
|
|
|
Cell cell = row.createCell(column);
|
|
|
|
|
CellStyle style = styles.get("data"+(align>=1&&align<=3?align:""));
|
|
|
|
|
try {
|
|
|
|
|
if (val == null){
|
|
|
|
|
cell.setCellValue("");
|
|
|
|
|
} else if (val instanceof String) {
|
|
|
|
|
cell.setCellValue((String) val);
|
|
|
|
|
} else if (val instanceof Integer) {
|
|
|
|
|
cell.setCellValue((Integer) val);
|
|
|
|
|
} else if (val instanceof Long) {
|
|
|
|
|
cell.setCellValue((Long) val);
|
|
|
|
|
} else if (val instanceof Double) {
|
|
|
|
|
cell.setCellValue((Double) val);
|
|
|
|
|
} else if (val instanceof Float) {
|
|
|
|
|
cell.setCellValue((Float) val);
|
|
|
|
|
} else if (val instanceof Date) {
|
|
|
|
|
DataFormat format = wb.createDataFormat();
|
|
|
|
|
style.setDataFormat(format.getFormat("yyyy-MM-dd"));
|
|
|
|
|
cell.setCellValue((Date) val);
|
|
|
|
|
} else {
|
|
|
|
|
if (fieldType != Class.class){
|
|
|
|
|
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
|
|
|
|
|
}else{
|
|
|
|
|
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){ // 添加单元格重载方法
|
|
|
|
|
Cell cell = row.createCell(column); // 创建单元格
|
|
|
|
|
CellStyle style = styles.get("data"+(align>=1&&align<=3?align:"")); // 获取样式
|
|
|
|
|
try { // 尝试设置单元格值
|
|
|
|
|
if (val == null){ // 如果值为null
|
|
|
|
|
cell.setCellValue(""); // 设置为空字符串
|
|
|
|
|
} else if (val instanceof String) { // 如果值是字符串
|
|
|
|
|
cell.setCellValue((String) val); // 设置单元格值
|
|
|
|
|
} else if (val instanceof Integer) { // 如果值是整数
|
|
|
|
|
cell.setCellValue((Integer) val); // 设置单元格值
|
|
|
|
|
} else if (val instanceof Long) { // 如果值是长整型
|
|
|
|
|
cell.setCellValue((Long) val); // 设置单元格值
|
|
|
|
|
} else if (val instanceof Double) { // 如果值是双精度浮点型
|
|
|
|
|
cell.setCellValue((Double) val); // 设置单元格值
|
|
|
|
|
} else if (val instanceof Float) { // 如果值是单精度浮点型
|
|
|
|
|
cell.setCellValue((Float) val); // 设置单元格值
|
|
|
|
|
} else if (val instanceof Date) { // 如果值是日期
|
|
|
|
|
DataFormat format = wb.createDataFormat(); // 创建数据格式
|
|
|
|
|
style.setDataFormat(format.getFormat("yyyy-MM-dd")); // 设置日期格式
|
|
|
|
|
cell.setCellValue((Date) val); // 设置单元格值
|
|
|
|
|
} else { // 如果值是其他类型
|
|
|
|
|
if (fieldType != Class.class){ // 如果字段类型不是Class
|
|
|
|
|
cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val)); // 设置单元格值
|
|
|
|
|
}else{ // 如果字段类型是Class
|
|
|
|
|
cell.setCellValue((String)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
|
|
|
|
|
"fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val));
|
|
|
|
|
"fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val)); // 设置单元格值
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
log.info("Set cell value ["+row.getRowNum()+","+column+"] error: " + ex.toString());
|
|
|
|
|
cell.setCellValue(val.toString());
|
|
|
|
|
} catch (Exception ex) { // 捕获异常
|
|
|
|
|
log.info("Set cell value ["+row.getRowNum()+","+column+"] error: " + ex.toString()); // 记录错误日志
|
|
|
|
|
cell.setCellValue(val.toString()); // 设置单元格值为字符串
|
|
|
|
|
}
|
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
return cell;
|
|
|
|
|
cell.setCellStyle(style); // 设置单元格样式
|
|
|
|
|
return cell; // 返回单元格对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加数据(通过annotation.ExportField添加数据)
|
|
|
|
|
* @return list 数据列表
|
|
|
|
|
*/
|
|
|
|
|
public <E> ExportExcel setDataList(List<E> list){
|
|
|
|
|
for (E e : list){
|
|
|
|
|
int colunm = 0;
|
|
|
|
|
Row row = this.addRow();
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
for (Object[] os : annotationList){
|
|
|
|
|
ExcelField ef = (ExcelField)os[0];
|
|
|
|
|
Object val = null;
|
|
|
|
|
try{
|
|
|
|
|
if (StringUtils.isNotBlank(ef.value())){
|
|
|
|
|
val = Reflections.invokeGetter(e, ef.value());
|
|
|
|
|
}else{
|
|
|
|
|
if (os[1] instanceof Field){
|
|
|
|
|
val = Reflections.invokeGetter(e, ((Field)os[1]).getName());
|
|
|
|
|
}else if (os[1] instanceof Method){
|
|
|
|
|
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {});
|
|
|
|
|
public <E> ExportExcel setDataList(List<E> list){ // 设置数据列表方法
|
|
|
|
|
for (E e : list){ // 遍历数据列表
|
|
|
|
|
int colunm = 0; // 初始化列号
|
|
|
|
|
Row row = this.addRow(); // 添加新行
|
|
|
|
|
StringBuilder sb = new StringBuilder(); // 初始化字符串构建器
|
|
|
|
|
for (Object[] os : annotationList){ // 遍历注解列表
|
|
|
|
|
ExcelField ef = (ExcelField)os[0]; // 获取ExcelField注解
|
|
|
|
|
Object val = null; // 初始化值
|
|
|
|
|
try{ // 尝试获取值
|
|
|
|
|
if (StringUtils.isNotBlank(ef.value())){ // 如果注解值不为空
|
|
|
|
|
val = Reflections.invokeGetter(e, ef.value()); // 通过反射获取值
|
|
|
|
|
}else{ // 如果注解值为空
|
|
|
|
|
if (os[1] instanceof Field){ // 如果是字段
|
|
|
|
|
val = Reflections.invokeGetter(e, ((Field)os[1]).getName()); // 通过反射获取值
|
|
|
|
|
}else if (os[1] instanceof Method){ // 如果是方法
|
|
|
|
|
val = Reflections.invokeMethod(e, ((Method)os[1]).getName(), new Class[] {}, new Object[] {}); // 通过反射获取值
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch(Exception ex) {
|
|
|
|
|
log.info(ex.toString());
|
|
|
|
|
val = "";
|
|
|
|
|
}catch(Exception ex) { // 捕获异常
|
|
|
|
|
log.info(ex.toString()); // 记录错误日志
|
|
|
|
|
val = ""; // 设置值为空字符串
|
|
|
|
|
}
|
|
|
|
|
this.addCell(row, colunm++, val, ef.align(), ef.fieldType());
|
|
|
|
|
sb.append(val + ", ");
|
|
|
|
|
this.addCell(row, colunm++, val, ef.align(), ef.fieldType()); // 添加单元格
|
|
|
|
|
sb.append(val + ", "); // 添加值到字符串构建器
|
|
|
|
|
}
|
|
|
|
|
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString());
|
|
|
|
|
log.debug("Write success: ["+row.getRowNum()+"] "+sb.toString()); // 记录写入成功日志
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
return this; // 返回当前对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输出数据流
|
|
|
|
|
* @param os 输出数据流
|
|
|
|
|
*/
|
|
|
|
|
public ExportExcel write(OutputStream os) throws IOException{
|
|
|
|
|
wb.write(os);
|
|
|
|
|
return this;
|
|
|
|
|
public ExportExcel write(OutputStream os) throws IOException{ // 输出数据流方法
|
|
|
|
|
wb.write(os); // 写入工作薄到输出流
|
|
|
|
|
return this; // 返回当前对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输出到客户端
|
|
|
|
|
* @param fileName 输出文件名
|
|
|
|
|
*/
|
|
|
|
|
public ExportExcel write(HttpServletResponse response, String fileName) throws IOException{
|
|
|
|
|
response.reset();
|
|
|
|
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
response.setContentType("application/octet-stream; charset=utf-8");
|
|
|
|
|
response.addHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "utf-8"));
|
|
|
|
|
write(response.getOutputStream());
|
|
|
|
|
return this;
|
|
|
|
|
public ExportExcel write(HttpServletResponse response, String fileName) throws IOException{ // 输出到客户端方法
|
|
|
|
|
response.reset(); // 重置响应
|
|
|
|
|
response.setHeader("Access-Control-Allow-Origin", "*"); // 设置跨域头
|
|
|
|
|
response.setContentType("application/octet-stream; charset=utf-8"); // 设置内容类型
|
|
|
|
|
response.addHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "utf-8")); // 设置下载文件名
|
|
|
|
|
write(response.getOutputStream()); // 写入输出流
|
|
|
|
|
return this; // 返回当前对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清理临时文件
|
|
|
|
|
*/
|
|
|
|
|
public ExportExcel dispose(){
|
|
|
|
|
wb.dispose();
|
|
|
|
|
return this;
|
|
|
|
|
public ExportExcel dispose(){ // 清理临时文件方法
|
|
|
|
|
wb.dispose(); // 释放工作薄资源
|
|
|
|
|
return this; // 返回当前对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|