zoukankan      html  css  js  c++  java
  • jsp请求java返回pdf、excel与word

    1,返回pdf关键代码

        /**
         * @todo 
         * @param 
         * @date 2019年3月8日
         * @author yanan
         */
        @RequestMapping("/getPdf")
        public void getPdf(HttpServletRequest req, HttpServletResponse response)throws Exception {
            response.setContentType("application/pdf"); // 设置返回内容格式
            try {
                String pdfPath="放置你的指定pdf文件路径";
                // 判断该路径下的文件是否存在
                File file = new File(pdfPath);
                if (file.exists()) {
                    DataOutputStream temps = new DataOutputStream(response.getOutputStream());  
                    DataInputStream in = new DataInputStream(new FileInputStream(pdfPath));
                    byte[] b = new byte[2048];
                    while ((in.read(b)) != -1) {
                        temps.write(b);
                        temps.flush();
                    }
                    in.close();
                    temps.close();
                } else {
                    System.out.println(" 文件不存在!");
                }
    
            } catch (Exception e) {
                logger.info(e.getMessage());
            }
        }
    View Code

    2,返回word关键代码

        /**
         * @todo 导出word
         * @param 
         * @date 2019年3月8日
         * @author yanan
         */
        @RequestMapping(value="/exportWord",method=RequestMethod.POST)
        public void exportWord(HttpServletResponse response,HttpServletRequest req) throws IOException{
            response.setContentType("application/doc"); // 设置返回内容格式
            response.setHeader("Content-disposition","attachment; filename="+new String("你的文件名称".getBytes("utf-8"), "8859_1"));
            DataOutputStream temps = new DataOutputStream(response.getOutputStream()); 
            temps.write("你的文件内容".getBytes());
            temps.flush();
            temps.close();
        }
    View Code

    3,返回excel关键代码

        /**
         * @todo 导出excel
         * @param tableHtml 要导出的table表格示例:<table><tr><td>aaa</td></tr></table>
         * @param fileName 要导出的文件名
         * @date 2019年3月8日
         * @author yanan
         */
        @RequestMapping(value="/exportExcel",method=RequestMethod.POST)
        public void export(HttpServletResponse response,String tableHtml,String fileName,HttpServletRequest req) throws IOException{
            String cssStyle = req.getParameter("cssStyle");//样式
            cssStyle=cssStyle==null?"":cssStyle;
            exportDataExcel(response, tableHtml, fileName,cssStyle);
        }
        private static void exportDataExcel(HttpServletResponse response, String tableHtml, String fileName,String cssStyle) throws IOException {
            response.setCharacterEncoding("UTF-8");
            //返回头设置文件名。Content-Disposition参数本来是为了在客户端另存文件时提供一个建议的文件名,但是考虑到安全的原因,就从规范中去掉了这个参数。
            //但是由于很多浏览器已经能够支持这个参数,所以只是在规范文档中列出,但是要注意这个不是HTTP/1.1的标准参数。
            //若不用java.net.URLEncoder.encode,则中文文件名乱码无法显示(英文部分不影响)
            response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName+".xls", "UTF-8"));
            //返回内容为excel
            response.setContentType("application/ms-excel;charset=UTF-8");
            PrintWriter out = response.getWriter();;
            tableHtml=String.format("<style type='text/css'>%s</style>%s", cssStyle, tableHtml);
            out.print(tableHtml);
            out.close();
        }
    View Code

    利用poi导出,移步我之前的文章@java利用poi生成/读取excel表格

  • 相关阅读:
    「学习记录」《数值分析》第三章计算实习题(Python语言)
    Set原理
    字符串流stringReader
    Collection List接口
    io
    Dubbo 服务容错Hystrix
    Duboo 与springboot整合
    读取配置文件
    springboot 端口号
    springboot 多环境选择
  • 原文地址:https://www.cnblogs.com/yanan7890/p/10497303.html
Copyright © 2011-2022 走看看