資料來源使用LoadData方式的Grid匯出Excel方法

.ASPX部分:



//toolitemonclick事件



function exportlocal() {



     var datagrid = $('#JQDataGrid7');//要匯出EXCELGRID ID



     var exportFields = [];



     var fields =
datagrid.datagrid(
'getColumnFields', true);



      for (var i = 0; i <
fields.length; i++) {



               
exportFields.push({ field: fields[i], title: datagrid.datagrid(
'getColumnOption',
fields[i]).title });



            }



            fields
= datagrid.datagrid(
'getColumnFields');



      for (var i = 0; i <
fields.length; i++) {



                exportFields.push({
field: fields[i], title: datagrid.datagrid(
'getColumnOption', fields[i]).title });



            }



  var rows = $('#JQDataGrid7').datagrid('getRows');//要匯出EXCELGRID ID



           
$.ajax({



               
type:
"POST",



                data:
{ mode:
'export',
fields: $.toJSONString(exportFields), rows: $.toJSONString(rows) },



               
cache:
false,



               
async:
false,



               
success:
function (data) {



                   
window.open(
'../handler/JqFileHandler.ashx?File=' + data, 'download');



                }



            });



       
}



.cs部分



funtion



public override void ProcessRequest(HttpContext context)



    {



        if (!JqHttpHandler.ProcessRequest(context))



        {



            base.ProcessRequest(context);



        }



   
}



改為



public override void ProcessRequest(HttpContext context)



    {



        if
(context.Request.Form[
"mode"] == "export")



        {



           
Export(context);



        }



        else if (!JqHttpHandler.ProcessRequest(context))



        {



            base.ProcessRequest(context);



        }



   
}



//新增下面的funtion



private void Export(HttpContext context)



    {



        var exportTable = new System.Data.DataTable();



 



        var fields =
(Newtonsoft.Json.Linq.
JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(context.Request.Form["fields"]);



        var rows =
(Newtonsoft.Json.Linq.
JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(context.Request.Form["rows"]);



        foreach
(Newtonsoft.Json.Linq.
JObject field in fields)



        {



            var columnType = typeof(string);



            if (rows.Count > 0)



            {



                var obj = rows[0] as
Newtonsoft.Json.Linq.
JObject;



                var value = obj[(string)field["field"]];



                if (value.Type ==
Newtonsoft.Json.Linq.
JTokenType.Integer)



                {



                   
columnType =
typeof(int);



                }



                else if (value.Type ==
Newtonsoft.Json.Linq.
JTokenType.Float)



                {



                   
columnType =
typeof(double);



                }



            }



           
exportTable.Columns.Add(
new System.Data.DataColumn((string)field["field"], columnType) { Caption = (string)field["title"] });



        }



        foreach
(Newtonsoft.Json.Linq.
JObject row in rows)



        {



            var dataRow =
exportTable.NewRow();



            foreach (var column in row)



            {



                if
(exportTable.Columns.Contains(column.Key))



                {



                   
dataRow[column.Key] = column.Value.ToString();



                }



            }



           
exportTable.Rows.Add(dataRow);



        }



 



        var fileName = string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss"));



        var path = string.Format("../Files/{0}",
fileName);



        JQUtility.ExportToExcel(exportTable,
HttpContext.Current.Server.MapPath(path),
"", new List<string>());



       
context.Response.ContentType =
"text/plain";



       
context.Response.Write(fileName);



   
}