فهرست منبع

完成合并单元格的处理

CodeLife Leno 1 ماه پیش
والد
کامیت
f6b71798f4

+ 0 - 4
Procedure/backend/project/src/main/java/com/sundata/product/rwa/util/ExcelUtil.java

@@ -1,4 +0,0 @@
-package com.sundata.product.rwa.util;
-
-public class ExcelUtil {
-}

+ 22 - 1
Procedure/backend/project/src/main/java/com/sundata/product/rwa/util/html/ToHtml.java

@@ -23,12 +23,14 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.format.CellFormat;
 import org.apache.poi.ss.format.CellFormatResult;
 import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 import java.io.*;
 import java.nio.charset.Charset;
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * This example shows how to display a spreadsheet in HTML using the classes for
@@ -435,16 +437,26 @@ public final class ToHtml {
 
         out.format("<tbody>%n");
         Iterator<Row> rows = sheet.rowIterator();
+        List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
+
+//        Map<Integer, Map<Integer,CellRangeAddress>> row2CellMergedRegions = new TreeMap<>();
+
+
         while (rows.hasNext()) {
             Row row = rows.next();
 
             out.format("  <tr>%n");
             out.format("    <td class=%s>%d</td>%n", ROW_HEAD_CLASS,
                     row.getRowNum() + 1);
+
+            List<CellRangeAddress> thisRowCellRange = mergedRegions.stream().filter(e-> e.getFirstRow() == row.getRowNum()).toList();
+
+
             for (int i = firstColumn; i < endColumn; i++) {
                 String content = "&nbsp;";
                 String attrs = "";
                 CellStyle style = null;
+                List<CellRangeAddress> thisCellRange = new ArrayList<>();
                 if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                     Cell cell = row.getCell(i);
                     if (cell != null) {
@@ -460,9 +472,18 @@ public final class ToHtml {
                             content = "&nbsp;";
                         }
                     }
+                    thisCellRange = thisRowCellRange.stream().filter(e->e.getFirstColumn() == cell.getColumnIndex()).toList();
                 }
-                out.format("    <td class=%s %s>%s</td>%n", styleName(style),
+
+                if (thisCellRange.isEmpty()) {
+                    out.format("    <td class=%s %s>%s</td>%n", styleName(style),
+                        attrs, content);
+                }else{
+                    CellRangeAddress addresses = thisCellRange.get(0);
+                    out.format("    <td colspan=\"%d\" rowspan=\"%d\" class=%s %s>%s</td>%n",(addresses.getLastColumn()-addresses.getFirstColumn()),(addresses.getLastRow()-addresses.getFirstRow()), styleName(style),
                         attrs, content);
+                }
+
             }
             out.format("  </tr>%n");
         }