init
This commit is contained in:
33
jeelowcode-framework/jeelowcode-excel/pom.xml
Normal file
33
jeelowcode-framework/jeelowcode-excel/pom.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>jeelowcode-framework</artifactId>
|
||||
<groupId>com.jeelowcode</groupId>
|
||||
<version>${jeelowcode.version}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>jeelowcode-excel</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<version>${jeelowcode.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
<description> JeeLowCode低代码平台 - excel模块 </description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jeelowcode</groupId>
|
||||
<artifactId>jeelowcode-utils</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,47 @@
|
||||
|
||||
package com.jeelowcode.framework.excel;
|
||||
|
||||
import com.jeelowcode.framework.excel.model.ExcelTitleModel;
|
||||
import com.jeelowcode.framework.excel.read.ExcelImportUtils;
|
||||
import com.jeelowcode.framework.excel.write.ExcelExportUtils;
|
||||
import org.apache.commons.collections4.map.LinkedMap;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JeeLowCodeExcelUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 导出 Excel
|
||||
* @param response
|
||||
* @param sheetName
|
||||
* @param headMap
|
||||
* @param dataList
|
||||
*/
|
||||
public static void exportExcel(HttpServletResponse response, String sheetName, LinkedMap<String, ExcelTitleModel> headMap, List<Map<String, Object>> dataList) {
|
||||
ExcelExportUtils.exportExcel(response,sheetName,headMap,dataList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入Excel
|
||||
* @param inputStream
|
||||
* @param headerCou 表头所在的行数
|
||||
* @return
|
||||
*/
|
||||
public static List<Map<String, Object>> importExcel(InputStream inputStream, Integer headerCou, Map<String, String> fieldNameAndCodeMap){
|
||||
return ExcelImportUtils.importExcel(inputStream,headerCou,fieldNameAndCodeMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入Excel
|
||||
* @param inputStream
|
||||
* @param headerCou 表头所在的行数
|
||||
* @return
|
||||
*/
|
||||
public static List<Map<String, Object>> importExcel(InputStream inputStream, Integer headerCou,Integer sheet, Map<String, String> fieldNameAndCodeMap){
|
||||
return ExcelImportUtils.importExcel(inputStream,headerCou,fieldNameAndCodeMap,sheet);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
|
||||
package com.jeelowcode.framework.excel.handler;
|
||||
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class CustomSheetWriteHandler implements SheetWriteHandler {
|
||||
|
||||
private static final Integer COLUMN = 26;
|
||||
|
||||
@Override
|
||||
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
|
||||
for (int i = 0; i < COLUMN; i++) {
|
||||
// 设置为文本格式
|
||||
SXSSFSheet sxssfSheet = (SXSSFSheet) writeSheetHolder.getSheet();
|
||||
CellStyle cellStyle = writeWorkbookHolder.getCachedWorkbook().createCellStyle();
|
||||
// 49为文本格式
|
||||
cellStyle.setDataFormat((short) 49);
|
||||
// i为列,一整列设置为文本格式
|
||||
sxssfSheet.setDefaultColumnStyle(i, cellStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
|
||||
package com.jeelowcode.framework.excel.handler;
|
||||
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
||||
import com.jeelowcode.framework.utils.utils.FuncBase;
|
||||
import com.alibaba.excel.write.handler.CellWriteHandler;
|
||||
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddressList;
|
||||
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
import org.apache.poi.xssf.usermodel.XSSFColor;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 单元格设置文本格式
|
||||
*/
|
||||
public class RowFormatSetTextHandler implements CellWriteHandler {
|
||||
|
||||
Map<Integer, List<String>> dropdownOptionsMap=new HashMap<>();
|
||||
|
||||
Map<Integer, CellStyle> cellStyleMap = new HashMap<>();
|
||||
|
||||
public RowFormatSetTextHandler() {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void beforeCellCreate(CellWriteHandlerContext context) {
|
||||
|
||||
WriteSheetHolder writeSheetHolder = context.getWriteSheetHolder();
|
||||
Sheet sheet = writeSheetHolder.getSheet();
|
||||
Integer rowIndex = context.getRowIndex();
|
||||
if(rowIndex!=0){
|
||||
return;
|
||||
}
|
||||
Integer columnIndex = context.getColumnIndex();
|
||||
|
||||
//下拉列表
|
||||
List<String> dropdownOptionList = dropdownOptionsMap.get(columnIndex);
|
||||
if(FuncBase.isEmpty(dropdownOptionList)){
|
||||
return;
|
||||
}
|
||||
|
||||
createDropdownList(sheet,dropdownOptionList,columnIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCellDispose(CellWriteHandlerContext context) {
|
||||
WriteCellData<?> cellData = context.getFirstCellData();
|
||||
// 清除原有单元格颜色
|
||||
WriteCellStyle writeCellStyle = cellData.getWriteCellStyle();
|
||||
writeCellStyle.setFillForegroundColor(null);
|
||||
// 重新设置自定义RGB颜色
|
||||
CellStyle cellStyle;
|
||||
int cellStyleKey;
|
||||
Integer rowIndex = context.getRowIndex();
|
||||
if (rowIndex == 0) {
|
||||
// 第0行,即表头
|
||||
cellStyleKey = 0;
|
||||
cellStyle = cellStyleMap.computeIfAbsent(cellStyleKey,
|
||||
value -> getCellStyle(cellData, context, new Color(115, 174, 76)));
|
||||
} else if ((rowIndex & 1) == 0) {
|
||||
// 偶数行
|
||||
cellStyleKey = 2;
|
||||
cellStyle = cellStyleMap.computeIfAbsent(cellStyleKey,
|
||||
value -> getCellStyle(cellData, context, new Color(255, 255, 255)));
|
||||
} else {
|
||||
// 奇数行
|
||||
cellStyleKey = 1;
|
||||
cellStyle = cellStyleMap.computeIfAbsent(cellStyleKey,
|
||||
value -> getCellStyle(cellData, context, new Color(227, 239, 219)));
|
||||
}
|
||||
cellData.setOriginCellStyle(cellStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* 单元格样式
|
||||
*/
|
||||
private CellStyle getCellStyle(WriteCellData<?> cellData, CellWriteHandlerContext context, Color color) {
|
||||
CellStyle style = cellData.getOriginCellStyle();
|
||||
if (FuncBase.isEmpty(style)) {
|
||||
style = context.getWriteWorkbookHolder().getWorkbook().createCellStyle();
|
||||
}
|
||||
XSSFColor xssfColor = new XSSFColor(color, new DefaultIndexedColorMap());
|
||||
((XSSFCellStyle) style).setFillForegroundColor(xssfColor);
|
||||
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下拉
|
||||
* @param sheet
|
||||
* @param dropdownOptionList
|
||||
*/
|
||||
public void createDropdownList( Sheet sheet, List<String> dropdownOptionList, int firstCol) {
|
||||
if(FuncBase.isEmpty(dropdownOptionList)){
|
||||
return;
|
||||
}
|
||||
|
||||
DataValidationHelper helper = sheet.getDataValidationHelper();
|
||||
|
||||
String[] dropdownOptionsArray = dropdownOptionList.toArray(new String[0]);
|
||||
|
||||
// 创建下拉列表的约束
|
||||
DataValidationConstraint constraint = helper.createExplicitListConstraint(dropdownOptionsArray);
|
||||
|
||||
// 设置下拉列表应用的单元格区域,例如第2行到最后一行的第2列
|
||||
int firstRow = 1; // 通常Excel中第一行是1,这里假设第一行为表头
|
||||
int lastRow=65536;
|
||||
int lastCol = firstCol;
|
||||
CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
|
||||
|
||||
// 创建数据验证规则并添加到工作表
|
||||
DataValidation validation = helper.createValidation(constraint, regions);
|
||||
sheet.addValidationData(validation);
|
||||
}
|
||||
|
||||
|
||||
public Map<Integer, List<String>> getDropdownOptionsMap() {
|
||||
return dropdownOptionsMap;
|
||||
}
|
||||
|
||||
public void setDropdownOptionsMap(Map<Integer, List<String>> dropdownOptionsMap) {
|
||||
this.dropdownOptionsMap = dropdownOptionsMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
|
||||
package com.jeelowcode.framework.excel.listener;
|
||||
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.event.AnalysisEventListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 自定义监听器
|
||||
*/
|
||||
public class JeeLowCodeExcelListener extends AnalysisEventListener<Map<Integer, String>> {
|
||||
//表头数据(存储所有的表头数据)
|
||||
private List<Map<Integer, String>> headList = new ArrayList<>();
|
||||
//数据体
|
||||
private List<Map<Integer, String>> dataList = new ArrayList<>();
|
||||
|
||||
|
||||
@Override//这里会一行行的返回头
|
||||
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
|
||||
//存储全部表头数据
|
||||
headList.add(headMap);
|
||||
}
|
||||
|
||||
@Override// 处理每一行数据
|
||||
public void invoke(Map<Integer, String> data, AnalysisContext context) {
|
||||
dataList.add(data);
|
||||
}
|
||||
|
||||
@Override// 全部处理结束执行
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
}
|
||||
|
||||
public List<Map<Integer, String>> getHeadList() {
|
||||
return headList;
|
||||
}
|
||||
|
||||
public List<Map<Integer, String>> getDataList() {
|
||||
return dataList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
package com.jeelowcode.framework.excel.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ExcelTitleModel {
|
||||
private String title;//标题
|
||||
private List<String> dropdownOptionList;//下拉列表
|
||||
|
||||
public ExcelTitleModel() {
|
||||
|
||||
}
|
||||
|
||||
public ExcelTitleModel(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public List<String> getDropdownOptionList() {
|
||||
return dropdownOptionList;
|
||||
}
|
||||
|
||||
public void setDropdownOptionList(List<String> dropdownOptionList) {
|
||||
this.dropdownOptionList = dropdownOptionList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.jeelowcode.framework.excel.model;
|
||||
|
||||
import org.apache.commons.collections4.map.LinkedMap;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MoreSheetConfig {
|
||||
private String sheetName;
|
||||
private LinkedMap<String, ExcelTitleModel> headMap;
|
||||
private List<Map<String, Object>> dataList;
|
||||
|
||||
public String getSheetName() {
|
||||
return sheetName;
|
||||
}
|
||||
|
||||
public void setSheetName(String sheetName) {
|
||||
this.sheetName = sheetName;
|
||||
}
|
||||
|
||||
public LinkedMap<String, ExcelTitleModel> getHeadMap() {
|
||||
return headMap;
|
||||
}
|
||||
|
||||
public void setHeadMap(LinkedMap<String, ExcelTitleModel> headMap) {
|
||||
this.headMap = headMap;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getDataList() {
|
||||
return dataList;
|
||||
}
|
||||
|
||||
public void setDataList(List<Map<String, Object>> dataList) {
|
||||
this.dataList = dataList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
|
||||
package com.jeelowcode.framework.excel.read;
|
||||
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.jeelowcode.framework.excel.listener.JeeLowCodeExcelListener;
|
||||
import com.jeelowcode.framework.exception.JeeLowCodeException;
|
||||
import com.jeelowcode.framework.utils.constant.JeeLowCodeConstant;
|
||||
import com.jeelowcode.framework.utils.utils.FuncBase;
|
||||
import com.alibaba.excel.EasyExcelFactory;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 处理导入数据
|
||||
*/
|
||||
public class ExcelImportUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 动态表头导入功能
|
||||
*
|
||||
* @param inputStream 文件
|
||||
* @return
|
||||
*/
|
||||
public static List<Map<String, Object>> importExcel(InputStream inputStream,Integer headerCou, Map<String, String> fieldNameAndCodeMap) {
|
||||
return importExcel(inputStream,headerCou,fieldNameAndCodeMap,0);
|
||||
}
|
||||
|
||||
public static List<Map<String, Object>> importExcel(InputStream inputStream,Integer headerCou, Map<String, String> fieldNameAndCodeMap,Integer sheet) {
|
||||
try {
|
||||
// 首先校验传入文件是否为空
|
||||
if (inputStream == null) {
|
||||
throw new JeeLowCodeException("文件源为空");
|
||||
}
|
||||
// 引入监听器(此处需注意,监听器不可被Spring管理)
|
||||
JeeLowCodeExcelListener readListener = new JeeLowCodeExcelListener();
|
||||
// 开始处理excel
|
||||
EasyExcelFactory.read(inputStream, readListener)
|
||||
.sheet(sheet)
|
||||
.headRowNumber(headerCou)
|
||||
.doRead();
|
||||
// 获取表头(验空)
|
||||
List<Map<Integer, String>> headList = readListener.getHeadList();
|
||||
if (FuncBase.isEmpty(headList)) {
|
||||
throw new JeeLowCodeException("Excel表头不能为空");
|
||||
}
|
||||
//获取头部,取最后一次解析的列头数据
|
||||
Map<Integer, String> excelHeadIdxNameMap = headList.get(headList.size() - 1);
|
||||
|
||||
// 获取表数据(验空)
|
||||
List<Map<Integer, String>> dataList = readListener.getDataList();
|
||||
if (FuncBase.isEmpty(dataList)) {
|
||||
throw new JeeLowCodeException("Excel数据内容不能为空");
|
||||
}
|
||||
|
||||
//封装数据体
|
||||
AtomicInteger atomicInteger= new AtomicInteger(1);
|
||||
List<Map<String, Object>> excelDataList = new ArrayList<Map<String, Object>>();
|
||||
for (Map<Integer, String> dataRow : dataList) {
|
||||
HashMap<String, Object> rowData = new HashMap<>();
|
||||
int step =atomicInteger.getAndIncrement();
|
||||
rowData.put(JeeLowCodeConstant.EXCEL_IMPORT_STEP,step);//处理序号
|
||||
rowData.put(JeeLowCodeConstant.EXCEL_IMPORT_ID,IdUtil.getSnowflakeNextId());//id
|
||||
|
||||
|
||||
excelHeadIdxNameMap.entrySet().forEach(columnHead -> {
|
||||
String value = columnHead.getValue();
|
||||
String fieldCode = fieldNameAndCodeMap.get(value);
|
||||
if(FuncBase.isNotEmpty(fieldCode)){
|
||||
rowData.put(fieldCode, dataRow.get(columnHead.getKey()));
|
||||
}
|
||||
});
|
||||
if(FuncBase.isEmpty(rowData)){
|
||||
continue;
|
||||
}
|
||||
excelDataList.add(rowData);
|
||||
}
|
||||
|
||||
return excelDataList;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new JeeLowCodeException(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
|
||||
|
||||
package com.jeelowcode.framework.excel.write;
|
||||
|
||||
import com.alibaba.excel.metadata.data.DataFormatData;
|
||||
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
|
||||
import com.alibaba.excel.write.metadata.style.WriteFont;
|
||||
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CellStyleUtils {
|
||||
|
||||
/**
|
||||
* 对行使用相同的样式<br/>
|
||||
* 头和内容分别使用不同样式
|
||||
*/
|
||||
public static HorizontalCellStyleStrategy getCellStyleStrategy(){
|
||||
// List<WriteCellStyle> contentStyleList = new ArrayList<>();// 内容奇偶数行样式不一致
|
||||
// contentStyleList.add(getOddRowContentStyle());
|
||||
// contentStyleList.add(getEvenRowContentStyle());
|
||||
return new HorizontalCellStyleStrategy(getHeadStyle(), getContentStyle());
|
||||
}
|
||||
|
||||
/**
|
||||
* 头样式
|
||||
*/
|
||||
public static WriteCellStyle getHeadStyle() {
|
||||
WriteCellStyle style = new WriteCellStyle();
|
||||
DataFormatData dataFormatData = new DataFormatData();
|
||||
dataFormatData.setFormat("@");
|
||||
style.setDataFormatData(dataFormatData);// 单元格格式
|
||||
WriteFont font = new WriteFont();
|
||||
font.setFontHeightInPoints((short) 12);
|
||||
font.setBold(false);
|
||||
font.setColor(IndexedColors.WHITE.getIndex());
|
||||
style.setWriteFont(font);// 字体
|
||||
style.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平对齐方式
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直对齐方式
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);// 边框样式
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setTopBorderColor(IndexedColors.BLACK.getIndex());// 边框颜色
|
||||
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内容样式
|
||||
*/
|
||||
public static WriteCellStyle getContentStyle() {
|
||||
WriteCellStyle style = new WriteCellStyle();
|
||||
DataFormatData dataFormatData = new DataFormatData();
|
||||
dataFormatData.setFormat("@");
|
||||
style.setDataFormatData(dataFormatData);// 单元格格式
|
||||
WriteFont font = new WriteFont();
|
||||
font.setFontHeightInPoints((short) 12);
|
||||
style.setWriteFont(font);// 字体
|
||||
style.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平对齐方式
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直对齐方式
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);// 边框样式
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setTopBorderColor(IndexedColors.BLACK.getIndex());// 边框颜色
|
||||
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 奇数行内容样式
|
||||
*/
|
||||
public static WriteCellStyle getOddRowContentStyle() {
|
||||
WriteCellStyle style = new WriteCellStyle();
|
||||
DataFormatData dataFormatData = new DataFormatData();
|
||||
dataFormatData.setFormat("@");
|
||||
style.setDataFormatData(dataFormatData);// 单元格格式
|
||||
WriteFont font = new WriteFont();
|
||||
font.setFontHeightInPoints((short) 12);
|
||||
style.setWriteFont(font);// 字体
|
||||
style.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平对齐方式
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直对齐方式
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);// 边框样式
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setTopBorderColor(IndexedColors.BLACK.getIndex());// 边框颜色
|
||||
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* 偶数行内容样式
|
||||
*/
|
||||
public static WriteCellStyle getEvenRowContentStyle() {
|
||||
WriteCellStyle style = new WriteCellStyle();
|
||||
WriteFont font = new WriteFont();
|
||||
DataFormatData dataFormatData = new DataFormatData();
|
||||
dataFormatData.setFormat("@");
|
||||
style.setDataFormatData(dataFormatData);// 文本格式
|
||||
font.setFontHeightInPoints((short) 12);
|
||||
style.setWriteFont(font);// 字体
|
||||
style.setHorizontalAlignment(HorizontalAlignment.CENTER);// 水平对齐方式
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直对齐方式
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderBottom(BorderStyle.THIN);// 边框样式
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setTopBorderColor(IndexedColors.BLACK.getIndex());// 边框颜色
|
||||
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
||||
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
||||
return style;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
|
||||
package com.jeelowcode.framework.excel.write;
|
||||
|
||||
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
|
||||
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.DataFormatter;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
|
||||
/**
|
||||
* 列宽样式策略
|
||||
*/
|
||||
public class CustomColumnWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {
|
||||
|
||||
@Override
|
||||
protected void setColumnWidth(CellWriteHandlerContext context) {
|
||||
// 只需设置首行
|
||||
if (context.getRowIndex() != 0) {
|
||||
return;
|
||||
}
|
||||
DataFormatter dataFormatter = new DataFormatter();
|
||||
Cell cell = context.getCell();
|
||||
int length = dataFormatter.formatCellValue(cell).length();
|
||||
if (length < 6) {
|
||||
length = 6;
|
||||
}
|
||||
Sheet sheet = context.getWriteSheetHolder().getSheet();
|
||||
sheet.setColumnWidth(context.getColumnIndex(), length * 1000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
|
||||
package com.jeelowcode.framework.excel.write;
|
||||
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||
import com.jeelowcode.framework.excel.handler.CustomSheetWriteHandler;
|
||||
import com.jeelowcode.framework.excel.handler.RowFormatSetTextHandler;
|
||||
import com.jeelowcode.framework.excel.model.ExcelTitleModel;
|
||||
import com.jeelowcode.framework.excel.model.MoreSheetConfig;
|
||||
import com.jeelowcode.framework.exception.JeeLowCodeException;
|
||||
import com.jeelowcode.framework.utils.utils.FuncBase;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import org.apache.commons.collections4.map.LinkedMap;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.*;
|
||||
|
||||
public class ExcelExportUtils {
|
||||
|
||||
/**
|
||||
* 封装表头信息
|
||||
* @param headMap {name:姓名} {age:年龄}
|
||||
* @return
|
||||
*/
|
||||
private static List<List<String>> getExcelHeaderList(LinkedMap<String, ExcelTitleModel> headMap){
|
||||
//处理头部
|
||||
List<List<String>> head = new ArrayList<>();
|
||||
headMap.forEach((code, model) -> {
|
||||
List<String> headList = new ArrayList<>();
|
||||
headList.add(model.getTitle());
|
||||
head.add(headList);
|
||||
});
|
||||
return head;
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装表数据
|
||||
* @param headMap
|
||||
* @param dataList {name:张三}{age:18}
|
||||
* @return
|
||||
*/
|
||||
private static List<List<Object>> getExcelDataList(LinkedMap<String, ExcelTitleModel> headMap, List<Map<String, Object>> dataList){
|
||||
List<List<Object>> data=new ArrayList<>();
|
||||
//处理数据
|
||||
dataList.stream().forEach(dataMap->{
|
||||
List<Object> tmpList=new ArrayList<>();
|
||||
headMap.forEach((code,title)->{
|
||||
Object tmpVal = dataMap.get(code);
|
||||
try{
|
||||
tmpList.add(FuncBase.json2Str(tmpVal));
|
||||
}catch (Exception e){
|
||||
tmpList.add(tmpVal);
|
||||
}
|
||||
});
|
||||
data.add(tmpList);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 动态表头导出
|
||||
* @param response
|
||||
* @param sheetName
|
||||
* @param headMap
|
||||
* @param dataList
|
||||
*/
|
||||
public static void exportExcel(HttpServletResponse response, String sheetName, LinkedMap<String, ExcelTitleModel> headMap, List<Map<String, Object>> dataList) {
|
||||
try {
|
||||
if(FuncBase.isEmpty(headMap)){
|
||||
throw new JeeLowCodeException("表头不允许为空");
|
||||
}
|
||||
|
||||
|
||||
//设置mime类型
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
//设置编码
|
||||
response.setCharacterEncoding("utf-8");
|
||||
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
||||
String outFileName = URLEncoder.encode(sheetName, "UTF-8");
|
||||
//设置响应头信息 Content-disposition
|
||||
response.setHeader("Content-disposition", "attachment;filename=" + outFileName + ".xlsx");
|
||||
|
||||
//处理头部
|
||||
List<List<String>> head =getExcelHeaderList(headMap);
|
||||
//处理表数据
|
||||
List<List<Object>> data=getExcelDataList(headMap,dataList);
|
||||
//处理下拉内容
|
||||
Map<Integer, List<String>> dropdownOptionsMap=getDropdownOptionsMap(headMap);
|
||||
|
||||
RowFormatSetTextHandler rowFormatSetTextHandler = new RowFormatSetTextHandler();
|
||||
rowFormatSetTextHandler.setDropdownOptionsMap(dropdownOptionsMap);
|
||||
EasyExcel.write(response.getOutputStream())
|
||||
.registerWriteHandler(rowFormatSetTextHandler)
|
||||
.registerWriteHandler(new CustomSheetWriteHandler())// 初始化单元格为文本格式
|
||||
.registerWriteHandler(new CustomColumnWidthStyleStrategy())// 单元格宽度
|
||||
.registerWriteHandler(CellStyleUtils.getCellStyleStrategy())// 单元格样式
|
||||
.head(head)
|
||||
.sheet(sheetName).doWrite(data);
|
||||
} catch (Exception e) {
|
||||
throw new JeeLowCodeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void exportExcel(HttpServletResponse response, String fileName,List<MoreSheetConfig> sheetConfigList) {
|
||||
try {
|
||||
if (FuncBase.isEmpty(sheetConfigList)) {
|
||||
throw new JeeLowCodeException("sheets数据不允许为空");
|
||||
}
|
||||
|
||||
// 设置mime类型
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
// 设置编码
|
||||
response.setCharacterEncoding("utf-8");
|
||||
// 防止中文乱码
|
||||
String outFileName = URLEncoder.encode(fileName, "UTF-8");
|
||||
// 设置响应头信息 Content-disposition
|
||||
response.setHeader("Content-disposition", "attachment;filename=" + outFileName + ".xlsx");
|
||||
|
||||
// 创建ExcelWriter
|
||||
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
|
||||
|
||||
// 遍历每个sheet
|
||||
for(MoreSheetConfig moreSheetConfig:sheetConfigList){
|
||||
List<Map<String, Object>> dataList = moreSheetConfig.getDataList();
|
||||
LinkedMap<String, ExcelTitleModel> headMap = moreSheetConfig.getHeadMap();
|
||||
String sheetName = moreSheetConfig.getSheetName();
|
||||
|
||||
if (FuncBase.isEmpty(headMap)) {
|
||||
continue; // 跳过空表头的sheet
|
||||
}
|
||||
|
||||
// 处理头部
|
||||
List<List<String>> head = getExcelHeaderList(headMap);
|
||||
// 处理表数据
|
||||
List<List<Object>> data = getExcelDataList(headMap, dataList);
|
||||
// 处理下拉内容
|
||||
Map<Integer, List<String>> dropdownOptionsMap = getDropdownOptionsMap(headMap);
|
||||
|
||||
RowFormatSetTextHandler rowFormatSetTextHandler = new RowFormatSetTextHandler();
|
||||
rowFormatSetTextHandler.setDropdownOptionsMap(dropdownOptionsMap);
|
||||
|
||||
// 创建WriteSheet
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet(sheetName)
|
||||
.head(head)
|
||||
.registerWriteHandler(rowFormatSetTextHandler)
|
||||
.registerWriteHandler(new CustomSheetWriteHandler())
|
||||
.registerWriteHandler(new CustomColumnWidthStyleStrategy())
|
||||
.registerWriteHandler(CellStyleUtils.getCellStyleStrategy())
|
||||
.build();
|
||||
// 写入数据
|
||||
excelWriter.write(data, writeSheet);
|
||||
}
|
||||
|
||||
// 关闭ExcelWriter
|
||||
excelWriter.finish();
|
||||
} catch (Exception e) {
|
||||
throw new JeeLowCodeException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理下拉列表
|
||||
* @param headMap
|
||||
* @return
|
||||
*/
|
||||
private static Map<Integer, List<String>> getDropdownOptionsMap(LinkedMap<String, ExcelTitleModel> headMap){
|
||||
Map<Integer, List<String>> dropdownOptionsMap=new HashMap<>();
|
||||
|
||||
int index = 0; // 初始化序号变量
|
||||
Iterator<Map.Entry<String, ExcelTitleModel>> iterator = headMap.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
|
||||
Map.Entry<String, ExcelTitleModel> entry = iterator.next();
|
||||
ExcelTitleModel model = entry.getValue();
|
||||
List<String> dropdownOptionList = model.getDropdownOptionList();
|
||||
if(FuncBase.isNotEmpty(dropdownOptionList)){
|
||||
dropdownOptionsMap.put(index,dropdownOptionList);
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return dropdownOptionsMap;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user