Ms word 如何使用Java从模板或现有文档创建Word文档?
我有一个文档模板,其中一些字段是静态的,另一些是动态的。我需要替换一些数据(姓名、姓氏、薪水)并生成新文件。你推荐哪个图书馆来做这个?POI是否合适?Ms word 如何使用Java从模板或现有文档创建Word文档?,ms-word,apache-poi,java-ee-6,.doc,Ms Word,Apache Poi,Java Ee 6,.doc,我有一个文档模板,其中一些字段是静态的,另一些是动态的。我需要替换一些数据(姓名、姓氏、薪水)并生成新文件。你推荐哪个图书馆来做这个?POI是否合适? 我正在使用Spring、Java EE6和Oracle。您可以尝试ApachePOI,但操作word文件所需的POI的HWPF和XWPF部分使用起来非常复杂-您至少需要很好地理解word文件的结构 使用iText和PDF的解决方案 我用PDF做了类似的事情(这可能是你的一个选择) 1) 您可以使用LibreOffice在文档中创建字段(如在Acr
我正在使用Spring、Java EE6和Oracle。您可以尝试ApachePOI,但操作word文件所需的POI的HWPF和XWPF部分使用起来非常复杂-您至少需要很好地理解word文件的结构 使用iText和PDF的解决方案 我用PDF做了类似的事情(这可能是你的一个选择) 1) 您可以使用LibreOffice在文档中创建字段(如在Acrobat Pro中)
- 创建一个.odt文件并设置其样式
- 或者使用MS Word或LibreOffice Writer将模板转换为它
- 然后进入查看->工具栏->表单设计并设置“设计模式开/关”
- 现在您可以将字段添加到文件中(双击它将打开字段的属性)
- 完成后:“文件->导出为PDF”
public byte[] getDocumentAsByteArray(Object dataBean, String pdfTemplateName) throws KkmsException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfStamper stamp = null;
InputStream templateInputStream = null;
Locale local = new Locale(language);
try {
templateInputStream = // get the file input stream of the pdf
PdfReader reader = new PdfReader(templateInputStream);
// Create a stamper that will copy the document to a new file
stamp = new PdfStamper(reader, outputStream);
AcroFields form = stamp.getAcroFields();
// form fields are normal text in the end
stamp.setFormFlattening(true);
Map<String, AcroFields.Item> map = (Map<String, AcroFields.Item>)form.getFields();
if (map != null) {
if (map.size() == 0) {
logger.debug("There are no fields in this PDF layout");
}
for (Entry<String, AcroFields.Item> e : map.entrySet()) {
logger.debug("PDF fieldname = " + e.getKey());
// at the moment we only handle text fields
if (AcroFields.FIELD_TYPE_TEXT == form.getFieldType(e.getKey())) {
fillForm(dataBean, form, e.getKey(), local);
} else {
logger.warn("Field type is not supported: "+form.getFieldType(e.getKey()));
}
}
}
stamp.close();
} catch (Exception e) {
logger.warn("Failed to create PDF document", e);
throw new KkmsException("Failed to create PDF document: "+e.getMessage());
} finally {
if (templateInputStream != null) {
try {
templateInputStream.close();
} catch (IOException e) {
throw new KkmsException("Failed to close InputStream of PDF document", e);
}
}
}
return outputStream.toByteArray();
}
public byte[]GetDocumentsByteArray(对象数据库,字符串pdfTemplateName)引发KkmsException{
ByteArrayOutputStream outputStream=新建ByteArrayOutputStream();
PdfStamper stamp=null;
InputStream templateInputStream=null;
本地语言环境=新的语言环境(语言);
试一试{
templateInputStream=//获取pdf的文件输入流
PdfReader reader=新的PdfReader(templateInputStream);
//创建将文档复制到新文件的压模
stamp=新的PdfStamper(读卡器、输出流);
AcroFields form=stamp.getAcroFields();
//表单字段最后是普通文本
stamp.SetFormFlatting(真);
Map Map=(Map)form.getFields();
if(map!=null){
if(map.size()==0){
debug(“此PDF布局中没有字段”);
}
对于(条目e:map.entrySet()){
logger.debug(“PDF fieldname=“+e.getKey());
//目前我们只处理文本字段
if(AcroFields.FIELD\u TYPE\u TEXT==form.getFieldType(e.getKey()){
fillForm(dataBean,form,e.getKey(),local);
}否则{
logger.warn(“不支持字段类型:”+form.getFieldType(e.getKey());
}
}
}
stamp.close();
}捕获(例外e){
logger.warn(“未能创建PDF文档”,e);
抛出新的KkmsException(“未能创建PDF文档:+e.getMessage());
}最后{
if(templateInputStream!=null){
试一试{
templateInputStream.close();
}捕获(IOE异常){
抛出新的KkmsException(“无法关闭PDF文档的InputStream”,e);
}
}
}
返回outputStream.toByteArray();
}
最后你会得到一个PDF->希望这对你至少有一点帮助
另一个快速而肮脏的解决方案
可以使用odt或docx的强大功能->将您的文档转换为docx或odt->它只是一个zip文件->因此解压缩它->您将在zip的根目录中看到content.xml文件->其中包含所有文档内容
现在,您可以在此处添加一些魔术标记(例如$$$),稍后可由您的程序替换
<text:p text:style-name="P3">SAP Customer Number:</text:p>
<text:p text:style-name="P3">SAP Customer Number: $$$sapCustomerNumber$$$</text:p>
SAP客户编号:
SAP客户编号:$$$sapCustomerNumber$$$
现在创建一个程序,将odt/docx文件解压->替换标签->再次解压文件,根据我在OSDC 2012上的演示,概述一些主要方法
现在我可能会添加“生成您想要的XHTML,然后将其导出到docx”。自从我们引入了支持将CSS@class值转换为Word样式的docx4j ImportXHTML以来,我们越来越多地看到了这种方法。看看这个问题: