当前位置: 代码迷 >> J2EE >> poi.hwpf 操作word 越详细越好解决办法
  详细解决方案

poi.hwpf 操作word 越详细越好解决办法

热度:1102   发布时间:2016-04-17 23:37:14.0
poi.hwpf 操作word 越详细越好
越详细越好
    图片操作
   图片水印
   还有其他在项目中经常用到的东西,希望大家能分享下,网上许多东东都是没有解决问题的东东希望
在csdn里集合大家的力量能有个好的结果


/***
 * 实现Word模板读取替换内容输出
 * @param filePath 模板路径
 * @param haderMap 页眉数据
 * @param bodyMap  内容数据
 * @param footMap  页脚数据
 */
    public static void readwriteWord(String filePath, Map<String,String> haderMap,Map<String ,String> bodyMap,Map<String,String> footMap){
        //读取word模板
        FileInputStream in = null;
        try {
            in = new FileInputStream(new File(filePath));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        HWPFDocument hdt = null;
        try {
            hdt = new HWPFDocument(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        //读取word页眉内容
        Range harderRange= hdt.getHeaderStoryRange();
        //替换word页眉内容
        for(Map.Entry<String, String> entry:haderMap.entrySet()){
         harderRange.replaceText("${" + entry.getKey() + "}", entry.getValue());        
        }
        
        //读取页脚内容
        Range footRange=hdt.getFootnoteRange();
        //替换页脚内容
        for(Map.Entry<String,String> entry:footMap.entrySet()){
         footRange.replaceText("${" + entry.getKey().trim() + "}", entry.getValue());        
        }
        
        //读取word文本内容
        Range bodyRange = hdt.getRange();
        //替换文本内容   
        for (Map.Entry<String,String> entry: bodyMap.entrySet()) {
            bodyRange.replaceText("${" + entry.getKey() + "}",entry.getValue());
           
        }
        
       // PicturesTable   picturesTable=  hdt.getPicturesTable();
//        //hdt.addPicture(bytes, XWPFDocument.PICTURE_TYPE_JPEG); 
//        
//        FileInputStream fis = new FileInputStream("F:\\picture\\http_imgload.jpg");
//        //将图片添加到xlsx文件
//        int picinx = hdt.addPicture(fis, XWPFDocument.PICTURE_TYPE_JPEG);
//        fis.close();
//    

        
        
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        String fileName = ""+System.currentTimeMillis();
        fileName += ".doc";
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("E:\\test\\"+fileName,true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            hdt.write(ostream);
        } catch (IOException e) {
            e.printStackTrace();
  相关解决方案