Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Wed Sep 21, 2022 3:26 am
现在功能已经满足了,但是现在存在性能问题(版本10.9.0 jdk 1.8.0)
经测试,生成的word 中 表格 数据 1条 大概400ms左右 、生成的word 中 表格 数据 67条 大概6s左右
部分测试代码
- Code: Select all
System.out.println(DateUtil.currentSeconds()+"---2");
// 添加空白行
section.addParagraph();
section.addParagraph();
// 添加文字
Paragraph paragraph2 = section.addParagraph();
paragraph2.appendText("哈哈 -- 嗯嗯");
ParagraphStyle style2 = new ParagraphStyle(document);
style2.setName("bottomStyle");
style2.getCharacterFormat().setFontName("宋体");
style2.getCharacterFormat().setBold(true);
document.getStyles().add(style2);
paragraph1.applyStyle("bottomStyle");
//保存文档
document.saveToFile(fwFileTemp, FileFormat.Docx_2013);
System.out.println(DateUtil.currentSeconds()+"---3");
这是测试代码的日志
- Code: Select all
1663730211---1
1663730211---2
1663730216---3
可以看到时间都花费在生成文件上了
-

whgao116
-
- Posts: 14
- Joined: Mon Sep 19, 2022 2:42 am
Wed Sep 21, 2022 8:09 am
Hello,
感谢您的询问。
在您提供的代码中没有生成表格的代码块。为了方便我们更准确地调查您的问题,麻烦提供下你完整的代码。提前感谢!
Sincerely,
Simple
E-iceblue support team
-


Simple.Li
-
- Posts: 248
- Joined: Fri Jul 01, 2022 2:33 am
Wed Sep 21, 2022 8:37 am
你好,这是完整测试代码,劳烦看下
- Code: Select all
public class CreateTable {
public static void main(String[] args) throws IOException {
System.out.println(DateUtil.currentSeconds()+"---1");
//创建Word文档
Document document = new Document();
//添加一个section
Section section = document.addSection();
//数据
// String[] header = {"姓名", "性别", "部门", "工号"};
String[][] data =
{
new String[]{"姓名", "性别", "部门", "工号"},
new String[]{"Winny", "女", "综合", "0109"},
new String[]{"Lois", "女", "综合", "0111"},
new String[]{"Jois", "男", "技术", "0110"}
};
// 添加文字
Paragraph paragraph1 = section.addParagraph();
paragraph1.appendText("单位:元");
ParagraphStyle style1 = new ParagraphStyle(document);
style1.setName("unitStyle");
style1.getCharacterFormat().setBold(true);
document.getStyles().add(style1);
paragraph1.applyStyle("unitStyle");
paragraph1.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);
//添加表格
Table table = section.addTable(true);
//设置表格的行数和列数
table.resetCells(data.length + 1, 4);
//将表格添加到文档中
section.getTables().add(table);
//设置表格是否跨页断行
table.getTableFormat().isBreakAcrossPages(false);
// 固定列宽
table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths);
//设置表格宽度
table.setPreferredWidth(new PreferredWidth(WidthType.Percentage, (short) 100));
//添加数据到剩余行
for (int r = 0; r < data.length; r++) {
TableRow dataRow = table.getRows().get(r);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.getRowFormat().setBackColor(Color.white);
for (int c = 0; c < data[r].length; c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
range2.getCharacterFormat().setFontName("Arial");
range2.getCharacterFormat().setFontSize(10f);
}
}
System.out.println(DateUtil.currentSeconds()+"---2");
// 添加文字
Paragraph paragraph2 = section.addParagraph();
paragraph2.appendText("哈哈: 嗯嗯:");
ParagraphStyle style2 = new ParagraphStyle(document);
style2.setName("bottomStyle");
style2.getCharacterFormat().setBold(true);
document.getStyles().add(style2);
paragraph1.applyStyle("bottomStyle");
//保存文档
document.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
System.out.println(DateUtil.currentSeconds()+"---3");
}
}
这是日志输出
- Code: Select all
1663749162---1
1663749167---2
1663749168---3
虽然数据量较小,担时间上还是挺长的。实际字段数为13,条数也比较多
-

whgao116
-
- Posts: 14
- Joined: Mon Sep 19, 2022 2:42 am
Thu Sep 22, 2022 9:24 am
Hello,
您好!感谢你的来信.
我使用你的代码测试创建100条表数据仅用了1秒左右。我将我项目可执行jar包提供你。你直接java -jar 运行看结果如何,如果你那边测试依然很慢,为了方便我们进一步调查,请提供给你的测试系统环境(E.g.win10,x64,RAM:16GB)我们进一步调查。
https://www.e-iceblue.com/downloads/demo/30858Demo.zipSincerely,
Simple
E-iceblue support team
-


Simple.Li
-
- Posts: 248
- Joined: Fri Jul 01, 2022 2:33 am
Wed Sep 28, 2022 8:58 am
Hello,
你能告诉我们你的问题进展如何吗?提前感谢您的反馈。
Sincerely,
Simple
E-iceblue support team
-


Simple.Li
-
- Posts: 248
- Joined: Fri Jul 01, 2022 2:33 am
Wed Sep 28, 2022 9:18 am
后来测试没有问题了,感谢

-

whgao116
-
- Posts: 14
- Joined: Mon Sep 19, 2022 2:42 am
Wed Sep 28, 2022 9:25 am
Hello,
感谢您的回复。
如果您还有其它问题,请随时与我们联系。
Sincerely,
Simple
E-iceblue support team
-


Simple.Li
-
- Posts: 248
- Joined: Fri Jul 01, 2022 2:33 am