添加easyexcel依赖,编写excel单元格合并测试代码,分析可行性

main
youHong.ai 2022-12-10 12:24:55 +08:00
parent b7af6ffe34
commit 7975f9b797
2 changed files with 216 additions and 8 deletions

15
pom.xml
View File

@ -1,4 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<!--指定了当前pom的版本4.0.0是固定的 --> <!--指定了当前pom的版本4.0.0是固定的 -->
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
@ -66,6 +66,19 @@
</dependency> </dependency>
<!-- easy excel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.0-beta1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </dependencies>

View File

@ -4,6 +4,14 @@ import aiyh.utils.httpUtil.HttpMultipartFile;
import aiyh.utils.httpUtil.ResponeVo; import aiyh.utils.httpUtil.ResponeVo;
import aiyh.utils.httpUtil.util.HttpUtils; import aiyh.utils.httpUtil.util.HttpUtils;
import basetest.BaseTest; import basetest.BaseTest;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.api.youhong.ai.pcn.organization.orgchart.service.OrgChartService; import com.api.youhong.ai.pcn.organization.orgchart.service.OrgChartService;
import com.api.youhong.ai.pcn.organization.orgchart.vo.OrgChartNodeVo; import com.api.youhong.ai.pcn.organization.orgchart.vo.OrgChartNodeVo;
@ -14,6 +22,11 @@ import org.apache.http.Header;
import org.apache.http.HeaderElement; import org.apache.http.HeaderElement;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.junit.Test; import org.junit.Test;
import weaver.conn.RecordSet; import weaver.conn.RecordSet;
import weaver.formmode.interfaces.action.WorkflowToMode; import weaver.formmode.interfaces.action.WorkflowToMode;
@ -30,10 +43,7 @@ import weaver.youhong.ai.pcn.hrorganization.wesmat.model.Employee;
import weaver.youhong.ai.pcn.hrorganization.wesmat.model.Position; import weaver.youhong.ai.pcn.hrorganization.wesmat.model.Position;
import weaver.youhong.ai.pcn.hrorganization.wesmat.result.GetOrganizationResult; import weaver.youhong.ai.pcn.hrorganization.wesmat.result.GetOrganizationResult;
import java.io.BufferedReader; import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -252,4 +262,189 @@ public class TestOrganization extends BaseTest {
System.out.println(requestManager.getMessagecontent()); System.out.println(requestManager.getMessagecontent());
System.out.println(execute); System.out.println(execute);
} }
@Test
public void testEasyExcel() throws FileNotFoundException {
String tempPath = "/Users/aoey.oct.22/company/Fan_wei/test_file/muban.xlsx";
String filePath = "/Users/aoey.oct.22/company/Fan_wei/test_file/muban1.xlsx";
FileOutputStream outputStream = new FileOutputStream(filePath);
int[] mergeColumeIndex = {0, 1};
int mergeRowIndex = 1;
ExcelWriter excelWriter = EasyExcel.write(outputStream)
.withTemplate(tempPath)
.registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumeIndex))
.build();
WriteSheet sheet = EasyExcel.writerSheet().build();
excelWriter.fill(buildList(), sheet);
excelWriter.finish();
try {
outputStream.close();
} catch (IOException e) {
}
}
public List<Map<String, Object>> buildList() {
List<Map<String, Object>> list = new ArrayList<>();
list.add(new HashMap<String, Object>() {{
put("addr", "北京");
put("kinds", "生活用品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "北京");
put("kinds", "生活用品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "北京");
put("kinds", "电子产品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "北京");
put("kinds", "电子产品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "上海");
put("kinds", "生活用品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "上海");
put("kinds", "生活用品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "上海");
put("kinds", "电子商品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "云南");
put("kinds", "生活用品");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "云南");
put("kinds", "电子铲平");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "云南");
put("kinds", "蘑菇");
put("price", 10);
put("num", 80);
}});
list.add(new HashMap<String, Object>() {{
put("addr", "云南");
put("kinds", "蘑菇");
put("price", 10);
put("num", 80);
}});
return list;
}
} }
class ExcelFillCellMergeStrategy implements CellWriteHandler {
/** 需要进行单元格合并的列数组 **/
private int[] mergeColumnIndex;
/** 单元格合并从第几行开始 **/
private int mergeRowIndex;
public ExcelFillCellMergeStrategy() {}
public ExcelFillCellMergeStrategy(int mergeRowIndex, int[] mergeColumnIndex) {
this.mergeRowIndex = mergeRowIndex;
this.mergeColumnIndex = mergeColumnIndex;
}
@Override
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
}
@Override
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
}
@Override
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
List<CellData> list, Cell cell, Head head, Integer integer, Boolean isHead) {
int curRowIndex = cell.getRowIndex();
int curColIndex = cell.getColumnIndex();
if (curRowIndex > mergeRowIndex) {
for (int i = 0; i < mergeColumnIndex.length; i++) {
if (curColIndex == mergeColumnIndex[i]) {
mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
break;
}
}
}
}
/**
*
*
* @param writeSheetHolder
* @param cell
* @param curRowIndex
* @param curColIndex
*/
private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
Object curData =
cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
Sheet sheet1 = cell.getSheet();
Row row = sheet1.getRow(curRowIndex - 1);
if (row == null) {
row = sheet1.getRow(curRowIndex);
}
Cell preCell = row.getCell(curColIndex);
Object preData =
preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
// 将当前单元格数据与上一个单元格数据比较
Boolean dataBool = preData.equals(curData);
if (dataBool) {
Sheet sheet = writeSheetHolder.getSheet();
List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
boolean isMerged = false;
for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
CellRangeAddress cellRangeAddr = mergeRegions.get(i);
// 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
sheet.removeMergedRegion(i);
cellRangeAddr.setLastRow(curRowIndex);
sheet.addMergedRegion(cellRangeAddr);
isMerged = true;
}
}
// 若上一个单元格未被合并,则新增合并单元
if (!isMerged) {
CellRangeAddress cellRangeAddress =
new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
sheet.addMergedRegion(cellRangeAddress);
}
}
}
}