feifan1985 2016-03-01 02:32 采纳率: 0%
浏览 3027

java poi excel导出 弹出框不出来!

下面是代码.所有代码都能执行完,没有报错,也不弹出保存窗口!求助...

 /**
     * 导出
     */
    public void exportData(HttpServletResponse response) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = null;
        // 创建一行
        HSSFRow row = null;
        HSSFCell cell = null;
        HSSFCellStyle stringstyle = null;
        HSSFCellStyle headStyle = null;
        headStyle = workbook.createCellStyle();
        headStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        headStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        headStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        headStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        headStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
        headStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        headStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        HSSFFont headfont = workbook.createFont();
        // 设置头部字体为宋体
        headfont.setFontName("宋体");
        // 粗体
        headfont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headfont.setFontHeightInPoints((short) 11);
        // 单元格样式使用字体
        headStyle.setFont(headfont);
        HSSFFont datafont = workbook.createFont();
        stringstyle = workbook.createCellStyle();
        stringstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        stringstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        stringstyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        stringstyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        stringstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        // 单元格样式使用字体
        stringstyle.setFont(datafont);
        sheet = (HSSFSheet) workbook.createSheet();
        sheet.setDefaultColumnWidth(16);

        //合并起始行,起始列,结束行,结束列
        CellRangeAddress title1 = new CellRangeAddress(0, 0, 0, 10);
        sheet.addMergedRegion(title1);

        //设置第一行
        row = (HSSFRow) sheet.createRow(0);
        cell = (HSSFCell) row.createCell(0);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("部室员工绩效合约考核汇总表");
        cell.setCellStyle(headStyle);

        //合并起始行,起始列,结束行,结束列
//      CellRangeAddress row1 = new CellRangeAddress(1, 0, 1, 1);
//      sheet.addMergedRegion(row1);

        //设置第二行
        row = (HSSFRow) sheet.createRow(1);

        cell = (HSSFCell) row.createCell(0);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("项目");
        cell.setCellStyle(headStyle);

        cell = (HSSFCell) row.createCell(1);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("员工1");
        cell.setCellStyle(headStyle);

        cell = (HSSFCell) row.createCell(2);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("员工2");
        cell.setCellStyle(headStyle);


        //合并起始行,起始列,结束行,结束列
//      CellRangeAddress row2 = new CellRangeAddress(2, 0, 2, 1);
//      sheet.addMergedRegion(row2);
        //设置第三行
        row = (HSSFRow) sheet.createRow(2);
        cell = (HSSFCell) row.createCell(0);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("一、主体指标");
        cell.setCellStyle(headStyle);

        cell = (HSSFCell) row.createCell(1);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("111");
        cell.setCellStyle(headStyle);

        cell = (HSSFCell) row.createCell(1);
        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue("222");
        cell.setCellStyle(headStyle);

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            workbook.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(("季度绩效工资分配.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);
            }
            bos.flush();
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
    }
}
  • 写回答

4条回答

  • 编程爱好者熊浪 2016-03-01 03:08
    关注

    你用了ajax吧,下载其实已经下载完了,但不能有返回值,所以其实没有进行弹出下载显示框,你看下这个,http://blog.csdn.net/xionglangs/article/details/49922923。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败