• java word导入导出工具类


    package com.shareworx.yjwy.utils;
    
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.poi.hwpf.HWPFDocument;
    import org.apache.poi.hwpf.usermodel.Range;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.util.IOUtils;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    /**
     * 
     * @author zhenwei.shi
     *
     */
    public class ImpAndExpWord {
        /**
         * 根据表单导出word文档
         * @author zhenwei.shi
         * @param map 导入对象的键值对  KEY=模板标签  VALUE=替换模板标签的内容
         * @param temlatePath word模板的全部路径  如:/templates/supplier/供应商管理表单.docs
         *            此路径根据架构师搭建的资源环境而定,一般src/../../xx.docs
         * @return
         */
        public static void export(Map<String,String> params, String docPath){        
            String docName = docPath.substring(docPath.lastIndexOf("/")+1, docPath.length());
            InputStream is = null;
            ServletOutputStream out = null;
            try{
                //下面这个MAP 就是上面参数里的MAP 在此为了视觉更清晰 真正实施时 提前通过MAP传进来,不要在此new
                params = new HashMap<String, String>();
                params.put("AAAA","v1");//word模板上有个字符串叫k1,这里将模板上的k1替换为要赋值v1
                params.put("k2","v2");//同上
                params.put("k3","v3");//同上
                params.put("k4","v4");//同上
                params.put("k5","v5");//同上
                params.put("k6","v6");//同上
                params.put("k7","v7");//同上
                
                is = ImpAndExpWord.class.getResourceAsStream("/"+docPath);
                POIFSFileSystem pfs = new POIFSFileSystem(is);  
                HWPFDocument hwpf = new HWPFDocument(pfs);  
                Range range = hwpf.getRange();
                
                Set<String> keys = params.keySet();
                for(String key: keys){
                    range.replaceText(key, params.get(key));
                }
                HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/octet-stream; charset=UTF-8");
                response.addHeader("Content-Disposition", "attachment; filename=""+new String(docName.getBytes("GB2312"),"ISO8859-1")+"";");// 
                out = response.getOutputStream();
                
                   hwpf.write(out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                IOUtils.closeQuietly(out);
                IOUtils.closeQuietly(is);
            }
        }
    
    }

    word 图片

  • 相关阅读:
    PHP远程下载图片,微信头像存到本地,本地图片转base64
    jQuery Validate自定义错误信息,自定义方法
    创建自己的composer包
    js,JQ获取短信验证码倒计时
    JQ JS复制到剪贴板
    js,JQuery 生成二维码
    js,JQuery实现,带筛选,搜索的select
    HTML5拖放牛刀小试
    HTML5上传图片预览功能
    一次dropzone体验
  • 原文地址:https://www.cnblogs.com/zwcry/p/8484261.html
Copyright © 2020-2023  润新知