Skip to content

Commit

Permalink
fixes #9 and bump version to v0.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
draco1023 committed Jun 18, 2021
1 parent 3b06626 commit 232d1c5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<dependency>
<groupId>io.github.draco1023</groupId>
<artifactId>poi-tl-ext</artifactId>
<version>0.3.2</version>
<version>0.3.3</version>
</dependency>
```

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/ddr/poi/html/HtmlRenderPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import com.steadystate.css.dom.Property;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.xwpf.usermodel.BodyElementType;
import org.apache.poi.xwpf.usermodel.BodyType;
import org.apache.poi.xwpf.usermodel.IBody;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.xmlbeans.XmlCursor;
import org.ddr.poi.html.tag.ARenderer;
import org.ddr.poi.html.tag.BigRenderer;
Expand Down Expand Up @@ -68,6 +71,7 @@
import java.io.StringReader;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -242,6 +246,14 @@ private void renderElement(Element element, HtmlRenderContext context) {
@Override
protected void afterRender(RenderContext<String> context) {
clearPlaceholder(context, true);
IBody container = context.getContainer();
if (container.getPartType() == BodyType.TABLECELL) {
// 单元格的最后一个元素应为p,否则可能无法正常打开文件
List<IBodyElement> bodyElements = container.getBodyElements();
if (bodyElements.isEmpty() || bodyElements.get(bodyElements.size() - 1).getElementType() != BodyElementType.PARAGRAPH) {
((XWPFTableCell) container).addParagraph();
}
}
}

private CSSStyleDeclarationImpl getCssStyleDeclaration(Element element) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ddr/poi/html/tag/TableRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean renderStart(Element element, HtmlRenderContext context) {
CSSLength width = CSSLength.of(widthDeclaration);
boolean explicitWidth = width.isValid() && !width.isPercent();
int tableWidth = context.computeLengthInEMU(widthDeclaration, styleDeclaration.getMaxWidth(), containerWidth, containerWidth);
int originWidth = width.isPercent() ? tableWidth : context.lengthToEMU(width);
int originWidth = !width.isValid() || width.isPercent() ? tableWidth : context.lengthToEMU(width);

// FIXME poi不支持设置tblCaption
// Element caption = JsoupUtils.firstChild(element, HtmlConstants.TAG_CAPTION);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ddr/poi/html/util/CSSLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public CSSLengthUnit getUnit() {

@Override
public String toString() {
return String.format("%.2f", value) + unit.getLiteral();
return String.format("%.2f", value) + (unit == null ? "" : unit.getLiteral());
}

/**
Expand Down

0 comments on commit 232d1c5

Please sign in to comment.