easyui下彈出窗口下載文件

jsp

 function kpdc1(){
             var rows = $("#xxx").datagrid('getRows');
             var datarows = JSON.stringify(rows);
            
             var url = 'url?datarows='+datarows
                window.location.href=url;
            
       }  

controller

@RequestMapping(value="xxx", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView xxx(Integer id)
            throws DocumentException, IOException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date());
        try {
            YhcwProcessBgbxAndFpWebClient webClient = WebClient.getCW(YhcwProcessBgbxAndFpWebClient.class);
            List<CxInvoicerecordParam> cxInvoicerecordParams = Json2Bean.getListBean(webClient.getInvoicerecord(id),
                    CxInvoicerecordParam.class);
            TemplateWebClient templatewebClient = WebClient.get(TemplateWebClient.class);
            Map<String, String> map = new HashMap<>();
            map.put("sysCompanyId", getCurrentUser().getSysCompany().getId().toString());
            map.put("templatetype", "3");
            List<Template> templates = Json2Bean.getListBean(
                    templatewebClient.getMorePropertyEqual(BeanJson.bean2json(map), "id", true), Template.class);
            String xml = templates.get(0).getContent();
            System.out.println(xml.toString());
            xml = xml.replace("count", cxInvoicerecordParams.size() + "");
            String fpString = xml.substring(xml.indexOf("<Fp>"), xml.lastIndexOf("</Fp>"));
            xml = xml.replace("bianma", "DJZ" + dateString + "-" + id.toString());

            System.out.println(fpString.toString());
            String fpStringtobianliang = xml.substring(xml.indexOf("<Fp>"), xml.lastIndexOf("</Fp>"));
            String targetEle = "corn";
            for (int j = 0; j < searchEleNum(fpString, targetEle); j++) {
                System.out.println(searchEleNum(fpString, targetEle));
                System.out.println(searchEleNum(fpString, targetEle));
                 String fristname = fpStringtobianliang.substring(fpStringtobianliang.lastIndexOf("<", fpStringtobianliang.indexOf(targetEle)), fpStringtobianliang.indexOf(">", fpStringtobianliang.indexOf(targetEle))+1);
                 String lastname = fpStringtobianliang.substring(fpStringtobianliang.lastIndexOf("<", fpStringtobianliang.indexOf(targetEle)), fpStringtobianliang.indexOf(">", fpStringtobianliang.indexOf(targetEle))+1);
                 String name = fristname.substring(fristname.lastIndexOf(">", fristname.indexOf(targetEle))+1, fristname.indexOf(targetEle)-1);
                 String countname = fristname.substring(fristname.lastIndexOf("(", fristname.indexOf(targetEle)),fristname.indexOf(")", fristname.indexOf(targetEle))+1);
                 String count = countname.substring(targetEle.length()+2,countname.length()-1);
                Map<String, BigDecimal> params = new HashMap<String, BigDecimal>();
                System.out.println(name);
                System.out.println(count);
                String IP4 = "[\\+\\-\\*\\/]";
                String[] nametoarray = name.split(IP4);
                for (int k = 0; k < nametoarray.length; k++) {
                    System.out.println(nametoarray[k]);
                    if (nametoarray[k].equals("serviceCharge")) {
                        if (cxInvoicerecordParams.get(0).getServiceCharge() != null
                                && cxInvoicerecordParams.get(0).getServiceCharge().toString().length() > 0) {
                            params.put(nametoarray[k], cxInvoicerecordParams.get(0).getServiceCharge());
                        } else {
                            params.put(nametoarray[k], BigDecimal.ZERO);
                        }
                    } else if (nametoarray[k].equals("paySocia")) {
                        if (cxInvoicerecordParams.get(0).getPaySocia() != null
                                && cxInvoicerecordParams.get(0).getPaySocia().toString().length() > 0) {
                            params.put(nametoarray[k], cxInvoicerecordParams.get(0).getPaySocia());
                        } else {
                            params.put(nametoarray[k], BigDecimal.ZERO);
                        }
                    } else if (nametoarray[k].equals("providentFund")) {
                        if (cxInvoicerecordParams.get(0).getProvidentFund() != null
                                && cxInvoicerecordParams.get(0).getProvidentFund().toString().length() > 0) {
                            params.put(nametoarray[k], cxInvoicerecordParams.get(0).getProvidentFund());
                        } else {
                            params.put(nametoarray[k], BigDecimal.ZERO);
                        }
                    } else if (nametoarray[k].equals("payWages")) {
                        if (cxInvoicerecordParams.get(0).getPayWages() != null
                                && cxInvoicerecordParams.get(0).getPayWages().toString().length() > 0) {
                            params.put(nametoarray[k], cxInvoicerecordParams.get(0).getPayWages());
                        } else {
                            params.put(nametoarray[k], BigDecimal.ZERO);
                        }
                    } else if (nametoarray[k].equals("residualPremium")) {
                        if (cxInvoicerecordParams.get(0).getResidualPremium() != null
                                && cxInvoicerecordParams.get(0).getResidualPremium().toString().length() > 0) {
                            params.put(nametoarray[k], cxInvoicerecordParams.get(0).getResidualPremium());
                        } else {
                            params.put(nametoarray[k], BigDecimal.ZERO);
                        }
                    } else if (nametoarray[k].equals("totalPrice")) {
                        if (cxInvoicerecordParams.get(0).getTotalPrice() != null
                                && cxInvoicerecordParams.get(0).getTotalPrice().toString().length() > 0) {
                            params.put(nametoarray[k], cxInvoicerecordParams.get(0).getTotalPrice());
                        } else {
                            params.put(nametoarray[k], BigDecimal.ZERO);
                        }
                    } else if (nametoarray[k].equals("incomePrice")) {
                        if (cxInvoicerecordParams.get(0).getIncomePrice() != null
                                && cxInvoicerecordParams.get(0).getIncomePrice().toString().length() > 0) {
                            params.put(nametoarray[k], cxInvoicerecordParams.get(0).getIncomePrice());
                        } else {
                            params.put(nametoarray[k], BigDecimal.ZERO);
                        }
                    } else {
                        params.put(nametoarray[k], new BigDecimal(nametoarray[k]));
                    }
                }
                System.out.println(name);
                
                lastname = lastname.replace(name, (new BigDecimal(MathFormulaUtil.getResult(name, params)).setScale(Integer.parseInt(count), BigDecimal.ROUND_HALF_UP)).toString());
                lastname = lastname.replace(countname,"");
                System.out.println(lastname);
                 xml = xml.replace(fristname, lastname);
                fpStringtobianliang = fpStringtobianliang.substring(fpStringtobianliang.indexOf(targetEle) + 1);
            }
            if (cxInvoicerecordParams.get(0).getSysCompanyId() != null) {
                xml = xml.replace("sysCompanyId", cxInvoicerecordParams.get(0).getSysCompanyId1());
            } else {
                xml = xml.replace("sysCompanyId", "");
            }
            if (cxInvoicerecordParams.get(0).getInvoicetag() != null
                    && cxInvoicerecordParams.get(0).getInvoicetag().length() > 0) {
                xml = xml.replace("invoicetag", cxInvoicerecordParams.get(0).getInvoicetag());
            } else {
                xml = xml.replace("invoicetag", "");
            }
            if (cxInvoicerecordParams.get(0).getInvoiceType() != null
                    && cxInvoicerecordParams.get(0).getInvoiceType().length() > 0) {
                xml = xml.replace("invoiceType", cxInvoicerecordParams.get(0).getInvoiceType());
            } else {
                xml = xml.replace("invoiceType", "");
            }
            if (cxInvoicerecordParams.get(0).getPayType() != null
                    && cxInvoicerecordParams.get(0).getPayType().length() > 0) {
                xml = xml.replace("payType", cxInvoicerecordParams.get(0).getPayType());
            } else {
                xml = xml.replace("payType", "");
            }
            if (cxInvoicerecordParams.get(0).getServiceCharge() != null
                    && cxInvoicerecordParams.get(0).getServiceCharge().toString().length() > 0) {
                xml = xml.replace("serviceCharge", cxInvoicerecordParams.get(0).getServiceCharge().toString());
            } else {
                xml = xml.replace("serviceCharge", "");
            }
            if (cxInvoicerecordParams.get(0).getPaySocia() != null
                    && cxInvoicerecordParams.get(0).getPaySocia().toString().length() > 0) {
                xml = xml.replace("paySocia", cxInvoicerecordParams.get(0).getPaySocia().toString());
            } else {
                xml = xml.replace("paySocia", "");
            }
            if (cxInvoicerecordParams.get(0).getProvidentFund() != null
                    && cxInvoicerecordParams.get(0).getProvidentFund().toString().length() > 0) {
                xml = xml.replace("providentFund", cxInvoicerecordParams.get(0).getProvidentFund().toString());
            } else {
                xml = xml.replace("providentFund", "");
            }
            if (cxInvoicerecordParams.get(0).getPayWages() != null
                    && cxInvoicerecordParams.get(0).getPayWages().toString().length() > 0) {
                xml = xml.replace("payWages", cxInvoicerecordParams.get(0).getPayWages().toString());
            } else {
                xml = xml.replace("payWages", "");
            }
            if (cxInvoicerecordParams.get(0).getResidualPremium() != null
                    && cxInvoicerecordParams.get(0).getResidualPremium().toString().length() > 0) {
                xml = xml.replace("residualPremium", cxInvoicerecordParams.get(0).getResidualPremium().toString());
            } else {
                xml = xml.replace("residualPremium", "");
            }
            if (cxInvoicerecordParams.get(0).getTotalPrice() != null
                    && cxInvoicerecordParams.get(0).getTotalPrice().toString().length() > 0) {
                xml = xml.replace("totalPrice", cxInvoicerecordParams.get(0).getTotalPrice().toString());
            } else {
                xml = xml.replace("totalPrice", "");
            }
            if (cxInvoicerecordParams.get(0).getIncomePrice() != null
                    && cxInvoicerecordParams.get(0).getIncomePrice().toString().length() > 0) {
                xml = xml.replace("incomePrice", cxInvoicerecordParams.get(0).getIncomePrice().toString());
            } else {
                xml = xml.replace("incomePrice", "");
            }
            if (cxInvoicerecordParams.get(0).getMailinfo() != null
                    && cxInvoicerecordParams.get(0).getMailinfo().length() > 0) {
                xml = xml.replace("mailinfo", cxInvoicerecordParams.get(0).getMailinfo());
            } else {
                xml = xml.replace("mailinfo", "");
            }
            if (cxInvoicerecordParams.get(0).getRemarks() != null
                    && cxInvoicerecordParams.get(0).getRemarks().length() > 0) {
                xml = xml.replace("remarks", cxInvoicerecordParams.get(0).getRemarks());
            } else {
                xml = xml.replace("remarks", "");
            }
            if (cxInvoicerecordParams.get(0).getBillingStart() != null
                    && cxInvoicerecordParams.get(0).getBillingStart().length() > 0) {
                xml = xml.replace("billingStart", cxInvoicerecordParams.get(0).getBillingStart());
            } else {
                xml = xml.replace("billingStart", "");
            }
            if (cxInvoicerecordParams.get(0).getTaxnNumber() != null
                    && cxInvoicerecordParams.get(0).getTaxnNumber().length() > 0) {
                xml = xml.replace("taxnNumber", cxInvoicerecordParams.get(0).getTaxnNumber());
            } else {
                xml = xml.replace("taxnNumber", "");
            }
            if (cxInvoicerecordParams.get(0).getUnitAddress() != null
                    && cxInvoicerecordParams.get(0).getUnitAddress().length() > 0) {
                xml = xml.replace("unitAddress", cxInvoicerecordParams.get(0).getUnitAddress());
            } else {
                xml = xml.replace("unitAddress", "");
            }
            if (cxInvoicerecordParams.get(0).getWorkTelephone() != null
                    && cxInvoicerecordParams.get(0).getWorkTelephone().length() > 0) {
                xml = xml.replace("workTelephone", cxInvoicerecordParams.get(0).getWorkTelephone());
            } else {
                xml = xml.replace("workTelephone", "");
            }
            if (cxInvoicerecordParams.get(0).getDepositBank() != null
                    && cxInvoicerecordParams.get(0).getDepositBank().length() > 0) {
                xml = xml.replace("depositBank", cxInvoicerecordParams.get(0).getDepositBank());
            } else {
                xml = xml.replace("depositBank", "");
            }
            if (cxInvoicerecordParams.get(0).getBankAccount() != null
                    && cxInvoicerecordParams.get(0).getBankAccount().length() > 0) {
                xml = xml.replace("bankAccount", cxInvoicerecordParams.get(0).getBankAccount());
            } else {
                xml = xml.replace("bankAccount", "");
            }
            if (cxInvoicerecordParams.get(0).getCheckTaker() != null
                    && cxInvoicerecordParams.get(0).getCheckTaker().length() > 0) {
                xml = xml.replace("checkTaker", cxInvoicerecordParams.get(0).getCheckTaker());
            } else {
                xml = xml.replace("checkTaker", "");
            }
            if (cxInvoicerecordParams.get(0).getTakerCall() != null
                    && cxInvoicerecordParams.get(0).getTakerCall().length() > 0) {
                xml = xml.replace("takerCall", cxInvoicerecordParams.get(0).getTakerCall());
            } else {
                xml = xml.replace("takerCall", "");
            }
            if (cxInvoicerecordParams.get(0).getTicketAddress() != null
                    && cxInvoicerecordParams.get(0).getTicketAddress().length() > 0) {
                xml = xml.replace("ticketAddress", cxInvoicerecordParams.get(0).getTicketAddress());
            } else {
                xml = xml.replace("ticketAddress", "");
            }
            System.out.println(xml);
            Map<String,Object> model = new HashMap<String, Object>();
            model.put("content", xml);

            return new ModelAndView(new ExportXmlFileView(),model);

ExportXmlFileView

public class ExportXmlFileView extends AbstractView{  
     
 
    
    @Override
    protected void renderMergedOutputModel(Map<String, Object> errorStrs,HttpServletRequest request, HttpServletResponse response)throws Exception {
        // TODO Auto-generated method stub
        //buildTxtDocument(request, response);  
        
        // Set the content type.  
        String excelName = "fapiao.xml";
        // 設置response方式,使執行此controller時候自動出現下載頁面,而非直接使用excel打開
        response.reset();
        response.setContentType("APPLICATION/OCTET-STREAM");
//        response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(excelName, "UTF-8"));
        
        response.setHeader("Content-Disposition", "attachment; filename="+ new String(excelName.getBytes("GBK"),"ISO8859-1"));
          
        // Should we set the content length here?  
        // response.setContentLength(workbook.getBytes().length);  
        /*BufferedOutputStream buff = null;  
        StringBuffer write = new StringBuffer();  */
        // Flush byte array to servlet output stream.  
/*        buff = new BufferedOutputStream(out);  
        write.append(errorStrs.get("content"));  
        buff.write(write.toString().getBytes("UTF-8"));  
        buff.flush();  
        buff.close();  
          
        out.flush();  
        out.close(); */
        OutputStream out = response.getOutputStream();  
        SAXReader saxReader = new SAXReader();
        Document document= saxReader.read(new ByteArrayInputStream(errorStrs.get("content").toString().getBytes("UTF-8")));
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, format);// 創建寫出對象
        writer.write(document);// 寫出對象
        writer.close();
        out.flush();  
        out.close();
        response.flushBuffer();
    }

}

格式輸出是xml文件的格式,帶空格和換行

數據是從數據庫拿到的,注意:xml文件的格式一定要正確,不然會報錯!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章