添加easyexcel依赖,编写excel单元格合并测试代码,分析可行性
parent
b7af6ffe34
commit
7975f9b797
21
pom.xml
21
pom.xml
|
@ -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">
|
||||
<!--指定了当前pom的版本,4.0.0是固定的 -->
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
@ -56,8 +56,8 @@
|
|||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
<!-- 单元测试-->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -65,7 +65,20 @@
|
|||
<version>4.12</version>
|
||||
</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>
|
||||
|
||||
|
|
|
@ -4,6 +4,14 @@ import aiyh.utils.httpUtil.HttpMultipartFile;
|
|||
import aiyh.utils.httpUtil.ResponeVo;
|
||||
import aiyh.utils.httpUtil.util.HttpUtils;
|
||||
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.api.youhong.ai.pcn.organization.orgchart.service.OrgChartService;
|
||||
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.HttpResponse;
|
||||
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 weaver.conn.RecordSet;
|
||||
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.result.GetOrganizationResult;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -252,4 +262,189 @@ public class TestOrganization extends BaseTest {
|
|||
System.out.println(requestManager.getMessagecontent());
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue