Springmvc和poi3.9导出excel并弹出下载框

来源:互联网 发布:datatype json 编辑:程序博客网 时间:2024/05/16 11:23

点击打开链接


@RequestMapping(value = "/importexcel.htm", method = RequestMethod.GET)    public ModelAndView _importExcel(HttpServletRequest request, HttpServletResponse response, Integer cId) throws Exception {        // 获得要导出的数据集        List<Map<String, Object>> list = d_ExchangeCodeService.selectExcelRecord(cId);        // 创建excel工作簿        Workbook wb = new HSSFWorkbook();        // 创建第一个sheet(页),并命名        Sheet sheet = wb.createSheet(list.get(0).get("NAME").toString());        // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,n为列高的像素数。        sheet.setColumnWidth((short) 0, (short) (35.7 * 150));        sheet.setColumnWidth((short) 1, (short) (35.7 * 150));        sheet.setColumnWidth((short) 2, (short) (35.7 * 150));        sheet.setColumnWidth((short) 3, (short) (35.7 * 100));        sheet.setColumnWidth((short) 4, (short) (35.7 * 250));        sheet.setColumnWidth((short) 5, (short) (35.7 * 150));        sheet.setColumnWidth((short) 6, (short) (35.7 * 150));        // 创建第一行        Row row = sheet.createRow((short) 0);        // 创建两种单元格格式        CellStyle cs = wb.createCellStyle();        CellStyle cs2 = wb.createCellStyle();        // DataFormat df = wb.createDataFormat();        // 创建两种字体        Font f = wb.createFont();        Font f2 = wb.createFont();        // 创建第一种字体样式        f.setFontHeightInPoints((short) 10);        f.setColor(IndexedColors.RED.getIndex());        f.setBoldweight(Font.BOLDWEIGHT_BOLD);        // 创建第二种字体样式        f2.setFontHeightInPoints((short) 10);        f2.setColor(IndexedColors.BLACK.getIndex());        f2.setBoldweight(Font.BOLDWEIGHT_BOLD);        // 设置第一种单元格的样式        cs.setFont(f);        cs.setBorderLeft(CellStyle.BORDER_THIN);        cs.setBorderRight(CellStyle.BORDER_THIN);        cs.setBorderTop(CellStyle.BORDER_THIN);        cs.setBorderBottom(CellStyle.BORDER_THIN);        // cs.setDataFormat(df.getFormat("#,##0.0"));        // 设置第二种单元格的样式        cs2.setFont(f2);        cs2.setBorderLeft(CellStyle.BORDER_THIN);        cs2.setBorderRight(CellStyle.BORDER_THIN);        cs2.setBorderTop(CellStyle.BORDER_THIN);        cs2.setBorderBottom(CellStyle.BORDER_THIN);        // cs2.setDataFormat(df.getFormat("text"));        // 创建列(每行里的单元格)        Cell cell = row.createCell(0);        cell.setCellValue("用户名");        cell.setCellStyle(cs);        cell = row.createCell(1);        cell.setCellValue("订单号");        cell.setCellStyle(cs);        cell = row.createCell(2);        cell.setCellValue("兑换券序列号");        cell.setCellStyle(cs);        cell = row.createCell(3);        cell.setCellValue("兑换券金额");        cell.setCellStyle(cs);        cell = row.createCell(4);        cell.setCellValue("兑换券类型名称");        cell.setCellStyle(cs);        cell = row.createCell(5);        cell.setCellValue("使用时间");        cell.setCellStyle(cs);        cell = row.createCell(6);        cell.setCellValue("使用结束日期");        cell.setCellStyle(cs);        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");        for (short i = 0; i < list.size(); i++) {            // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的            // 创建一行,在页sheet上            row = sheet.createRow((short) i + 1);            // 在row行上创建一个方格            cell = row.createCell(0);            cell.setCellValue(list.get(i).get("usr_UserID") == null ? "未使用" : list.get(i).get("usr_UserID").toString());            cell.setCellStyle(cs2);            cell = row.createCell(1);            cell.setCellValue(list.get(i).get("ord_OrderID") == null ? "未使用" : list.get(i).get("ord_OrderID").toString());            cell.setCellStyle(cs2);            cell = row.createCell(2);            cell.setCellValue(list.get(i).get("Account").toString());            cell.setCellStyle(cs2);            cell = row.createCell(3);            cell.setCellValue(Double.parseDouble(list.get(i).get("Amount").toString()));            cell.setCellStyle(cs2);            cell = row.createCell(4);            cell.setCellValue(list.get(i).get("NAME").toString());            cell.setCellStyle(cs2);            cell = row.createCell(5);            cell.setCellValue(list.get(i).get("UsedTime") == null ? "未使用" : df.format(list.get(i).get("UsedTime")).toString());            cell.setCellStyle(cs2);            cell = row.createCell(6);            cell.setCellValue(df.format(list.get(i).get("BlankOutTime")).toString());            cell.setCellStyle(cs2);        }        ByteArrayOutputStream os = new ByteArrayOutputStream();        try {            wb.write(os);        } catch (IOException e) {            e.printStackTrace();        }        byte[] content = os.toByteArray();        InputStream is = new ByteArrayInputStream(content);        // 设置response参数,可以打开下载页面        response.reset();        response.setContentType("application/vnd.ms-excel;charset=utf-8");        response.setHeader("Content-Disposition", "attachment;filename=" + new String((list.get(0).get("NAME").toString() + ".xls").getBytes(), "iso-8859-1"));        ServletOutputStream out = response.getOutputStream();        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        try {            bis = new BufferedInputStream(is);            bos = new BufferedOutputStream(out);            byte[] buff = new byte[2048];            int bytesRead;            // Simple read/write loop.            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {                bos.write(buff, 0, bytesRead);            }        } catch (final IOException e) {            throw e;        } finally {            if (bis != null)                bis.close();            if (bos != null)                bos.close();        }        return null;    }


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信上买东西不给退货怎么办 微信买东西转红包被骗怎么办 买手机分期被骗了怎么办 苹果手机摔弯了怎么办 6s主板摔弯了怎么办 京东显示器坏了怎么办 苹果6s手机弯了怎么办 苹果手机变弯了怎么办 苹果手机屏幕摔碎了怎么办 苹果6p后壳弯曲怎么办 苹果手机后壳有点弯曲怎么办 苹果手机x弯了怎么办 苹果8手机弯了怎么办 苹果8p手机弯了怎么办? 美版iphone弯了怎么办 oppor9手机听筒声音小怎么办 苹果7摔弯了屏幕怎么办 苹果手机压弯了怎么办 京东买的电脑坏了怎么办 拼多多低价乱价怎么办 厂家不通过经销商直接发货怎么办 媳妇吵架说我全家有病怎么办 全家都不尊重媳妇怎么办 招商闪电贷有额度不通过怎么办 闪电贷页面登录不进去怎么办 91借钱极速逾期怎么办 论文没过拿不到毕业证怎么办 大学论文不过拿不到毕业证怎么办 转店被黑中介骗了钱怎么办? 被星外转铺骗了怎么办 店铺转了后悔了怎么办 商铺转让不出去怎么办? 和包券密码丢失怎么办 天猫购物卷兑换不了怎么办 淘宝新店每天只有几个访客怎么办 注册淘宝企业店铺需要审核怎么办 淘宝店铺被投诉知识产权怎么办 一般违规扣48分怎么办 金税盘处于报税期不能开票怎么办 小规模税率开错了怎么办 我是代购卖家被买家投诉偷税怎么办