poi.hwpf 操作word 越详细越好

J2EE 码拜 9年前 (2015-04-08) 2958次浏览 0个评论
 

越详细越好
    图片操作
   图片水印
   还有其他在项目中经常用到的东西,希望大家能分享下,网上许多东东都是没有解决问题的东东希望
在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();
        }
        //输出字节流
        try {
            out.write(ostream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ostream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }    


注:次代码只能在 poi 3.8 _4 的版本上使用
我希望能有人告诉我 其他的高级的用法 谢谢 

poi.hwpf 操作word 越详细越好
有做过图片插入,水印的不?
poi.hwpf 操作word 越详细越好
帮帮忙啊
poi.hwpf 操作word 越详细越好
11分
页眉页脚的样式不好控制,还是不够灵活啊。最好引入一个类似word控件就OK了。
poi.hwpf 操作word 越详细越好
不想用控件,兼容性部好,还要在服务器安装东西 
poi.hwpf 操作word 越详细越好
42分
这个方法好像有BUG,简单的doc文件可以,如果复杂点就over了,我测试的时候表格如果大一点就不行了
poi.hwpf 操作word 越详细越好
14分
插入图片不成功,,,
poi.hwpf 操作word 越详细越好
11分
其他的高级用法,你还不如去看poi api。
poi.hwpf 操作word 越详细越好
11分
插入图片不成功,兄弟,能告诉我怎么插入图片吗
poi.hwpf 操作word 越详细越好
11分
求解 求解  兄弟  为什么我一生成 中文文件名就乱码 
   <result name=”success” type=”stream”>   
        <param name=”contentType”>application/vnd.ms-excel;charset=utf-8</param>  
        <param name=”inputName”>inputStream</param>   
        <param name=”contentDisposition”>inline;filename=”影像统计.xlsx”</param>   
        <param name=”bufferSize”>4096</param>   
 </result> 
  在线等待
poi.hwpf 操作word 越详细越好
插入图片是可以的。见我blog
poi.hwpf 操作word 越详细越好
//读取页脚内容         Range footRange=hdt.getFootnoteRange();         //替换页脚内容         for(Map.Entry<String,String> entry:footMap.entrySet()){             footRange.replaceText(“${” + entry.getKey().trim() + “}”, entry.getValue());                     } 

怎么读取页眉内容,????

poi.hwpf 操作word 越详细越好
亲爱的楼主大人,我想请问,如果想用poi新建一个word文档,然后再新建一个表格插入到该文档中,该怎么做啊,求代码,求指点,很急用呢,有劳您了呢~~~
poi.hwpf 操作word 越详细越好
引用 6 楼 zyc901016 的回复:

这个方法好像有BUG,简单的doc文件可以,如果复杂点就over了,我测试的时候表格如果大一点就不行了

遇到了相同的问题,大表格分页全都乱了

poi.hwpf 操作word 越详细越好
不知POI对word操作有没有插入分页的方法??

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明poi.hwpf 操作word 越详细越好
喜欢 (1)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!