ytr_卫庄 2016-02-25 03:39 采纳率: 0%
浏览 2477

为什么java导出excel表格没有弹出下载框

/** 导出存支列表 */
public void exportDepositList(){
HttpServletResponse response = Struts2Utils.getResponse();
ServletOutputStream sos = null;
WritableWorkbook wb = null;
List depositList = null;
String str = getSql();
depositList = depositService.getDepositForList(str, -1, -1);
String fileName = depositList.get(0).getDeptName()+"-存支列表.xls";
try {

        if(deposit == null){return;}
        float zsr = 0;float zzc = 0;
        sos = response.getOutputStream();
        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
        wb = Workbook.createWorkbook(sos);
        WritableSheet ws = wb.createSheet("存支列表",0);

        WritableCellFormat wcfTitle = new WritableCellFormat();
        wcfTitle.setBackground(Colour.GRAY_25);

        ws.setRowView(0, 600, false); //设置行高
        ws.setColumnView(0, 20);
        ws.setColumnView(1, 20);
        ws.setColumnView(2, 20);
        ws.setColumnView(3, 6);
        ws.setColumnView(6, 13);
        ws.setColumnView(8, 15);
        ws.setColumnView(10, 15);
        ws.setColumnView(12, 13);
        ws.setColumnView(13, 13);

        ws.addCell(new Label(0, 0, "类型", wcfTitle));
        ws.addCell(new Label(1, 0, "收入", wcfTitle));
        ws.addCell(new Label(2, 0, "支出", wcfTitle));
        ws.addCell(new Label(3, 0, "余额", wcfTitle));
        ws.addCell(new Label(4, 0, "存付单位名称", wcfTitle));
        ws.addCell(new Label(5, 0, "存支时间", wcfTitle));
        ws.addCell(new Label(6, 0, "订单号", wcfTitle));
        ws.addCell(new Label(7, 0, "备注", wcfTitle));
        ws.addCell(new Label(8, 0, "入账", wcfTitle));
        ws.addCell(new Label(9, 0, "核算号", wcfTitle));
        ws.addCell(new Label(10, 0, "核定", wcfTitle));
        ws.addCell(new Label(11, 0, "收款已用", wcfTitle));
        ws.addCell(new Label(12, 0, "编号", wcfTitle));
        int toursCount = 1;
        for (DepositEntity deposit : depositList) {
            ws.addCell(new Label(0, toursCount, deposit.getDepoType()));
            if(deposit.getMtype() == 1){
                ws.addCell(new Label(1, toursCount, String.valueOf(deposit.getMoney())));
                zsr += deposit.getMoney();
            }else if(deposit.getMtype() == 0){
                ws.addCell(new Label(2, toursCount, String.valueOf(deposit.getMoney())));
                zzc += deposit.getMoney();
            }
            ws.addCell(new Label(3, toursCount, String.valueOf(deposit.getEmoney())));
            ws.addCell(new Label(4, toursCount, deposit.getCustomer()));
            ws.addCell(new Label(5, toursCount, deposit.getAddTime()));
            ws.addCell(new Label(6, toursCount, deposit.getOrderNum()));
            ws.addCell(new Label(7, toursCount, deposit.getRemark()));
            if(deposit.getIsAccounte() == 0){ws.addCell(new Label(8, toursCount, ""));}else{ws.addCell(new Label(8, toursCount, "√"));}
            ws.addCell(new Label(9, toursCount, deposit.getCheckBatchNum()));
            ws.addCell(new Label(10, toursCount, String.valueOf(deposit.getHmoney())));
            ws.addCell(new Label(11, toursCount, String.valueOf(deposit.getYmoney())));
            ws.addCell(new Label(12, toursCount, deposit.getDepoNum()));

            toursCount++;
        }
        ws.addCell(new Label(2, toursCount+5, "统计"));
        ws.addCell(new Label(4, toursCount+5, "总收入:"+String.valueOf(zsr)));
        ws.addCell(new Label(6, toursCount+5, "总支出:"+String.valueOf(zzc)));
        ws.addCell(new Label(8, toursCount+5, "余额:"+String.valueOf(zsr-zzc)));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(wb != null){
            try {
                wb.write();
                wb.close();
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }
        if(sos != null){
            try {
                sos.flush();
                sos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
  • 写回答

1条回答 默认 最新

  • qq_33741655 2016-03-16 08:39
    关注

    我也没有弹出,直接将地址写死了再用下载的方法去做报一堆乱码 如果解决了麻烦告诉一下方法啊

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?