jsp下载Excel问题

jsp中下载Excel

此处使用JavaScript的open()函数,后台使用jxl动态生成Excel文件流。

前台:window.opne("downExcelAction");

后台:

public void downExcelAction() throws RowsExceededException, WriteException{
        System.out.println("【导出EXCEL表格】");
        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        String result = "success";
        //查询出数据
        //填充到excel中
        OutputStream os;
        WritableWorkbook wbook;
        try {
            os = response.getOutputStream();
            response.reset();// 清空输出流
            response.setHeader("Content-disposition", "attachment;filename="
                    + new String(fileName.getBytes("GB2312"), "ISO8859_1"));// 设定输出文件头,<span style="font-size:18px;color:#FF6666;">此处可以解决文件名中文乱码问题</span>
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os);//建立excel文件
            WritableSheet wsheet = wbook.createSheet("会员卡信息", 0);//创建sheet
            
            //定义单元格样式
            WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false,
                    UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
            WritableCellFormat wcf = new WritableCellFormat(wf);
            
            //设置对齐方式
            wcf.setAlignment(jxl.format.Alignment.CENTRE);
            
            //冻结第一行
            wsheet.getSettings().setVerticalFreeze(1);
            wsheet.addCell(new Label(0, 0, "内码", wcf));
            wsheet.addCell(new Label(1, 0, "会员卡", wcf));
            wsheet.addCell(new Label(2, 0, "代理商", wcf));
            wsheet.addCell(new Label(3, 0, "会员卡信息", wcf));
            wsheet.addCell(new Label(4, 0, "会员卡状态", wcf));
            wsheet.addCell(new Label(5, 0, "消费信息", wcf));
            wsheet.addCell(new Label(6, 0, "当前余额", wcf));
            wsheet.addCell(new Label(7, 0, "时间", wcf));
            
            //返回excel
            wbook.write();
            wbook.close();
            os.close();
        } catch (Exception e) {
            result = "下载出现异常,请联系管理员!";
            e.printStackTrace();
        }
        
    }


你可能感兴趣的:(jsp下载Excel问题)