基于WordML生成word文件

基于WordML生成word文件

生成Word文档的类库有很多,常用的有jacob,itext等等,Itext只能支持word的部分功能,itext-rtf-2.1.7.jar 没有持续更新,itext 不支持在word文档中显示位置图片指定X,Y值,不支持表格的单元格画图(如画单元格斜线)。

WordML语法非常复杂,因为Word有非常多的功能,但可以根据使用场景去生成部分xml,可查询官方文档对wordML的元素定义,例如table,图片,浮动层文本、图片等。

一个类似浮动层文本基本的WordXML结构是如下:

<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xml:space="preserve"><w:docPr><w:view w:val="print" />
</w:docPr><w:body><w:p><w:r><w:pict><v:shape
style="position:absolute;left:0;width:200;height:200;z-index:1"><v:textbox><w:txbxContent><w:p><w:r><w:rPr /><w:t>test</w:t>
</w:r>
</w:p>
</w:txbxContent>
</v:textbox>
</v:shape>
</w:pict>
</w:r>
</w:p><w:tbl><w:tblPr><w:tblW w:w="0" w:type="auto" />
</w:tblPr><w:tr><w:trPr><w:trHeight w:val="330.0" />
</w:trPr><w:tc><w:tcPr><w:tcW w:w="1650.0" w:type="dxa" /><w:tcBorders /><w:vAlign
w:val="center" />
</w:tcPr><w:p><w:pPr><w:jc w:val="left" />
</w:pPr><w:r><w:rPr><w:color w:val="000000" />
</w:rPr><w:t></w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
</w:tbl><w:sectPr><w:pgSz w:w="11900.0" w:h="16840.0" /><w:pgMar
w:top="1440.0" w:right="1800.0" w:bottom="1440.0" w:left="1800.0"
w:header="851" w:footer="992" w:gutter="0" /><w:cols
w:space="452" /><w:docGrid w:type="lines"
w:line-pitch="312" />
</w:sectPr>
</w:body>
</w:wordDocument>

可以用记事本创建一个文件,将上面的XML内容粘贴,并保存为test.doc,则打开word会解析xml为doc文档。

Word命名空间如下:

public interface WordML {

    Namespace NS_W = new Namespace("w", "http://schemas.microsoft.com/office/word/2003/wordml");

    Namespace NS_V = new Namespace("v", "urn:schemas-microsoft-com:vml");

    Namespace NS_W10 = new Namespace("w10", "urn:schemas-microsoft-com:office:word");

    Namespace NS_SL = new Namespace("sl", "http://schemas.microsoft.com/schemaLibrary/2003/core");

    Namespace NS_AML = new Namespace("aml", "http://schemas.microsoft.com/aml/2001/core");

    Namespace NS_WX = new Namespace("wx", "http://schemas.microsoft.com/office/word/2003/auxHint");

    Namespace NS_O = new Namespace("o", "urn:schemas-microsoft-com:office:office");

    Namespace NS_DT = new Namespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");

}

WordML图片表示方法:

<w:pict><w:binData w:name="wordml://3000001.png">

Base64图片数据

</w:binData><v:shape style="position:absolute;left:0;width:400;height:300;z-index:1"><v:imagedata src="wordml://3000001.png" o:title="name"/></v:shape></w:pict>

Word 中table对应的xml元素:

        Element tbl = new DefaultElement(new QName("tbl", WordML.NS_W));

        Element tblPrE = tbl.addElement(new QName("tblPr", WordML.NS_W));

        Element tblWE = tblPrE.addElement(new QName("tblW", WordML.NS_W));

        tblWE.addAttribute(new QName("w", WordML.NS_W), "0").addAttribute(new QName("type", WordML.NS_W), "auto");

创建table中tr:

       Element tr = new DefaultElement(new QName("tr", WordML.NS_W));

       Element trPr = new DefaultElement(new QName("trPr", WordML.NS_W));

       Element trHeight = trPr.addElement(new QName("trHeight", WordML.NS_W));

       trHeight.addAttribute(new QName("val", WordML.NS_W), String.valueOf(height));

创建table中td,单元格合并:

        Element tc = new DefaultElement(new QName("tc", WordML.NS_W));

        Element tcPr = tc.addElement(new QName("tcPr", WordML.NS_W));

        Element tcW = tcPr.addElement(new QName("tcW", WordML.NS_W));

 

        float width = cell.getWidth() * WordML.PTW_VALUE;

        tcW.addAttribute(new QName("w", WordML.NS_W), String.valueOf(width));

        tcW.addAttribute(new QName("type", WordML.NS_W), "dxa");

 

        Element tcBorders = tcPr.addElement(new QName("tcBorders", WordML.NS_W));

        appendTcBorders(tcBorders, cell);

 

        if (StringUtil.isNotEmpty(cell.getVerticalAlignment())) {

            Element vAlign = tcPr.addElement(new QName("vAlign", WordML.NS_W));

            vAlign.addAttribute(new QName("val", WordML.NS_W), cell.getVerticalAlignment());

        }

 

        if (cell.getBackgroundColor() != null) {

            tcPr.add(asShdXml(cell.getBackgroundColor()));

        }

        if (cell.getColSpan() > 1) {

            Element gridSpan = tcPr.addElement(new QName("gridSpan", WordML.NS_W));

            gridSpan.addAttribute(new QName("val", WordML.NS_W), String.valueOf(cell.getColSpan()));

        }

        if (cell.isRestart()) {

            tcPr.add(asVmergeXml(true));

        }

        if (cell.isVmerge()) {

            tcPr.add(asVmergeXml(false));

        }