|
@@ -126,7 +126,6 @@ public class HtmlTableToExcelConverter {
|
|
|
|
|
|
// 获取单元格内容
|
|
|
String cellText = extractCellText(td);
|
|
|
-// String cellText = extractCellTextWithFormElements(td);
|
|
|
|
|
|
// 创建单元格
|
|
|
Cell cell = excelRow.createCell(excelColNum);
|
|
@@ -421,10 +420,20 @@ public class HtmlTableToExcelConverter {
|
|
|
|
|
|
// 辅助方法:提取单元格文本内容
|
|
|
private static String extractCellText(Element td) {
|
|
|
+ //复选框
|
|
|
+ Elements select = td.select("hc-form-checkbox-group");
|
|
|
+ if(select.size() > 0){
|
|
|
+ Element element = select.get(0);
|
|
|
+ return "□ " + element.attr("placeholder");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
// 优先获取直接文本内容
|
|
|
if (!td.ownText().isEmpty()) {
|
|
|
return td.ownText().trim();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
return "";
|
|
|
}
|
|
|
|
|
@@ -671,80 +680,4 @@ public class HtmlTableToExcelConverter {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 扩展方法:提取单元格文本并处理换行和表单元素
|
|
|
- private static String extractCellTextWithFormElements(Element td) {
|
|
|
- // 使用自定义NodeVisitor处理文本、换行和表单元素
|
|
|
- FormElementVisitor visitor = new FormElementVisitor();
|
|
|
- NodeTraversor traversor = new NodeTraversor(visitor);
|
|
|
- traversor.traverse(td);
|
|
|
- return visitor.getText().trim();
|
|
|
- }
|
|
|
-
|
|
|
- // 自定义NodeVisitor处理文本节点、<br>标签和表单元素
|
|
|
- private static class FormElementVisitor implements NodeVisitor {
|
|
|
- private final StringBuilder text = new StringBuilder();
|
|
|
- private boolean lastWasBr = false;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void head(Node node, int depth) {
|
|
|
- if (node instanceof TextNode) {
|
|
|
- text.append(((TextNode) node).text());
|
|
|
- lastWasBr = false;
|
|
|
- } else if (node instanceof Element) {
|
|
|
- Element el = (Element) node;
|
|
|
- String tagName = el.tagName().toLowerCase();
|
|
|
-
|
|
|
- // 处理换行
|
|
|
- if ("br".equals(tagName)) {
|
|
|
- if (!lastWasBr) {
|
|
|
- text.append("\n");
|
|
|
- }
|
|
|
- lastWasBr = true;
|
|
|
- }
|
|
|
- // 处理单选框
|
|
|
- else if ("input".equals(tagName) && "radio".equalsIgnoreCase(el.attr("type"))) {
|
|
|
- text.append(el.hasAttr("checked") ? "✓" : "□");
|
|
|
- lastWasBr = false;
|
|
|
- }
|
|
|
- // 处理复选框
|
|
|
- else if ("input".equals(tagName) && "checkbox".equalsIgnoreCase(el.attr("type"))) {
|
|
|
- text.append(el.hasAttr("checked") ? "✓" : "□");
|
|
|
- lastWasBr = false;
|
|
|
- }
|
|
|
- // 处理下拉选择框
|
|
|
- else if ("select".equals(tagName)) {
|
|
|
- Element selectedOption = el.select("option[selected]").first();
|
|
|
- if (selectedOption != null) {
|
|
|
- text.append(selectedOption.text());
|
|
|
- } else {
|
|
|
- Element firstOption = el.select("option").first();
|
|
|
- if (firstOption != null) {
|
|
|
- text.append(firstOption.text());
|
|
|
- }
|
|
|
- }
|
|
|
- lastWasBr = false;
|
|
|
- }
|
|
|
- // 处理文本输入框和文本区域
|
|
|
- else if (("input".equals(tagName) && "text".equalsIgnoreCase(el.attr("type")) ||
|
|
|
- "textarea".equals(tagName))) {
|
|
|
- String value = el.hasAttr("value") ? el.attr("value") : el.text();
|
|
|
- if (value.isEmpty() && el.hasAttr("placeholder")) {
|
|
|
- value = el.attr("placeholder");
|
|
|
- }
|
|
|
- text.append(value);
|
|
|
- lastWasBr = false;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void tail(Node node, int depth) {
|
|
|
- // 不需要处理
|
|
|
- }
|
|
|
-
|
|
|
- public String getText() {
|
|
|
- return text.toString();
|
|
|
- }
|
|
|
- }
|
|
|
}
|