需要使用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");

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...