简体   繁体   English

xsl样式表,用于从Java生成Word文档

[英]xsl stylesheet for generating word documents from Java

I was wondering if there is any Java API which allows to generate Word document similarly Apache FOP does. 我想知道是否有任何Java API都可以像Apache FOP一样生成Word文档。
With FOP, it is possible to specify a style sheet which defines the layout of the page in which the data (stored in an xml file) are printed. 使用FOP,可以指定样式表,该样式表定义打印数据(存储在xml文件中)的页面布局。
The Transformer object within FOP library is in charge of that. FOP库中的Transformer对象负责此工作。

Is there any equivalent API for word document? Word文档是否有等效的API?

With FOP, you can try XML to RTF , which Word accepts. 使用FOP,您可以尝试将XML转换为 Word接受的RTF

From their webpage, XMLmind XSL-FO Converter apparently generates: XMLmind XSL-FO Converter从他们的网页显然可以生成:

  • RTF (can be opened in Word 2000+), RTF(可以在Word 2000+中打开),
  • WordprocessingML (can be opened inWord 2003+), WordprocessingML(可以在Word 2003+中打开),
  • Office Open XML (.docx, can be opened in Word 2007+), Office Open XML(.docx,可以在Word 2007+中打开),

Putting FO to one side, here are 2 different approaches: 将FO放在一边,这里有2种不同的方法:

The first would be to write an XSLT to convert your XML to Flat OPC XML. 首先是编写一个XSLT将XML转换为Flat OPC XML。 Most parts in the Flat OPC XML would simply be copied there by your XSLT. Flat OPC XML中的大多数部分都可以通过XSLT轻松复制到那里。 (Generate that template content in Word, using "save as XML"). (使用“另存为XML”在Word中生成该模板内容)。 You'll be focusing mainly on populating the document.xml part. 您将主要集中于填充document.xml部分。 Word can open a Flat OPC XML file, or you can use docx4j (a project I work on) to convert Flat OPC XML to docx. Word可以打开Flat OPC XML文件,也可以使用docx4j(我正在研究的项目)将Flat OPC XML转换为docx。

The second would be to use the docx4j Flying Saucer fork to convert your XML + CSS to docx content. 第二种方法是使用docx4j Flying Saucer分支将XML + CSS转换为docx内容。 See the code samples . 请参阅代码示例 You may need to customise it a bit; 您可能需要对其进行一些自定义; one way of feeding it CSS is this file . 馈送CSS的一种方法是此文件 This actually ought to work pretty well; 这实际上应该工作得很好。 there is stuff there for mapping class attributes to Word styles, so if you could adorn your XML with class attributes, you could get even better results. 有一些东西可以将类属性映射到Word样式,因此,如果可以用类属性装饰XML,则可以获得更好的结果。

I will assume that your input is an XML document, or at least a CSV file. 我将假设您的输入是XML文档,或者至少是CSV文件。

1) Create an XSLT stylesheet to transform your input into the Word document format. 1)创建一个XSLT样式表,将您的输入转换为Word文档格式。 The result will be a file we will call content.xml . 结果将是一个我们称为content.xml的文件。 You can apply the stylesheet to the input from Java. 您可以将样式表应用于Java的输入。

2) Create a MS Word shell and put the content.xml into the shell. 2)创建一个MS Word shell,并将content.xml放入该shell中。 There are tools within Apache POI to do this. Apache POI中有一些工具可以做到这一点。

I may have taken your question too literally. 我可能真的从字面上回答了您的问题。 You might also be able to generate the document using Apache POI API. 您也许还可以使用Apache POI API生成文档。 Also, if your MS Word document doesn't have tables, you can use Apache FOP to generate an RTF document, which can then be easily translated to a .docx file. 另外,如果您的MS Word文档没有表,则可以使用Apache FOP生成RTF文档,然后可以将其轻松转换为.docx文件。

Apache POI provides Java APIs for reading and writing Microsoft Excel, Word, and PowerPoint files. Apache POI提供用于读取和写入Microsoft Excel,Word和PowerPoint文件的Java API。

You can checkout POI's Javadocs here . 您可以在此处签出POI的Javadocs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM