NutzCN Logo
问答 response已关闭错误...
发布于 2598天前 作者 蛋蛋的忧伤 1812 次浏览 复制 上一个帖子 下一个帖子
标签:

我做导出功能,mvc入口方法查到数据调用了另外一个方法进行输出excel,但是报了一个错误.代码:

 /**
     * 导出
     * @Author wqh
     * @return
     */
    @At
    public void export(@Attr("_user_id") int _user_id, @Param("channel")String channel,HttpServletResponse resp){
        Cnd cnd = SysUtils.autoCnd(Earnings.class);
        System.out.println(cnd.getCri().toString());
        Cnd cnd2 = null;
        Cnd cnd3 =null;
        Cnd cnd4 =null;
        if(Strings.isBlank(cnd.getCri().toString())){
            cnd2= Cnd.NEW();
            cnd3= Cnd.NEW();
            cnd4 = Cnd.NEW();
        }else{
            cnd2= Cnd.where(cnd.where());
            cnd3= Cnd.where(cnd.where());
            cnd4 = Cnd.where(cnd.where());
        }

        Result result = Result.ok();
        if (!SecurityUtils.getSubject().hasRole(SysConfig.SYS_ROLE)) {
            cnd3.and(Earnings.SYS_ID, "=", _user_id);
        }
        else{
//            List<SqlExpression> sqlExpressions = cnd.where().cloneExps();
//            SqlExpressionGroup where = cnd.where();
////            System.out.println(where.toString());
//            Object sum = 0;


//            if(where.getExps().size()>1){
//                sum  = dao().func2(Earnings.class, "sum", Earnings.EARNINGS,cnd2.and(Earnings.SYS_ID, "=", 0));
//            }else{
//                sum = dao().func2(Earnings.class, "sum", Earnings.EARNINGS,Cnd.where(Earnings.SYS_ID, "=", 0));
//            }
//            if(sum == null){
//                sum = 0;
//            }
////            Object sum = dao().func2(Earnings.class, "sum", Earnings.EARNINGS,Cnd.where(Earnings.SYS_ID, "=", 0));
//            BigDecimal sum2 = new BigDecimal(String.valueOf(sum));
//            BigDecimal bigDecimal = sum2.setScale(2, RoundingMode.HALF_UP);
//            String s = bigDecimal.toString();
//            result.setv("sum",sum);
        }
        cnd3.desc(Earnings.CREATE_TIME);
//        Pager pager = SysUtils.pager();
        List<Earnings> list = SysUtils.dao().query(Earnings.class, cnd3, null);
//        pager.setRecordCount(SysUtils.dao().count(Earnings.class, cnd3));

        for(Earnings e : list){
            if(e.getSys_id() == 0){
                e.setEgrnName("公司");
            }else{
                if(e.getSys_id() != null){
                    Agent age = dao().fetch(Agent.class,e.getSys_id());
                    if(age == null){
                        e.setEgrnName("未知");
                    }else{
                        e.setEgrnName("代理:"+age.getUsername());
                    }
                }else{
                    e.setEgrnName("未知代理");
                }
            }
        }
//        Object sum = dao().func2(Earnings.class, "sum", Earnings.EARNINGS,cnd4);
//        result.put(PAGER, pager);
        List<Earnings> list2 = new ArrayList<Earnings>();
        for(Earnings ear:list){
            String order_no = ear.getOrder_no();
            if(!Strings.isBlank(order_no)){
                Order fetch = dao().fetch(Order.class, Cnd.where(Order.ORDER_NO, "=", order_no));
                if(fetch!=null){
                    if(!Strings.isBlank(channel)){
                        if(channel.equals(fetch.getChannel())){
                            ear.setChannel(fetch.getChannel());
                            list2.add(ear);
                        }
                    }else{
                        ear.setChannel(fetch.getChannel());
                    }

                }else{
                    ear.setChannel(" ");
                }

            }else{
                ear.setChannel(" ");
            }
        }

   
        if(Strings.isBlank(channel)){
//            result.put(LIST, list);
            exportExcel(list,resp);
        }else{
//            result.put(LIST, list2);
            exportExcel(list2,resp);
        }
    }

    /**
     * 导出exportExcel
     * @Author  wqh
     * @param list
     * @param resp
     */
    public void exportExcel(List<Earnings> list, HttpServletResponse resp){
        try {
            try (OutputStream out = Mvcs.getResp().getOutputStream(); HSSFWorkbook workbook = new HSSFWorkbook();) {

                resp.addHeader("content-type", "application/shlnd.ms-excel;charset=utf-8");
                resp.addHeader("content-disposition", "attachment; filename=earnings.xls");
                Sheet sheet = workbook.createSheet("信息");

                int row_num = 1;
                Row onerow = sheet.createRow(row_num);

                Cell order_no = onerow.createCell(0);
                order_no.setCellValue("交易单号");
                Cell mear_no = onerow.createCell(1);
                mear_no.setCellValue("商户号");
                Cell mear_name = onerow.createCell(2);
                mear_name.setCellValue("商户姓名");
                Cell total_price = onerow.createCell(3);
                total_price.setCellValue("订单金额");
                Cell real_price = onerow.createCell(4);
                real_price.setCellValue("实际到账");
                Cell earnings = onerow.createCell(5);
                earnings.setCellValue("收益");
                Cell from_cardno = onerow.createCell(6);
                from_cardno.setCellValue("付款卡号");
                Cell to_cardno = onerow.createCell(7);
                to_cardno.setCellValue("收款卡号");
                Cell ear_name = onerow.createCell(8);
                ear_name.setCellValue("受益人");
                Cell channe = onerow.createCell(9);
                channe.setCellValue("通道");
                Cell createTime = onerow.createCell(10);
                createTime.setCellValue("创建时间");

                row_num++;
                for(Earnings ear:list){

                    Row line = sheet.createRow(row_num);

                    Cell orderNO = line.createCell(0);
                    orderNO.setCellValue(ear.getOrder_no());
                    Cell mear_No = line.createCell(1);
                    mear_No.setCellValue(ear.getMerchant_name());
                    Cell mear_Name = line.createCell(2);
                    mear_Name.setCellValue(ear.getMerchant_name());
                    Cell total_Price = line.createCell(3);
                    total_Price.setCellValue(ear.getTotal_price());

                    Cell real_Price = line.createCell(4);
                    real_Price.setCellValue(ear.getReal_price());

                    Cell earningS = line.createCell(5);
                    earningS.setCellValue(ear.getEarnings());

                    Cell from_cardNo = line.createCell(6);
                    from_cardNo.setCellValue(ear.getBankcard());

                    Cell to_cardNo = line.createCell(7);
                    to_cardNo.setCellValue(ear.getTobankcard());

                    Cell ear_Name = line.createCell(8);
                    ear_Name.setCellValue(ear.getEgrnName());

                    Cell channE = line.createCell(9);
                    channE.setCellValue(ear.getChannel());

                    Cell createtime = line.createCell(10);
                    createtime.setCellValue(ear.getCreate_time());

                    row_num++;
                }
             //==============在这里报错===================
                workbook.write(out); // 输出......
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

错误:

[WARN ] 11:52:40.089 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/earnings/export :
java.lang.IllegalStateException: Cannot forward after response has been committed
	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328)
	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
	at com.wangzc.mvc.view.HtmlPathView.render(HtmlPathView.java:50)
	at org.nutz.mvc.impl.processor.ViewProcessor.process(ViewProcessor.java:66)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.wangzc.mvc.processor.NutShiroProcessor.process(NutShiroProcessor.java:53)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at com.wangzc.mvc.processor.LogTimeProcessor.process(LogTimeProcessor.java:25)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
添加回复
请先登陆
回到顶部