基于WordML生成word文件的完美解决方案

阅读更多
生成Word文档的类库有很多,常用的有jacob,itext等等,Itext只能支持word的部分功能,itext-rtf-2.1.7.jar 没有持续更新,itext 不支持在word文档中显示位置图片指定X,Y值,不支持表格的单元格画图(如画单元格斜线)
WordML语法非常复杂,因为Word有非常多的功能,但可以根据使用场景去生成部分xml,可查询官方文档对wordML的元素定义,例如table,图片,浮动层文本、图片等。
一个类似浮动层文本基本的WordXML结构是如下:
 
 

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">
style="position:absolute;left:0;width:200;height:200;z-index:1">test









w:val="center" />







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:space="452" /> w:line-pitch="312" />




可以用记事本创建一个文件,将上面的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图片表示方法:

 
    Base64图片数据
 

     


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));
        }


原文出处: http://www.anyrt.com/blog/list/201609092011.html

你可能感兴趣的:(office,xml)