zoukankan      html  css  js  c++  java
  • 导出Excel表格

    html代码

    <body class="w hauto filter2 f14">
        <form id="searchForm" action="../../dataCompared/export.do" method="post">
            <input type="hidden" name="tableTitle" id="tableTitle"/>
            <input type="hidden" name="tableData" id="tableData"/>
            <div class="ml30 mt10 mb20">
                <span class="fl mr10">1、选择考核内容</span>
                <span class="fl mr10" id="checkContentCount" style="color: blue;"></span>
                <span class="btn2" id="selectBtn">点击选择</span>
            </div>
            <div class="ml30 mt10 mb20 h25">
                <span class="fl mr10">2、添加对比市县</span>
                <span class="fl"><input type="text" id="county" placeholder="最多可以选择30个县对比数据"></span>
                <span class="fl ml10"><span class="btn2"  id="submitBtn">提交</span></span>
                <span class="fl ml10"><span class="btn2"  id="exportBtn">表格导出</span></span>
            </div>
            <table border="0" class="authentication_info" style="margin-bottom: 20px" cellpadding="0" cellspacing="1">
                <col style=" 100px"/>
                <col style=" 20px" id="colFlag"/>
                <thead id="tableHead">
                </thead>
                <tbody id="test"></tbody>
            </table>
        </form>
    </body>

    java代码

    /**
         * 导出
         */
        @RequestMapping("/export")
        @SuppressWarnings({"unchecked", "rawtypes"})
        public void export(@RequestParam Map map, HttpServletRequest request, HttpServletResponse response) {
            response.setContentType("application/json;charset=utf-8");
            String title = request.getParameter("tableTitle");
            String data = request.getParameter("tableData");
            JSONArray json = JSONArray.fromObject(data);
            List<Object> list= (List<Object>)JSONArray.toCollection(json, Object.class);
    
            // 生成文件名称
            String filename = "安全县创建实际完成量数据对比" + ".xls";
            // 获取导出信息列表
            Workbook book = new HSSFWorkbook();
            ServletOutputStream outputStream = null;
            try {
                // 创建第一个sheet页
                Sheet sheet = book.createSheet("数据对比");
                // 生成第一行
                Row row = sheet.createRow(0);
    //                String biaoti = "安全县创建实际完成量数据对比";
    
                String[] d = new String[20];
                d = title.split("@");
                row.createCell(0).setCellValue("区县|实际完成量|考核内容");
                for (int i = 1; i < d.length+1; i++) {
                    row.createCell(i).setCellValue(d[i-1]);
                }
    
                sheet.setColumnWidth(0, 8 * 2 * 512);
    
                // 生成第二行之后的内容
                int i = 1;
                for (Object object : list) {
                    List listData = (List) object;
                    // 创建一行
                    Row row2 = sheet.createRow(i++);
                    for (int j=0; j<listData.size(); j++){
                        row2.createCell(j).setCellValue(listData.get(j)+"");
                    }
                }
                response.setContentType("application/OCTET-STREAM;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1"));
                response.setCharacterEncoding("UTF-8");
                ByteArrayOutputStream outPut = new ByteArrayOutputStream();
                book.write(outPut);
                byte[] b = outPut.toByteArray();
                ByteArrayInputStream inputStream = new ByteArrayInputStream(b);
                outputStream = response.getOutputStream();
                int bytesRead = 0;
                // 用输出流去写,缓冲输入输出流
                byte[] buffer = new byte[8192];
                while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }

    注意:导出Excel表格的请求要放在form表单的action中,尝试用ajax请求失败

  • 相关阅读:
    笔记35 跨重定向请求传递数
    判断邮箱的正则表达式
    按钮
    async await 的用法
    笔记34 Spring MVC的高级技术——处理multipart形式的数据
    Convert Sorted Array to Binary Search Tree
    Binary Tree Zigzag Level Order Traversal
    Unique Binary Search Trees,Unique Binary Search Trees II
    Validate Binary Search Tree
    Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
  • 原文地址:https://www.cnblogs.com/ysgcs/p/9235197.html
Copyright © 2011-2022 走看看