Export Ext. grid. Panel to excel

Source: Internet
Author: User
Tags cmss
1. Client definition. The basic idea is to submit the table header definition in form and transmit data in json format ExtgridPaneladdMembers ({exportExcel: function (options) {if (! ExtisDefined (options) {options {}}; 1. The basic idea of the client definition is to submit the table header definition in form and transmit data in json format.
Ext. grid. Panel. addMembers ({exportExcel: function (options) {if (! Ext. isDefined (options) {options ={}}; options. name = options. name | 'unname'; // there is an agreement here that the attribute ending with time or date is of the time type var cms = Ext. array. map (this. columns, function (item) {if (Ext. string. endsWith (item. dataIndex, 'time', true) | Ext. string. endsWith (item. dataIndex, 'date', true) {return {dataIndex: item. dataIndex, text: item. text, datatype: 'datetime', format: 'yyyy-MM-dd HH: mm: ss'} else {return {dataIndex: item. dataIndex, text: item. text }}); var data = Ext. array. map (this. store. getRange (), function (item) {var d ={}; for (var index = 0; index
 
  

 

2. call the instance this. gripmma. exportExcel ({name: this. title}); 3. the current job is transferred to the server. I personally like myxls because the data is transmitted in json format. We recommend that you use json.net with these two tools. you can complete general tasks simply by packing them.
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using System. IO; using System. text; using Newtonsoft. json. linq; using org. in2bits. myXls; namespace CJRApp2.Web. controllers {public class ExportController: Controller {// private DateTime baseDT = new DateTime (1970, 1, 1, 8, 0, 0); private void addCell (XlsDocument doc, JToken r, JToken c, Ce Lls cells, int rowIndex, int cellIndex) {string cName = c ["dataIndex"]. toString (); if (c ["datatype"] = null) {JValue jv = r [cName] as JValue; if (jv! = Null & jv. Value! = Null) {switch (jv. Type) {case JTokenType. Float: cells. Add (rowIndex, cellIndex, jv. ToObject
   
    
(); Break; case JTokenType. Integer: cells. Add (rowIndex, cellIndex, jv. ToObject
    
     
(); Break; default: cells. add (rowIndex, cellIndex, jv. toString (); break ;}} else if (c ["datatype"]. toString () = "DateTime") {DateTime dt; if (r [cName]! = Null & DateTime. tryParse (r [cName]. toString (), out dt) {string format = "yyyy-MM-dd HH: mm: ss"; if (c ["format"]! = Null) {format = c ["format"]. toString ();} XF xf = doc. newXF (); xf. format = format; Cell cell = cells. add (rowIndex, cellIndex, dt, xf) ;}} [HttpPost] [ValidateInput (false)] public ActionResult excel (string cms, string data, string title = "title ", string fileName = "export.xls") {string json = "{\" data \ ":" + data + ", \" cms \ ":" + cms + "}"; JObject jsonObj = JObject. parse (json); JToken cmsObj = jsonObj ["cms"]; JToken dataObj = jsonObj ["data"]; List
     
      
Cmss = cmsObj. ToList (); List
      
       
Datas = dataObj. ToList (); if (HttpContext. Request. UserAgent. IndexOf ("MSIE ")! =-1) {fileName = HttpContext. server. urlEncode (fileName);} XlsDocument doc = new XlsDocument (); Worksheet sheet = doc. workbook. worksheets. add (title); Cells cells = sheet. cells; int rowIndex = 1; int cellIndex = 0; bool addrow = false; foreach (JToken t in cmss) {cellIndex ++; Cell cell = cells. add (rowIndex, cellIndex, t ["text"]. toString (); if (t ["columns"]! = Null) {addrow = true; int c2Index = 0; foreach (JToken c2 in t ["columns"]) {cells. add (rowIndex + 1, cellIndex + c2Index, c2 ["text"]. toString (); c2Index ++;} cellIndex + = t ["columns"]. count ()-1; // sheet. addMergeArea }}if (addrow) {rowIndex ++;} foreach (JToken r in datas) {rowIndex ++; cellIndex = 0; foreach (JToken c in cmss) {cellIndex ++; if (c ["columns"]! = Null) {foreach (JToken cc in c ["columns"]) {addCell (doc, r, cc, cells, rowIndex, cellIndex ++);} cellIndex --;} else {addCell (doc, r, c, cells, rowIndex, cellIndex) ;}} return this. file (doc. bytes. byteArray, "application/vnd. ms-excel ", fileName );}}}
      
     
    
   

 


Related Article

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.