2 feifan1985 feifan1985 于 2016.03.01 10:32 提问

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个回答

rui888
rui888   Ds   Rxr 2016.03.01 10:59
rui888
rui888   Ds   Rxr 2016.03.01 11:01
xionglangs
xionglangs   Rxr 2016.03.01 11:08

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

qq_33741655
qq_33741655   2016.03.16 15:44

解决了吗?我现在也碰到这个问题了 问一下你的解决方法是什么啊?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!