diff --git a/pom.xml b/pom.xml index 64e83b1..348d3ae 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,4 @@ - 4.0.0 @@ -56,8 +56,8 @@ ${lombok.version} provided - - + + junit @@ -65,7 +65,20 @@ 4.12 - + + + + com.alibaba + easyexcel + 2.2.0-beta1 + + + org.slf4j + slf4j-api + + + + diff --git a/src/test/java/youhong/ai/pcn/TestOrganization.java b/src/test/java/youhong/ai/pcn/TestOrganization.java index 65e2711..5a4e797 100644 --- a/src/test/java/youhong/ai/pcn/TestOrganization.java +++ b/src/test/java/youhong/ai/pcn/TestOrganization.java @@ -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> buildList() { + List> list = new ArrayList<>(); + list.add(new HashMap() {{ + put("addr", "北京"); + put("kinds", "生活用品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "北京"); + put("kinds", "生活用品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "北京"); + put("kinds", "电子产品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "北京"); + put("kinds", "电子产品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "上海"); + put("kinds", "生活用品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "上海"); + put("kinds", "生活用品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "上海"); + put("kinds", "电子商品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "云南"); + put("kinds", "生活用品"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "云南"); + put("kinds", "电子铲平"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + put("addr", "云南"); + put("kinds", "蘑菇"); + put("price", 10); + put("num", 80); + }}); + list.add(new HashMap() {{ + 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 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 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); + } + } + } +} +