EasyUI1.5 Grid Export Excel

Source: Internet
Author: User
Tags base64

The

Export DataGrid includes only non-hidden and non-fixed columns (frozen).
1.js page initialization function initexport2excel ()

/** * Initialize export Excel */function Initexport2excel () {/** * Jquery easyui DataGrid JS Export Excel Modify from Extgrid export Excel allo WS for downloading * to Grid data (store) directly into Excel method:extracts data of * Gridpanel store, uses C
     Olumnmodel to construct XML Excel document, * Converts to Base64, then loads into a data URL link. * * * * * * @author Animal <EXTJS Support team> * * * * * * * * $.extend ($.fn.datagrid.methods, {getexcel
            Xml:function (JQ, param) {var worksheet = This.createworksheet (JQ, param);
            Alert ($ (JQ). DataGrid (' Getcolumnfields '));
            var totalwidth = 0;
            var cfs = $ (JQ). DataGrid (' Getcolumnfields '); for (var i = 1; i < Cfs.length. i++) {for (var i = 0; i < cfs.length; i++) {////////////////////correct this
            , which causes the first column to be lost, should be traversed from 0 to totalwidth + = $ (JQ). DataGrid (' Getcolumnoption ', Cfs[i]). width;
}//var totalwidth =            This.getcolumnmodel (). Gettotalwidth (Includehidden); Return ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?> ' +//XML asserts there is a problem to fix, note is Utf-8 code, if it is gb2312, need to modify the dynamic page file The write encoding ' <ss:workbook xmlns:ss= ' urn:schemas-microsoft-com:office:spreadsheet ' xmlns:x= ' Urn:schemas-mi Crosoft-com:office:excel "xmlns:o=" Urn:schemas-microsoft-com:office:office "> ' + ' <o:documentprop Erties><o:title> ' + param.title + ' </o:title></o:documentpropert
                    Ies> ' + ' <ss:ExcelWorkbook> ' + ' <ss:WindowHeight> '
                    + worksheet.height + ' </ss:WindowHeight> ' + ' <ss:WindowWidth> ' + worksheet.width + ' </ss:WindowWidth> ' + ' <ss:protectst Ructure>false</ss:protectstructure> ' + ' <ss:ProtectWindows>False</ss:ProtectWindows> ' + ' </ss:ExcelWorkbook> ' + ' <ss:Styles> ' + ' <ss:style ss:id= ' Default ' > ' + ' <ss:align
                    ment ss:vertical= "Top"/> ' + ' <ss:font ss:fontname= ' Arial ' ss:size= '/> '  + ' <ss:Borders> ' + ' <ss:border ss:weight= "1" ss:linestyle= "continuous" ss:position= "top"
                    /> ' + ' <ss:border ss:weight= ' 1 "ss:linestyle=" continuous "ss:position=" Bottom "/>" + ' <ss:border ss:weight= ' 1 "ss:linestyle=" continuous "ss:position=" left "/> ' + '
                    Ss:border 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:vertical= ' Center '
                    Ss:horizontal= "Center"/> ' + ' <ss:numberformat ss:format= @ '/> ' + ' </ss:Style> '
                    + ' <ss:style ss:id= ' Headercell ' > ' + ' <ss:font ss:bold= ' 1 ' ss:size= '/> ' + ' <ss:alignment ss:horizontal= ' Center '/> ' + ' <ss:interior ss:pattern= ' S Olid "/> ' + ' </ss:Style> ' + ' <ss:style ss:id=" even "> ' + ' <ss:in Terior ss:pattern= "Solid"/> ' + ' </ss:Style> ' + ' <ss:style ss:parent= ' even "ss:id=" Evenda TE "> ' + ' <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" > ' + ' &LT;SS:
                    NumberFormat ss:format= "0.00"/> ' + ' </ss:Style> ' + ' <ss:style ss:id= ' odd ' > ' + ' <ss:interior ss:pattern= ' Solid '/> ' + ' </ss:Style> ' + ' <ss:style ss:paren
                    T= "Odd" ss:id= "odddate" > ' + ' <ss:numberformat ss:format= ' Yyyy-mm-dd "/>" + ' </ss:Style> ' + ' <ss:style ss:parent= ' odd ' ss:id= ' oddint ' > ' + ' <s S:numberformat ss:format= "0"/> ' + ' </ss:Style> ' + ' <ss:style ss:parent= ' odd "ss:id=" ODDFL Oat "> ' + ' <ss:numberformat ss:format=" 0.00 "/> ' + ' </ss:Style> ' + ' </ss:Styles> ' + woRksheet.xml + ' </ss:Workbook> '; }, Createworksheet:function (JQ, param) {//Calculate cell data types and extra class names which AF
            Fect//formatting var celltype = [];
            var celltypeclass = [];
            var cm = This.getcolumnmodel ();
            var totalwidthinpixels = 0;
            var colxml = ';
            var headerxml = ';
            var visiblecolumncountreduction = 0;
            var cfs = $ (JQ). DataGrid (' Getcolumnfields ');
            var colcount = cfs.length;
            var colformatter = [];//used to record the custom function of a column to get the displayed data with Formmater instead of the raw data//for (var i = 1; i < ColCount; i++) {  for (var i = 0; i < ColCount i++) {////////////////////correction, this will cause the first column to be lost and should begin traversing the if (cfs[i) from 0!=
                    ') {var Copts = $ (JQ). DataGrid (' Getcolumnoption ', cfs[i]);
                    Colformatter[i] = Copts.formatter;
      var w = copts.width;              Totalwidthinpixels = W;
                        if (cfs[i] = = "") {Celltype.push ("None");
                        Celltypeclass.push ("");
                    ++visiblecolumncountreduction; else {if (!copts.hidden) {//Not hidden column can be written to colxml = = ' <ss:column ss:
                            Autofitwidth= "1" ss:width= "130"/> "; Headerxml + = ' <ss:cell ss:styleid= ' Headercell ' > ' + ' <ss:data ss:type= ' String '
                                    > ' + $ (JQ). DataGrid (' Getcolumnoption ', Cfs[i]). title + ' </ss:Data> ' + ' <ss:namedcell ss:name= ' print_titles '/></ss:cel
                            L> ';
                            Celltype.push ("String");
                        Celltypeclass.push ("");
}

                    }
                }
            }            var visiblecolumncount = celltype.length-visiblecolumncountreduction;
            var result = {height:9000, Width:Math.floor (Totalwidthinpixels * 30) + 50
            };
            var rows = $ (JQ). DataGrid (' GetRows ');
            Generate Worksheet Header details.
                    var t = ' <ss:worksheet ss:name= ' + param.title + ' > ' + ' <ss:Names> ' + ' <ss:namedrange ss:name= ' print_titles ' ss:refersto= ' =\ ' + param.title + ' \ '!
                    R1:r2 "/> ' + ' </ss:Names> ' + ' <ss:table x:fullrows=" 1 "x:fullcolumns=" 1 " + ' ss:expandedcolumncount= ' + (Visiblecolumncount + 2) + ' ss:expandedrowcount= ' + (rows.lengt H + 2) + ' > ' + colxml + ' <ss:row ss:autofitheight= ' 1 ' > ' + headerxml + ' &
            Lt;/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++) {for (var i = 0, it = rows, L = It.length i < l; i++) {T + = ' <ss:Row> '; var Cellclass = (I & 1)?
                ' Odd ': ' Even ';
                r = It[i];
                var k = 0; for (var j = 1; j < ColCount; J +) {for (var j = 0; J < ColCount; J + +) {////////////////////correct this
                        , which causes the first column to be lost, should start at 0 traversal//if ((Cm.getdataindex (j)!= ') if (Cfs[j]!= ') {
                        var v = r[cm.getdataindex (j)];
                        var v = r[cfs[j]];
                                    if (Celltype[k]!== "None" {t + = ' <ss:cell ss:styleid= ' + cellclass + celltypeclass[k] + ' "><ss:data ss:type=" ' + celltype[k] + ' "&G
                t; ';            if (celltype[k] = = ' DateTime ') {T + + v.format (' y-m-d '); else {T + = Colformatter[j]? colformatt
                            Er[j] (V, R, i): v;//to determine whether the column has formatter, there are calls formatter get raw data
                        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=" land Scape "/> ' + ' <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 :P rint> ' + ' <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>fal
            Se</x:protectscenarios> ' + ' </x:WorksheetOptions> ' + ' </ss:Worksheet> ';
        return result;
}
    }); }
Introducing Encrypted Base64.js files
  var Base64 = {//private property _keystr: "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy Z0123456789+/= ",//public method for encoding encode:function (input) {var outp
                UT = "";
                var chr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4;
                var i = 0;
                input = Base64._utf8_encode (input);
                    while (I < input.length) {chr1 = Input.charcodeat (i++);
                    CHR2 = Input.charcodeat (i++);
                    CHR3 = Input.charcodeat (i++);
                    ENC1 = Chr1 >> 2; ENC2 = ((Chr1 & 3) << 4) |
                    (CHR2 >> 4); Enc3 = ((CHR2 &) << 2) |
                    (CHR3 >> 6);
                    Enc4 = CHR3 & 63;
                    if (isNaN (CHR2)) {enc3 = Enc4 = 64;
         else if (isNaN (CHR3)) {Enc4 = 64;           Output = output + This._keystr.charat (ENC1) + This._keystr.charat (ENC2) +
                This._keystr.charat (ENC3) + This._keystr.charat (ENC4);
            return output;
                Decoding decode:function (input) {var output = "";
                var chr1, CHR2, CHR3;
                var enc1, Enc2, enc3, Enc4;
                var i = 0;
                input = Input.replace (/[^a-za-z0-9\+\/\=]/g, "");
                    while (I < input.length) {enc1 = This._keystr.indexof (Input.charat (i++));
                    ENC2 = This._keystr.indexof (Input.charat (i++));
                    enc3 = This._keystr.indexof (Input.charat (i++));
                    Enc4 = This._keystr.indexof (Input.charat (i++)); CHR1 = (enc1 << 2) |
                    (Enc2 >> 4); CHR2 = ((Enc2 &) << 4) |
(enc3 >> 2);                    CHR3 = ((enc3 & 3) << 6) |
                    Enc4;
                    Output = output + String.fromCharCode (CHR1);
                    if (enc3!=) {output = output + String.fromCharCode (CHR2);
                    } if (Enc4!=) {output = output + String.fromCharCode (CHR3);
                } output = Base64._utf8_decode (output);
            return output;  },//Private method for UTF-8 encoding _utf8_encode:function (string) {string =
                String.Replace (/\r\n/g, "\ n");
                var utftext = "";
                    for (var n = 0; n < string.length; n++) {var c = string.charcodeat (n);
                    if (c < 128) {Utftext + = String.fromCharCode (c);
           else if ((C > 127) && (C < 2048)) {             Utftext + + String.fromCharCode ((c >> 6) | 192);
                    Utftext + + String.fromCharCode ((C & 63) | 128);
                        else {Utftext + = String.fromCharCode ((c >> 12) | 224);
                        Utftext + + String.fromCharCode ((c >> 6) & 63) | 128;
                    Utftext + + String.fromCharCode ((C & 63) | 128);
            } return utftext; },//private method for UTF-8 decoding _utf8_decode:function (utftext) {var str
                ing = "";
                var i = 0;
                var c = C1 = C2 = 0;
                    while (I < utftext.length) {c = utftext.charcodeat (i);
                        if (c < 128) {string + = String.fromCharCode (c);
                    i++; else if ((C > 191)&& (c < 224)) {C2 = utftext.charcodeat (i + 1); String + String.fromCharCode (((C &) << 6) |
                        (C2 & 63));
                    i + 2;
                        else {c2 = utftext.charcodeat (i + 1);
                        C3 = Utftext.charcodeat (i + 2); String + String.fromCharCode ((C &) << 12) | ((C2 &) << 6) |
                        (C3 & 63));
                    i + 3;
            } return string; }
        }
Add Export button, click to perform export function exports (Strxlsname, Exportgrid)
<a href= "#" class= "Easyui-linkbutton" iconcls= "Icon-putout" plain= "true" onclick= "Obj.export2excel ();" > Export excel</a>

To perform an export function

var obj = {
    export2excel:function () {
        Export (' list of finished goods in storage ', goodsinputlist);      
    } 

Export Function Details:

/** * exported to Excel Easyui * @param {} strxlsname name * @param {} exportgrid DataGrid/function Export (Strxlsname, EXPORTG
    RID) {var supportbase64import = true; if (navigator.userAgent.indexOf (' Safari ')!=-1) Supportbase64import = false; Safari Direct judgment does not support, go to AJAX feeder//below using IE annotation condition, ie8-also go Ajax feeder. Chrome,firefox supports client direct export of Base64 string data//getexcelxml has a JSON object configuration, configuration items see only title configuration, for Excel document title var data = Exportgr Id.datagrid (' Getexcelxml ', {title:strxlsname}); Get the XML format content that Excel needs for the DataGrid data if (Supportbase64import) {//Client Support Base64 content export file, such as Chrome,firefox window
            . location = ' data:application/vnd.ms-excel;base64, ' + base64.encode (data);
        return false; //Unsupported form submission, no need to generate Excel temporary file var url = ' Exporttoexcelcontroller.do?export ';
        If the suffix var f = $ (' #importForm ') is modified for the ASP's attention; if (F.length = = 0) F = $ (' <form id= "Importform" method= "post" target= "_blank" action= "' + URL + '" ><input type= "H Idden "name=" Data "/></form> ');
        F.find (' input '). val (data); Type: ' POST ', DataType: ' Text ', Success:function (FN) {alert (' Export Excel succeeded.
                '); window.location = fn; Perform download Operation}, Error:function (XHR) {alert (' dynamic page has a problem \nstatus: ' + xhr.status + ' \nresp
Onsetext: ' + Xhr.responsetext '}}); */return false;  
 }
Back-desk request, background processing pass past DataGrid data, export Excel
Background Export Excel Method details:
/**
 * Export DataGrid content to Excel
 * @author li Xiaodong
 * *
 @param txtcontent
 * @param txtname
 * @param request
 *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.