首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

poi.hwpf 操作word 越详细越好解决方案

2012-12-21 
poi.hwpf 操作word 越详细越好越详细越好图片操作图片水印还有其他在项目中经常用到的东西,希望大家能分享

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();
        }
        //输出字节流
        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 的版本上使用
我希望能有人告诉我 其他的高级的用法 谢谢 
[最优解释]
这个方法好像有BUG,简单的doc文件可以,如果复杂点就over了,我测试的时候表格如果大一点就不行了
[其他解释]
插入图片不成功,,,
[其他解释]
页眉页脚的样式不好控制,还是不够灵活啊。最好引入一个类似word控件就OK了。
------其他解决方案--------------------


其他的高级用法,你还不如去看poi api。
[其他解释]
插入图片不成功,兄弟,能告诉我怎么插入图片吗
[其他解释]
求解 求解  兄弟  为什么我一生成 中文文件名就乱码 
  <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> 
  在线等待
[其他解释]
有做过图片插入,水印的不?
[其他解释]
帮帮忙啊
[其他解释]
该回复于2012-03-21 17:30:30被版主删除
[其他解释]
不想用控件,兼容性部好,还要在服务器安装东西 
[其他解释]
插入图片是可以的。见我blog

热点排行