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