|
@@ -0,0 +1,185 @@
|
|
|
+package com.platform.office.poi.excel;
|
|
|
+
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+import com.platform.office.vo.PoiModel;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+ * Created by zelei.fan on 2017/3/14.
|
|
|
+ */
|
|
|
+public class ExcelMergeExportUtil {
|
|
|
+
|
|
|
+
|
|
|
+ * @param title 标题集合 tilte的长度应该与list中的model的属性个数一致
|
|
|
+ * @param maps 内容集合
|
|
|
+ * @param mergeIndex 合并单元格的列
|
|
|
+ */
|
|
|
+ public static String createExcel(String[] title, Map<String, List<Map<String, String>>> maps, int[] mergeIndex) {
|
|
|
+ if (title.length == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Workbook workbook = new XSSFWorkbook();
|
|
|
+ Sheet sheet = null;
|
|
|
+ int n = 0;
|
|
|
+
|
|
|
+ for (Map.Entry<String, List<Map<String, String>>> entry : maps.entrySet()) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ sheet = workbook.createSheet();
|
|
|
+ workbook.setSheetName(n, entry.getKey());
|
|
|
+ workbook.setSelectedTab(0);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ Row row0 = sheet.createRow(0);
|
|
|
+ for (int i = 0; i < title.length; i++) {
|
|
|
+
|
|
|
+ Cell cell_1 = row0.createCell(i, Cell.CELL_TYPE_STRING);
|
|
|
+ cell_1.setCellValue(title[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, String>> list = entry.getValue();
|
|
|
+
|
|
|
+ List<PoiModel> poiModels = Lists.newArrayList();
|
|
|
+ if (null != workbook) {
|
|
|
+ Iterator iterator = list.iterator();
|
|
|
+ int index = 1;
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ Row row = sheet.createRow(index);
|
|
|
+
|
|
|
+ Map<String, String> map = (Map<String, String>) iterator.next();
|
|
|
+
|
|
|
+ for (int i = 0; i < title.length; i++) {
|
|
|
+ String old = "";
|
|
|
+
|
|
|
+ if (index > 1) {
|
|
|
+ old = poiModels.get(i) == null ? "" : poiModels.get(i).getContent();
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int j = 0; j < mergeIndex.length; j++) {
|
|
|
+ if (index == 1) {
|
|
|
+
|
|
|
+ PoiModel poiModel = new PoiModel();
|
|
|
+ poiModel.setOldContent(map.get(title[i]));
|
|
|
+ poiModel.setContent(map.get(title[i]));
|
|
|
+ poiModel.setRowIndex(1);
|
|
|
+ poiModel.setCellIndex(i);
|
|
|
+ poiModels.add(poiModel);
|
|
|
+ break;
|
|
|
+ } else if (i > 0 && mergeIndex[j] == i) {
|
|
|
+
|
|
|
+
|
|
|
+ if (!poiModels.get(i).getContent().equals(map.get(title[i])) || poiModels.get(i).getContent().equals(map.get(title[i])) && !poiModels.get(i - 1).getOldContent().equals(map.get(title[i - 1]))) {
|
|
|
+
|
|
|
+ CellRangeAddress cra = new CellRangeAddress(poiModels.get(i).getRowIndex(), index - 1, poiModels.get(i).getCellIndex(), poiModels.get(i).getCellIndex());
|
|
|
+
|
|
|
+ sheet.addMergedRegion(cra);
|
|
|
+
|
|
|
+ poiModels.get(i).setContent(map.get(title[i]));
|
|
|
+ poiModels.get(i).setRowIndex(index);
|
|
|
+ poiModels.get(i).setCellIndex(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mergeIndex[j] == i && i == 0 && !poiModels.get(i).getContent().equals(map.get(title[i]))) {
|
|
|
+
|
|
|
+ CellRangeAddress cra = new CellRangeAddress(poiModels.get(i).getRowIndex(), index - 1, poiModels.get(i).getCellIndex(), poiModels.get(i).getCellIndex());
|
|
|
+
|
|
|
+ sheet.addMergedRegion(cra);
|
|
|
+
|
|
|
+ poiModels.get(i).setContent(map.get(title[i]));
|
|
|
+ poiModels.get(i).setRowIndex(index);
|
|
|
+ poiModels.get(i).setCellIndex(i);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (mergeIndex[j] == i && index == list.size()) {
|
|
|
+ CellRangeAddress cra = new CellRangeAddress(poiModels.get(i).getRowIndex(), index, poiModels.get(i).getCellIndex(), poiModels.get(i).getCellIndex());
|
|
|
+
|
|
|
+ sheet.addMergedRegion(cra);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
|
|
|
+ cell.setCellValue(map.get(title[i]));
|
|
|
+
|
|
|
+ poiModels.get(i).setOldContent(old);
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ n++;
|
|
|
+ }
|
|
|
+
|
|
|
+ FileOutputStream out = null;
|
|
|
+ String localPath = null;
|
|
|
+ File tempFile = null;
|
|
|
+ String fileName = String.valueOf(new Date().getTime() / 1000);
|
|
|
+ try {
|
|
|
+ tempFile = File.createTempFile(fileName, ".xlsx");
|
|
|
+ localPath = tempFile.getAbsolutePath();
|
|
|
+ out = new FileOutputStream(localPath);
|
|
|
+ workbook.write(out);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return localPath;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+
|
|
|
+ String[] title = {"id", "标题", "描述", "负责人", "开始时间"};
|
|
|
+ List<Map<String, String>> list = Lists.newArrayList();
|
|
|
+
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
+ HashMap<String, String> map = com.google.common.collect.Maps.newHashMap();
|
|
|
+ if (i > 5) {
|
|
|
+ if (i < 7) {
|
|
|
+ map.put("id", "333");
|
|
|
+ map.put("标题", "mmmm");
|
|
|
+ } else {
|
|
|
+ map.put("id", "333");
|
|
|
+ map.put("标题", "aaaaa");
|
|
|
+ }
|
|
|
+ } else if (i > 3) {
|
|
|
+ map.put("id", "222");
|
|
|
+ map.put("标题", "哈哈哈哈");
|
|
|
+ } else if (i > 1 && i < 3) {
|
|
|
+ map.put("id", "222");
|
|
|
+ map.put("标题", "hhhhhhhh");
|
|
|
+ } else {
|
|
|
+ map.put("id", "222");
|
|
|
+ map.put("标题", "bbbb");
|
|
|
+ }
|
|
|
+ map.put("描述", "sssssss");
|
|
|
+ map.put("负责人", "vvvvv");
|
|
|
+ map.put("开始时间", "2017-02-27 11:20:26");
|
|
|
+ list.add(map);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Map<String, List<Map<String, String>>> map = new HashMap<>();
|
|
|
+ map.put("测试合并数据", list);
|
|
|
+ System.out.println(createExcel(title, map, new int[]{0, 1, 2}));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|