package com.example.demo.exportExcel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* Copyright © 2018 Shanghai
*
* @Description Excel 导入工具辅助类
* @Author zhaoweijiang
* @Date 2019/3/29 11:11
*
* Excel_2003 支持最大导出65536行
* Excel_2007 支持最大导出1048576行
* 详情查看SpreadsheetVersion.class
*
*/
public class ExportExcelUtil {
//注释换行符
public static final String NEWLINE = "\r\n";
//工作簿
private Workbook wb;
//工作表
private Sheet sheet;
//样式
private CellStyle cellStyle;
//工作簿类型 .xls/.xlsx
private String extension;
/**
* 默认模板工作表名称
*/
private String sheetName = "";
/*
* 默认模板名称字体大小为36
*/
private int typeFontSize = 36;
/*
* 默认模板注释字体大小为11
*/
private int annotationFontSize = 11;
/*
* 默认模板标题字体大小为11
*/
private int titleFontSize = 11;
/*
* 默认模板示例字体大小为11
*/
private int exampleFontSize = 11;
/**
* 对外提供set方法,可修改默认字体大小
*/
public void setSize(int typeFontSize) {
this.typeFontSize = typeFontSize;
}
public void setAnnotationFontSize(int annotationFontSize) {
this.annotationFontSize = annotationFontSize;
}
public void setTitleFontSize(int titleFontSize) {
this.titleFontSize = titleFontSize;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
/**
* 构造函数初始化
* ExcelTypeTl 自定义的一个枚举类,该枚举类是工作簿格式 .xls/.xlsx
*/
public ExportExcelUtil(ExcelTypeTl excelTypeTl){
String type = excelTypeTl.getType();
if (".xls".equals(type)){
this.wb = new HSSFWorkbook();
}else if (".xlsx".equals(type)){
this.wb = new XSSFWorkbook();
}else {
throw new IllegalArgumentException("Illegal parameter type");
}
//传递给私有属性,下面的其他方法还需要
this.extension = type;
}
/**
* 创建工作表
* 创建样式
* 注:表名称不可用特殊符号
*/
private void createSheet(){
this.sheet = wb.createSheet(this.sheetName.length() > 0 ? sheetName : "sheet1");
this.cellStyle = wb.createCellStyle();
}
/**
* 说明: excel的字体大小 * 20 = 设置值 例; excel 字体是 13 * 20 = 260
* 你需要设置字体大小为260.导出的excel才会是13的字体大小 ,,,同行高,具体源码里有
*
* row.setHeight( (short)( 20 * Math.round( (this.typeFontSize/0.69) ) )); //设置行高
* 我设置的行高,根据字体大小自己调试的我认为最佳行高
*
* 创建Excel模板名称 / 第一行
* @param titleLenght 标题长度
* @param description 模板名称 同工作簿名称
*/
private void createTlType(int titleLenght , String description){
CellRangeAddress region &
POI导出工具类,可导出模板,也可导出数据.
最新推荐文章于 2023-07-11 10:48:45 发布