diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java index 7a76c966..95453534 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java @@ -95,25 +95,37 @@ public class ExcelExport { * @param groups 导入分组 */ public ExcelExport(String title, Class cls, Type type, String... groups){ - this(null, title, cls, type, groups); + this(null, null, title, cls, type, groups); } /** * 构造函数 - * @param wb 工作簿对象,支持多个Sheet,通过ExcelExport.createWorkbook()创建 - * @param sheetName,指定Sheet名称 + * @param sheetName 指定Sheet名称 * @param title 表格标题,传“空值”,表示无标题 * @param cls 实体对象,通过annotation.ExportField获取标题 * @param type 导出类型(1:导出数据;2:导出模板) * @param groups 导入分组 */ - public ExcelExport(Workbook wb, String title, Class cls, Type type, String... groups){ + public ExcelExport(String sheetName, String title, Class cls, Type type, String... groups){ + this(null, sheetName, title, cls, type, groups); + } + + /** + * 构造函数 + * @param wb 指定现有工作簿对象 + * @param sheetName 指定Sheet名称 + * @param title 表格标题,传“空值”,表示无标题 + * @param cls 实体对象,通过annotation.ExportField获取标题 + * @param type 导出类型(1:导出数据;2:导出模板) + * @param groups 导入分组 + */ + public ExcelExport(Workbook wb, String sheetName, String title, Class cls, Type type, String... groups){ if (wb != null){ this.wb = wb; }else{ this.wb = createWorkbook(); } - this.createSheet(null, title, cls, type, groups); + this.createSheet(sheetName, title, cls, type, groups); } /** @@ -121,33 +133,51 @@ public class ExcelExport { * @param title 表格标题,传“空值”,表示无标题 * @param headerList 表头数组 */ - public ExcelExport(String title, List headerList) { - this(null, null, title, headerList); + public ExcelExport(String title, List headerList, List headerWidthList) { + this(null, null, title, headerList, headerWidthList); } /** * 构造函数 - * @param wb 工作簿对象,支持多个Sheet,通过ExcelExport.createWorkbook()创建 + * @param sheetName 指定Sheet名称 + * @param title 表格标题,传“空值”,表示无标题 + * @param headerList 表头数组 + */ + public ExcelExport(String sheetName, String title, List headerList, List headerWidthList) { + this(null, sheetName, title, headerList, headerWidthList); + } + + /** + * 构造函数 + * @param wb 指定现有工作簿对象 * @param sheetName,指定Sheet名称 * @param title 表格标题,传“空值”,表示无标题 * @param headerList 表头列表 */ - public ExcelExport(Workbook wb, String sheetName, String title, List headerList) { + public ExcelExport(Workbook wb, String sheetName, String title, List headerList, List headerWidthList) { if (wb != null){ this.wb = wb; }else{ this.wb = createWorkbook(); } - this.createSheet(sheetName, title, headerList, null); + this.createSheet(sheetName, title, headerList, headerWidthList); } /** * 创建一个工作簿 */ - private Workbook createWorkbook(){ + public Workbook createWorkbook(){ return new SXSSFWorkbook(500); } + /** + * 获取当前工作薄 + * @author ThinkGem + */ + public Workbook getWorkbook() { + return wb; + } + /** * 创建工作表 * @param sheetName,指定Sheet名称 @@ -238,15 +268,15 @@ public class ExcelExport { /** * 创建工作表 - * @param sheetName,指定Sheet名称 + * @param sheetName 指定Sheet名称 * @param title 表格标题,传“空值”,表示无标题 - * @param cls 实体对象,通过annotation.ExportField获取标题 - * @param type 导出类型(1:导出数据;2:导出模板) - * @param groups 导入分组 + * @param headerList 表头字段设置 + * @param headerWidthList 表头字段宽度设置 */ public void createSheet(String sheetName, String title, List headerList, List headerWidthList) { this.sheet = wb.createSheet(StringUtils.defaultString(sheetName, StringUtils.defaultString(title, "Sheet1"))); this.styles = createStyles(wb); + this.rownum = 0; // Create title if (StringUtils.isNotBlank(title)){ Row titleRow = sheet.createRow(rownum++); @@ -296,29 +326,9 @@ public class ExcelExport { sheet.setColumnWidth(i, colWidth); } } - log.debug("Create sheet {0} success.", sheetName); + log.debug("Create sheet {} success.", sheetName); } -// /** -// * 构造函数 -// * @param title 表格标题,传“空值”,表示无标题 -// * @param headers 表头数组 -// */ -// public ExcelExport(String title, List headerList) { -// this(null, null, title, headerList); -// } -// -// /** -// * 构造函数 -// * @param wb 工作簿对象,支持多个Sheet,通过ExcelExport.createWorkbook()创建 -// * @param sheetName,指定Sheet名称 -// * @param title 表格标题,传“空值”,表示无标题 -// * @param headerList 表头列表 -// */ -// public ExcelExport(Workbook wb, String sheetName, String title, List headerList) { -// initialize(wb, sheetName, title, headerList, null); -// } - /** * 创建表格样式 * @param wb 工作薄对象 @@ -576,23 +586,24 @@ public class ExcelExport { // */ // public static void main(String[] args) throws Throwable { // +// // 初始化表头 // List headerList = ListUtils.newArrayList(); // for (int i = 1; i <= 10; i++) { // headerList.add("表头"+i); // } -// -// List dataRowList = ListUtils.newArrayList(); +// +// // 初始化数据集 +// List rowList = ListUtils.newArrayList(); // for (int i = 1; i <= headerList.size(); i++) { -// dataRowList.add("数据"+i); +// rowList.add("数据"+i); // } -// // List> dataList = ListUtils.newArrayList(); // for (int i = 1; i <=100; i++) { -// dataList.add(dataRowList); +// dataList.add(rowList); // } -// -// ExcelExport ee = new ExcelExport("表格标题", headerList); // +// // 创建一个Sheet表,并导入数据 +// ExcelExport ee = new ExcelExport("表格1", "表格标题1", headerList, null); // for (int i = 0; i < dataList.size(); i++) { // Row row = ee.addRow(); // for (int j = 0; j < dataList.get(i).size(); j++) { @@ -600,8 +611,19 @@ public class ExcelExport { // } // } // +// // 再创建一个Sheet表,并导入数据 +// ee.createSheet("表格2", "表格标题2", headerList, null); +// for (int i = 0; i < dataList.size(); i++) { +// Row row = ee.addRow(); +// for (int j = 0; j < dataList.get(i).size(); j++) { +// ee.addCell(row, j, dataList.get(i).get(j)+"2"); +// } +// } +// +// // 输出到文件 // ee.writeFile("target/export.xlsx"); // +// // 清理销毁 // ee.dispose(); // // log.debug("Export success.");