前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >EXCLS 的导出 下载

EXCLS 的导出 下载

作者头像
斯文的程序
发布2019-11-07 16:59:10
3800
发布2019-11-07 16:59:10
举报
文章被收录于专栏:带你回家带你回家带你回家
/**
     *  下载
     * @param response
     * @param request
     * @return
     * @throws Exception
     */
    /*@RequestMapping(value="downExels")
    public String typeExls(HttpServletResponse response
            ,HttpServletRequest request)throws Exception{
        
        
        // 获得要导出的数据集
        
        // 创建excel工作簿
        Workbook wb = new HSSFWorkbook();
        // 创建第一个sheet(页),并命名
        Sheet sheet = wb.createSheet("woxs");

        // 手动设置列宽。第一个参数表示要为第几列设;,第二个参数表示列的宽度,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);
 
        // 创建列(每行里的单元格)
        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 < 5; i++) {

            // Row 行,Cell 方格 , Row 和 Cell 都是从0开始计数的
            // 创建一行,在页sheet上
            row = sheet.createRow((short) i + 2);
            // 在row行上创建一个方格
            cell = row.createCell(0);
            cell.setCellValue("123");
            cell.setCellStyle(cs2);

            cell = row.createCell(1);
            cell.setCellValue("ww");
            cell.setCellStyle(cs2);

            cell = row.createCell(2);
            cell.setCellValue("123");
            cell.setCellStyle(cs2);

            cell = row.createCell(3);
            cell.setCellValue("22");
            cell.setCellStyle(cs2);

            cell = row.createCell(4);
            cell.setCellValue("wo de ");
            cell.setCellStyle(cs2);

            cell = row.createCell(5);
            cell.setCellValue("s");
            cell.setCellStyle(cs2);

            cell = row.createCell(6);
            cell.setCellValue("123");
            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(("ss".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;
    
}*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档