微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

需要使用Aspose

如何解决需要使用Aspose

我一直在使用Aspose.Words.jdk15.jar打印.doc文件的旧版应用程序上工作。我有一个要求,我要获取值列表,然后我们必须循环并将其打印在doc文件中。

然后我们使用range.replace()方法在文档中替换该值。该文档已经存在于我的工作空间中,在该工作空间中我们已经将值映射为这样

组件名称:$ COMPONENT_NAME 帐单生效日期:$ EFFECTIVE_DATE 帐单结束日期:$ END_DATE

和我编写的用于替换doc的代码。因此,我的要求是我需要根据列表大小在文档中多次使用此值。

for(int i = 0; i

doc.getRange()。replace(“ $ COMPONENT_NAME”,checkNull(details.get(i).getComponentName())+“,”,false,false); doc.getRange()。replace(“ $ EFFECTIVE_DATE”,checkNull(details.get(i).getBillEffectiveDate())+“,”,false,false); doc.getRange()。replace(“ $ END_DATE”,checkNull(details.get(i).getBillEndDate())+“,”,false,false); }

解决方法

实现所需功能的最佳方法是使用邮件合并功能。 https://docs.aspose.com/words/java/about-mail-merge/ 但是在这种情况下,您需要用模板中的mergefields替换占位符。 如果无法更改模板,则可以为列表中的每个项目克隆文档,替换克隆的文档中的值,然后它们将文档合并在一起以获得最终结果。代码如下所示:

// Open template
Document doc = new Document("C:\\Temp\\in.doc");

// Create document where result will be stored to.
// Simply clone the original template without children,// In this case styles of the original document will be kept.
Document result = (Document)doc.deepClone(false);

for(int i=0; i<details.length; i++)
{
    Document component = doc.deepClone();

    component.getRange().replace("$COMPONENT_NAME",details[i].getComponentName());
    component.getRange().replace("$EFFECTIVE_DATE",details[i].getBillEffectiveDate().toString());
    component.getRange().replace("$END_DATE",details[i].getBillEndDate().toString());

    // Append the result to the final document.
    result.appendDocument(component,ImportFormatMode.USE_DESTINATION_STYLES);
}

result.save("C:\\Temp\\out.doc");

但是我更喜欢邮件合并方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。