修改测试类

main
jiacheng.deng 2022-12-16 15:13:09 +08:00
parent 8c9a17a653
commit d0c7e50ec4
1 changed files with 7 additions and 195 deletions

View File

@ -5,14 +5,6 @@ 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;
@ -24,6 +16,9 @@ import weaver.conn.RecordSet;
import weaver.formmode.interfaces.action.WorkflowToMode;
import weaver.general.GCONST;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestManager;
import weaver.xiao.commons.config.entity.MultipartFile;
import weaver.xiao.commons.config.entity.RequestMappingConfig;
import weaver.xiao.commons.config.service.DealWithMapping;
@ -32,7 +27,10 @@ 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.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -267,191 +265,5 @@ public class TestOrganization extends BaseTest {
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();
// 获取当前shert表
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);
}
}
}
}