madamqin
madamqin
2015-01-07 03:16
采纳率: 100%
浏览 3.2k
已采纳

extjs将gridPanel导出为excel文件,1000条以下可正常导出,超过1000条就不行

导出excel方法:

 exportResult:function(){
                var fileName = this.title;
                var vExportContent = this.getExcelXml();
                //alert(vExportContent);
                if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8 || Ext.isSafari
                        || Ext.isSafari2 || Ext.isSafari3) {
                    var fd = Ext.get('frmDummy');
                    if (!fd) {
                        fd = Ext.DomHelper.append(Ext.getBody(), {
                            tag : 'form',
                            method : 'post',
                            id : 'frmDummy',
                            action : '../exportexcel.jsp',
                            target : '_blank',
                            name : 'frmDummy',
                            cls : 'x-hidden',
                            cn : [{
                                tag : 'input',
                                name : 'fileName',
                                id : 'fileName',
                                type : 'hidden'
                            }, {
                                tag : 'input',
                                name : 'exportContent',
                                id : 'exportContent',
                                type : 'hidden'
                            }]
                        }, true);
                    }
                    fd.child('#fileName').set({
                        value : fileName
                    });
                    fd.child('#exportContent').set({
                        value : vExportContent
                    });
                    fd.dom.submit();
                } else {
                    document.location = 'data:application/vnd.ms-excel;base64,'
                            + Base64.encode(vExportContent);
                }
            },
            getExcelXml : function(includeHidden) {
                var worksheet = this.createWorksheet(includeHidden);
                var totalWidth = this.getColumnModel().getTotalWidth(includeHidden);
                return '<xml version="1.0" encoding="utf-8">'
                        + '<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office">'
                        + '<o:DocumentProperties><o:Title>'
                        + this.title
                        + '</o:Title></o:DocumentProperties>'
                        + '<ss:ExcelWorkbook>'
                        + '<ss:WindowHeight>'
                        + worksheet.height
                        + '</ss:WindowHeight>'
                        + '<ss:WindowWidth>'
                        + worksheet.width
                        + '</ss:WindowWidth>'
                        + '<ss:ProtectStructure>False</ss:ProtectStructure>'
                        + '<ss:ProtectWindows>False</ss:ProtectWindows>'
                        + '</ss:ExcelWorkbook>'
                        + '<ss:Styles>'
                        + '<ss:Style ss:ID="Default">'
                        + '<ss:Alignment ss:Vertical="Top" ss:WrapText="1" />'
                        + '<ss:Font ss:FontName="arial" ss:Size="10" />'
                        + '<ss:Borders>'
                        + '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" />'
                        + '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" />'
                        + '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" />'
                        + '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" />'
                        + '</ss:Borders>'
                        + '<ss:Interior />'
                        + '<ss:NumberFormat />'
                        + '<ss:Protection />'
                        + '</ss:Style>'
                        + '<ss:Style ss:ID="title">'
                        + '<ss:Borders />'
                        + '<ss:Font />'
                        + '<ss:Alignment ss:WrapText="1" ss:Vertical="Center" ss:Horizontal="Center" />'
                        + '<ss:NumberFormat ss:Format="@" />' + '</ss:Style>'
                        + '<ss:Style ss:ID="headercell">'
                        + '<ss:Font ss:Bold="1" ss:Size="10" />'
                        + '<ss:Alignment ss:WrapText="1" ss:Horizontal="Center" />'
                        + '<ss:Interior ss:Pattern="Solid" ss:Color="#A3C9F1" />'
                        + '</ss:Style>' + '<ss:Style ss:ID="even">'
                        + '<ss:Interior ss:Pattern="Solid" ss:Color="#CCFFFF" />'
                        + '</ss:Style>'
                        + '<ss:Style ss:Parent="even" ss:ID="evendate">'
                        + '<ss:NumberFormat ss:Format="yyyy-mm-dd" />' + '</ss:Style>'
                        + '<ss:Style ss:Parent="even" ss:ID="evenint">'
                        + '<ss:NumberFormat ss:Format="0" />' + '</ss:Style>'
                        + '<ss:Style ss:Parent="even" ss:ID="evenfloat">'
                        + '<ss:NumberFormat ss:Format="0.00" />' + '</ss:Style>'
                        + '<ss:Style ss:ID="odd">'
                        + '<ss:Interior ss:Pattern="Solid" ss:Color="#CCCCFF" />'
                        + '</ss:Style>' + '<ss:Style ss:Parent="odd" ss:ID="odddate">'
                        + '<ss:NumberFormat ss:Format="yyyy-mm-dd" />' + '</ss:Style>'
                        + '<ss:Style ss:Parent="odd" ss:ID="oddint">'
                        + '<ss:NumberFormat ss:Format="0" />' + '</ss:Style>'
                        + '<ss:Style ss:Parent="odd" ss:ID="oddfloat">'
                        + '<ss:NumberFormat ss:Format="0.00" />' + '</ss:Style>'
                        + '</ss:Styles>' + worksheet.xml + '</ss:Workbook>';
            }
            ,
            createWorksheet : function(includeHidden) {
                // Calculate cell data types and extra class names which affect
                // formatting
                var cellType = [];
                var cellTypeClass = [];
                var cm = this.getColumnModel();
                var totalWidthInPixels = 0;
                var colXml = '';
                var headerXml = '';
                var visibleColumnCountReduction = 0;
                var colCount = cm.getColumnCount();
                for (var i = 0; i < colCount; i++) {
                    if ((cm.getDataIndex(i) != '')
                            && (includeHidden || !cm.isHidden(i))) {
                        var w = cm.getColumnWidth(i)
                        totalWidthInPixels += w;
                        if (cm.getColumnHeader(i) === "") {
                            cellType.push("None");
                            cellTypeClass.push("");
                            ++visibleColumnCountReduction;
                        } else {
                            colXml += '<ss:Column ss:AutoFitWidth="1" ss:Width="' + w
                                    + '" />';
                            headerXml += '<ss:Cell ss:StyleID="headercell">'
                                    + '<ss:Data ss:Type="String">'
                                    + cm.getColumnHeader(i)
                                    + '</ss:Data>'
                                    + '<ss:NamedCell ss:Name="Print_Titles" /></ss:Cell>';
                            var fld = this.store.recordType.prototype.fields.get(cm
                                    .getDataIndex(i));
                            switch (fld.type) {
                                case "int" :
                                    cellType.push("Number");
                                    cellTypeClass.push("int");
                                    break;
                                case "float" :
                                    cellType.push("Number");
                                    cellTypeClass.push("float");
                                    break;
                                case "bool" :
                                case "boolean" :
                                    cellType.push("String");
                                    cellTypeClass.push("");
                                    break;
                                case "date" :
                                    cellType.push("DateTime");
                                    cellTypeClass.push("date");
                                    break;
                                default :
                                    cellType.push("String");
                                    cellTypeClass.push("");
                                    break;
                            }
                        }
                    }
                }
                var visibleColumnCount = cellType.length - visibleColumnCountReduction;

                var result = {
                    height : 9000,
                    width : Math.floor(totalWidthInPixels * 30) + 50
                };

                // Generate worksheet header details.
                var t = '<ss:Worksheet ss:Name="' + this.title + '">' + '<ss:Names>'
                        + '<ss:NamedRange ss:Name="Print_Titles" ss:RefersTo="=\''
                        + this.title + '\'!R1:R2" />' + '</ss:Names>'
                        + '<ss:Table x:FullRows="1" x:FullColumns="1"'
                        + ' ss:ExpandedColumnCount="' + (visibleColumnCount + 2)
                        + '" ss:ExpandedRowCount="' + (this.store.getCount() + 2)
                        + '">' + colXml + '<ss:Row ss:AutoFitHeight="1">' + headerXml
                        + '</ss:Row>';

                // Generate the data rows from the data in the Store
                for (var i = 0, it = this.store.data.items, l = it.length; i < l; i++) {
                    t += '<ss:Row>';
                    var cellClass = (i & 1) ? 'odd' : 'even';
                    r = it[i].data;
                    var k = 0;
                    for (var j = 0; j < colCount; j++) {
                        if ((cm.getDataIndex(j) != '')
                                && (includeHidden || !cm.isHidden(j))) {
                            var v = r[cm.getDataIndex(j)];
                            var rd = cm.getRenderer(j);
                            if (cellType[k] !== "None") {
                                t += '<ss:Cell ss:StyleID="' + cellClass
                                        + cellTypeClass[k] + '"><ss:Data ss:Type="'
                                        + cellType[k] + '">';
                                if(v==null){
                                    t+='';
                                }
                                else if (cellType[k] == 'DateTime') {
                                    t += v.format('Y-m-d');
                                } else {
                                    t += rd(v);
                                }
                                t += '</ss:Data></ss:Cell>';
                            }
                            k++;
                        }
                    }
                    t += '</ss:Row>';
                }

                result.xml = t
                        + '</ss:Table>'
                        + '<x:WorksheetOptions>'
                        + '<x:PageSetup>'
                        + '<x:Layout x:CenterHorizontal="1" x:Orientation="Landscape" />'
                        + '<x:Footer x:Data="Page &amp;P of &amp;N" x:Margin="0.5" />'
                        + '<x:PageMargins x:Top="0.5" x:Right="0.5" x:Left="0.5" x:Bottom="0.8" />'
                        + '</x:PageSetup>' + '<x:FitToPage />' + '<x:Print>'
                        + '<x:PrintErrors>Blank</x:PrintErrors>'
                        + '<x:FitWidth>1</x:FitWidth>'
                        + '<x:FitHeight>32767</x:FitHeight>' + '<x:ValidPrinterInfo />'
                        + '<x:VerticalResolution>600</x:VerticalResolution>'
                        + '</x:Print>' + '<x:Selected />'
                        + '<x:DoNotDisplayGridlines />'
                        + '<x:ProtectObjects>False</x:ProtectObjects>'
                        + '<x:ProtectScenarios>False</x:ProtectScenarios>'
                        + '</x:WorksheetOptions>' + '</ss:Worksheet>';
                return result;
            }

        跳转的jsp页面:
            <%@page import="java.net.URLEncoder"%>
<%
    response.reset();
    //request.setCharacterEncoding("UTF-8");
    String fileName = request.getParameter("fileName");
    fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
    //System.out.println(fileName);
    response.setHeader("Content-Type", "application/force-download");
    response.setHeader("Content-Type",
            "application/vnd.ms-excel;charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment;filename="
            + fileName + ".xls");
    out.print(request.getParameter("exportContent"));
%>

错误信息:request.getParameter("fileName") 取到的为空值,getParameter("exportContent")也为空值,跳转到jsp页面之前是有值的

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • showbo
    已采纳

    是不是内容大小超过你jsp最大限制了?或者服务器的最大上传大小限制。如asp默认是200KB。tomcat默认好像是2M

    tomcat传输大小受限制

    点赞 评论
  • lzp_lrp
    WorldMobile 2015-01-07 04:45

    应该是应用服务器的问题,检查一下里面的配置,是不是超了

    点赞 评论
  • madamqin
    madamqin 2015-01-08 05:45

    改了配置也不行,火狐浏览器可以,ie就不行

    点赞 评论

相关推荐