浏览代码

导出加上了只读参数

4.1.3.A
jueyue 5 年前
父节点
当前提交
8ae8266c31
共有 4 个文件被更改,包括 70 次插入36 次删除
  1. +29
    -17
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/ExportParams.java
  2. +13
    -1
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/TemplateExportParams.java
  3. +19
    -18
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/ExcelExportService.java
  4. +9
    -0
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java

+ 29
- 17
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;
}
}

+ 13
- 1
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;
}
}

+ 19
- 18
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<ExcelExportEntity> 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<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
// 得到所有字段
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<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
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<Object> tempList = new ArrayList<Object>();
while (its.hasNext()) {
Object t = its.next();


+ 9
- 0
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. 正常的数据填充


正在加载...
取消
保存