使用POI分段落生成纯Word动态模板并导入数据

来源:互联网 发布:手机能注册淘宝卖家吗 编辑:程序博客网 时间:2024/05/14 11:54

导出数据,可以用word另存为xml格式的ftl文件,变量用${变量名}表示,然后在类中通过freemarker去替换变量。

但是怎么导入word数据。发现如果是xml格式,数据格式很易变。如一个标题中如果有中文,后面是个数字,另存成xml时就变成了2个元素了。太郁闷了。


后来找到方法可以分段落读入纯word文档。要找到了word基于模板替换的相关例子。于是方案如下。

纯word文件变量用${变量名}表示。导出动态模板时,把业务数据替换变量。

导入是分段落读入数据。


POIReportUtil.java文件


package com.sunrise.wbmp.manage.project.report.util;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;


import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;


public class POIReportUtil {


    public static void main(String[] args) throws Exception {
        Map<String, POIText> replaces = new HashMap<String, POIText>();
        System.out.println("导出数据:");
        
        replaces.put("${projectName}", POIText.str("CMMI"));
        
        replaces.put("${graph1}", POIText.str("rongzhi_li"));
        replaces.put("${内容1}", POIText.str("1123456"));


        replaces.put("${graph2}", POIText.str("graph"));
        replaces.put("${内容2}", POIText.str("2222"));
//
        replaces.put("${graph3}", POIText.str("graph"));
        replaces.put("${内容3}", POIText.str("33333"));


        replaces.put("${graph4}", POIText.str("<END>"));
        replaces.put("${内容4}", POIText.str(""));
        
        for (int i=5;i<10;i++){
            replaces.put("${graph"+i+"}", POIText.str(""));
            replaces.put("${内容"+i+"}", POIText.str(""));
        }


         HWPFDocument hwpf=poiWordTableReplace("E:\\temp\\template\\week_report.doc", replaces);
    FileOutputStream out = new FileOutputStream("E:\\temp\\template\\t2.doc");
    hwpf.write(out);

    out.flush();
    out.close();  
    
    System.out.println("读入数据:");
    FileInputStream in = new FileInputStream("E:\\temp\\template\\t2.doc");
    HWPFDocument hwpf2 = new HWPFDocument(in);
    Map<String,String> map=readWord( hwpf2);    
    for (Iterator iter=map.entrySet().iterator();iter.hasNext();){
    java.util.Map.Entry entry=(java.util.Map.Entry)iter.next();
    System.out.println(entry.getKey()+":"+entry.getValue());
    }
    }
    public static Map<String,String> readWord(HWPFDocument hwpf
            ) throws Exception {
//        FileInputStream in = new FileInputStream(sourceFile);
//        HWPFDocument hwpf = new HWPFDocument(in);
    Map<String, String> oldMap=new HashMap<String, String>();
        Range r = hwpf.getRange();
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < r.numParagraphs(); i++) {
            Paragraph p = r.getParagraph(i);
            // check if style index is greater than total number of styles
            int numStyles =hwpf.getStyleSheet().numStyles();
            int styleIndex = p.getStyleIndex();
            if (numStyles > styleIndex) {
                StyleSheet style_sheet = hwpf.getStyleSheet();
                StyleDescription style = style_sheet.getStyleDescription(styleIndex);
                String styleName = style.getName();
                if (styleName!=null&&styleName.contains("标题 3")) {
                    // write style name and associated text
                    System.out.println(styleName + " -> " + p.text());
                    //System.out.println(p.text());
                    String text = p.text();
                    text=text.substring(0,text.length()-1);
                    list.add(text);
                    oldMap.put(text, null);
                    if ("<END>".equals(text)){
                    break;
                    }
                }else{
                if (list.size()>0){
                String key=list.get(list.size()-1);
                String oldS=oldMap.get(list.get(list.size()-1));
                String s=( p.text()==null)?"": p.text();
                oldS=(oldS==null)?s:(oldS+s);
                if (oldS!=null && oldS.trim().length()>0){
                oldMap.put(key, oldS);
                }
                }
                }
            }
        }
        return oldMap;
    }    
    public static HWPFDocument poiWordTableReplace(String sourceFile,
            Map<String, POIText> replaces) throws Exception {
        FileInputStream in = new FileInputStream(sourceFile);
        HWPFDocument hwpf = new HWPFDocument(in);
//        Range range = hwpf.getRange();// 得到文档的读取范围
//        TableIterator it = new TableIterator(range);
//        // 迭代文档中的表格
//        while (it.hasNext()) {
//            Table tb = (Table) it.next();
//            // 迭代行,默认从0开始
//            for (int i = 0; i < tb.numRows(); i++) {
//                TableRow tr = tb.getRow(i);
//                // 迭代列,默认从0开始
//                for (int j = 0; j < tr.numCells(); j++) {
//                    TableCell td = tr.getCell(j);// 取得单元格
//                    // 取得单元格的内容
//                    for (int k = 0; k < td.numParagraphs(); k++) {
//                        Paragraph para = td.getParagraph(k);
//
//                        String s = para.text();
//                        final String old = s;
//                        for (String key : replaces.keySet()) {
//                            if (s.contains(key)) {
//                                s = s.replace(key, replaces.get(key).getText());
//                            }
//                        }
//                        if (!old.equals(s)) {// 有变化
//                            para.replaceText(old, s);
//                            s = para.text();
//                            System.out.println("old:" + old + "->" + "s:" + s);
//                        }
//
//                    } // end for
//                } // end for
//            } // end for
//        } // end while


        Range r = hwpf.getRange();
        CharacterProperties props = new CharacterProperties();
        props.setFontSize(10);
        
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < r.numParagraphs(); i++) {
            Paragraph p = r.getParagraph(i);
            // check if style index is greater than total number of styles
            int numStyles =hwpf.getStyleSheet().numStyles();
            int styleIndex = p.getStyleIndex();
            if (numStyles > styleIndex) {
                StyleSheet style_sheet = hwpf.getStyleSheet();
                StyleDescription style = style_sheet.getStyleDescription(styleIndex);
                String styleName = style.getName();
//                if (styleName!=null&&styleName.contains("标题")) {
//                    // write style name and associated text
//                    //System.out.println(styleName + " -> " + p.text());
//                    System.out.println(p.text());
//                    String text = p.text();
//                    list.add(text);
//                }else{
//                    System.out.println(  p.text());
//                }
                
                System.out.println(  styleName);
                
                String s = p.text();
                final String old = s;
                for (String key : replaces.keySet()) {
                    if (s.contains(key)) {
                        s = s.replace(key, replaces.get(key).getText());
                    }
                }
                if (!old.equals(s)) {// 有变化
                    p.replaceText(old, s);
                    s = p.text();
                    System.out.println("old:" + old + "->" + "s:" + s);
                }




            }
        }
        return hwpf;
    }
}

POIReportUtil.java文件

package com.sunrise.wbmp.manage.project.report.util;


public abstract class POIText {


    public abstract String getText();


    public static POIText str(final String string) {
        return new POIText() {
            @Override
            public String getText() {
                return string;
            }
        };
    }
}



0 0