From 8ae8266c31b65ee76d6fe57bf43011185adbeeb3 Mon Sep 17 00:00:00 2001 From: jueyue Date: Thu, 10 Oct 2019 09:42:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=8A=A0=E4=B8=8A=E4=BA=86?= =?UTF-8?q?=E5=8F=AA=E8=AF=BB=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easypoi/excel/entity/ExportParams.java | 46 ++++++++++++------- .../excel/entity/TemplateExportParams.java | 14 +++++- .../excel/export/ExcelExportService.java | 37 +++++++-------- .../template/ExcelExportOfTemplateUtil.java | 9 ++++ 4 files changed, 70 insertions(+), 36 deletions(-) diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExportParams.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExportParams.java index e848923..a837276 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExportParams.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExportParams.java @@ -15,10 +15,9 @@ */ package cn.afterturn.easypoi.excel.entity; -import org.apache.poi.hssf.util.HSSFColor; - import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl; +import org.apache.poi.hssf.util.HSSFColor; /** * Excel 导出参数 @@ -46,49 +45,49 @@ public class ExportParams extends ExcelBaseParams { /** * 表格名称 */ - private short secondTitleHeight = 8; + private short secondTitleHeight = 8; /** * sheetName */ - private String sheetName; + private String sheetName; /** * 过滤的属性 */ - private String[] exclusions; + private String[] exclusions; /** * 是否添加需要需要 */ - private boolean addIndex; + private boolean addIndex; /** * 是否添加需要需要 */ - private String indexName = "序号"; + private String indexName = "序号"; /** * 冰冻列 */ - private int freezeCol; + private int freezeCol; /** * 表头颜色 & 标题颜色 */ - private short color = HSSFColor.HSSFColorPredefined.WHITE.getIndex(); + private short color = HSSFColor.HSSFColorPredefined.WHITE.getIndex(); /** * 第二行标题颜色 * 属性说明行的颜色 例如:HSSFColor.SKY_BLUE.index 默认 */ - private short headerColor = HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex(); + private short headerColor = HSSFColor.HSSFColorPredefined.SKY_BLUE.getIndex(); /** * Excel 导出版本 */ - private ExcelType type = ExcelType.HSSF; + private ExcelType type = ExcelType.HSSF; /** * Excel 导出style */ - private Class style = ExcelExportStylerDefaultImpl.class; + private Class style = ExcelExportStylerDefaultImpl.class; /** * 表头高度 */ - private double headerHeight = 9D; + private double headerHeight = 9D; /** * 是否创建表头 */ @@ -96,20 +95,20 @@ public class ExportParams extends ExcelBaseParams { /** * 是否动态获取数据 */ - private boolean isDynamicData = false; + private boolean isDynamicData = false; /** * 是否追加图形 */ - private boolean isAppendGraph = true; + private boolean isAppendGraph = true; /** * 是否固定表头 */ - private boolean isFixedTitle= true; + private boolean isFixedTitle = true; /** * 单sheet最大值 * 03版本默认6W行,07默认100W */ - private int maxNum = 0; + private int maxNum = 0; /** * 导出时在excel中每个列的高度 单位为字符,一个汉字=2个字符 @@ -117,6 +116,11 @@ public class ExportParams extends ExcelBaseParams { */ private short height = 0; + /** + * 只读 + */ + private boolean readonly = false; + public ExportParams() { } @@ -297,4 +301,12 @@ public class ExportParams extends ExcelBaseParams { public void setFixedTitle(boolean fixedTitle) { isFixedTitle = fixedTitle; } + + public boolean isReadonly() { + return readonly; + } + + public void setReadonly(boolean readonly) { + this.readonly = readonly; + } } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/TemplateExportParams.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/TemplateExportParams.java index baadc42..b7cf527 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/TemplateExportParams.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/TemplateExportParams.java @@ -74,7 +74,11 @@ public class TemplateExportParams extends ExcelBaseParams { */ private String tempParams = "t"; - private boolean colForEach = false; + private boolean colForEach = false; + /** + * 只读 + */ + private boolean readonly = false; /** * 默认构造器 @@ -219,4 +223,12 @@ public class TemplateExportParams extends ExcelBaseParams { public Workbook getTemplateWb() { return templateWb; } + + public boolean isReadonly() { + return readonly; + } + + public void setReadonly(boolean readonly) { + this.readonly = readonly; + } } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportService.java index ab05822..2af12ea 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportService.java @@ -15,14 +15,6 @@ */ package cn.afterturn.easypoi.excel.export; -import java.lang.reflect.Field; -import java.util.*; - -import cn.afterturn.easypoi.util.PoiMergeCellUtil; -import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.ss.util.CellRangeAddress; - import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; @@ -32,7 +24,14 @@ import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; import cn.afterturn.easypoi.exception.excel.ExcelExportException; import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum; import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil; +import cn.afterturn.easypoi.util.PoiMergeCellUtil; import cn.afterturn.easypoi.util.PoiPublicUtil; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; + +import java.lang.reflect.Field; +import java.util.*; /** * Excel导出服务 @@ -65,7 +64,7 @@ public class ExcelExportService extends BaseExportService { */ private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index, List excelParams, int cellIndex) { - Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); + Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index); int rows = getRowNums(excelParams, true); row.setHeight(title.getHeaderHeight()); Row listRow = null; @@ -77,8 +76,8 @@ public class ExcelExportService extends BaseExportService { } } - int groupCellLength = 0; - CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); + int groupCellLength = 0; + CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor()); for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) { ExcelExportEntity entity = excelParams.get(i); // 加入换了groupName或者结束就,就把之前的那个换行 @@ -167,9 +166,9 @@ public class ExcelExportService extends BaseExportService { try { List excelParams = new ArrayList(); // 得到所有字段 - Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); - ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); - String targetId = etarget == null ? null : etarget.value(); + Field[] fileds = PoiPublicUtil.getClassFields(pojoClass); + ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); + String targetId = etarget == null ? null : etarget.value(); getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass, null, null); //获取所有参数后,后面的逻辑判断就一致了 @@ -203,8 +202,10 @@ public class ExcelExportService extends BaseExportService { // 重复遍历,出现了重名现象,创建非指定的名称Sheet sheet = workbook.createSheet(); } - - if (dataSet.getClass().getClass().getName().contains("Unmodifiable") ){ + if (entity.isReadonly()) { + sheet.protectSheet(UUID.randomUUID().toString()); + } + if (dataSet.getClass().getClass().getName().contains("Unmodifiable")) { List dataTemp = new ArrayList<>(); dataTemp.addAll(dataSet); dataSet = dataTemp; @@ -225,7 +226,7 @@ public class ExcelExportService extends BaseExportService { // 创建表格样式 setExcelExportStyler((IExcelExportStyler) entity.getStyle() .getConstructor(Workbook.class).newInstance(workbook)); - Drawing patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); + Drawing patriarch = PoiExcelGraphDataUtil.getDrawingPatriarch(sheet); List excelParams = new ArrayList(); if (entity.isAddIndex()) { excelParams.add(indexExcelEntity(entity)); @@ -239,7 +240,7 @@ public class ExcelExportService extends BaseExportService { setColumnHidden(excelParams, sheet); short rowHeight = entity.getHeight() != 0 ? entity.getHeight() : getRowHeight(excelParams); setCurrentIndex(1); - Iterator its = dataSet.iterator(); + Iterator its = dataSet.iterator(); List tempList = new ArrayList(); while (its.hasNext()) { Object t = its.next(); diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java index 6d0aaf8..895d000 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java @@ -258,6 +258,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { wb.setSheetName(sheetIndex, mapList.get(i).get(SHEET_NAME).toString()); } parseTemplate(wb.getSheetAt(sheetIndex), mapList.get(i), params.isColForEach()); + if (params.isReadonly()) { + wb.getSheetAt(i).protectSheet(UUID.randomUUID().toString()); + } sheetIndex++; } } @@ -291,6 +294,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { } tempCreateCellSet.clear(); parseTemplate(wb.getSheetAt(i), map.get(i), params.isColForEach()); + if (params.isReadonly()) { + wb.getSheetAt(i).protectSheet(UUID.randomUUID().toString()); + } } } catch (Exception e) { LOGGER.error(e.getMessage(), e); @@ -332,6 +338,9 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { } tempCreateCellSet.clear(); parseTemplate(wb.getSheetAt(i), map, params.isColForEach()); + if (params.isReadonly()) { + wb.getSheetAt(i).protectSheet(UUID.randomUUID().toString()); + } } if (dataSet != null) { // step 4. 正常的数据填充