Skip to content

Commit

Permalink
fixes #57 Support colgroup rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
draco1023 committed Oct 18, 2022
1 parent 607886e commit 3b38784
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/org/ddr/poi/html/HtmlConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public interface HtmlConstants {
String ATTR_SRC = "src";
String ATTR_WIDTH = "width";
String ATTR_HEIGHT = "height";
String ATTR_SPAN = "span";
String ATTR_ROWSPAN = "rowspan";
String ATTR_COLSPAN = "colspan";
String ATTR_HREF = "href";
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/org/ddr/poi/html/tag/TableRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.ddr.poi.html.tag;

import com.steadystate.css.dom.CSSStyleDeclarationImpl;
import com.steadystate.css.dom.Property;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFTable;
Expand Down Expand Up @@ -46,8 +47,11 @@
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

Expand Down Expand Up @@ -84,9 +88,18 @@ public boolean renderStart(Element element, HtmlRenderContext context) {
// Element caption = JsoupUtils.firstChild(element, HtmlConstants.TAG_CAPTION);

Element colgroup = JsoupUtils.firstChild(element, HtmlConstants.TAG_COLGROUP);
List<CSSStyleDeclarationImpl> columnStyles = Collections.emptyList();
if (colgroup != null) {
Elements cols = colgroup.select(HtmlConstants.TAG_COL);
// FIXME col可以拥有style
columnStyles = new ArrayList<>();
for (Element col : cols) {
String style = col.attr(HtmlConstants.ATTR_STYLE);
CSSStyleDeclarationImpl cssStyleDeclaration = CSSStyleUtils.parse(style);
int span = NumberUtils.toInt(col.attr(HtmlConstants.ATTR_SPAN), 1);
for (int i = 0; i < span; i++) {
columnStyles.add(cssStyleDeclaration);
}
}
colgroup.remove();
}
Elements trs = JsoupUtils.childRows(element);
Expand Down Expand Up @@ -151,6 +164,15 @@ public boolean renderStart(Element element, HtmlRenderContext context) {
ctTcPr.addNewGridSpan().setVal(BigInteger.valueOf(colspan));
}

// 列定义的样式与单元格的样式合并
if (!columnStyles.isEmpty() && columnIndex < columnStyles.size()) {
List<Property> properties = columnStyles.get(columnIndex).getProperties();
if (!properties.isEmpty()) {
tdStyleDeclaration.getProperties().addAll(0, properties);
td.attr(HtmlConstants.ATTR_STYLE, tdStyleDeclaration.getCssText());
}
}

columnIndex += colspan;
}

Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/2.html
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,12 @@ <h1><big><u><em><strong>威风威风文&nbsp; &nbsp;<big><u><em><strong><big><u>
<h1 class="ui header">java生成word的几种方案</h1>
<table cellspacing="0" class="Table"
style="border-collapse:collapse; border:none; font-family:&quot;Times New Roman&quot;; font-size:13px">
<colgroup>
<col/>
<col/>
<col span="2" style="background-color: red"/>
<col span="2" style="font-weight: bolder; background-color: yellow"/>
</colgroup>
<tbody>
<tr>
<td rowspan="2"
Expand Down

0 comments on commit 3b38784

Please sign in to comment.