Dec 15, 2011, 4:58 PM
How to create CSV and excel file from GridResult [Ext.NET MVC]
Hello I would like to export CSV file from GridResult; I implemented an action and Ajax function, I had not any error any exception but I had not also any result :
This is the Ajax function :
Thx
This is the Ajax function :
var submitValue = function (grid, format) {
// console.log(grid.getRowsValues());
var json = new Array();
for (var i = 0; i < grid.getStore().getCount(); i++) {
json.push(grid.getStore().getAt(i).data);
}
var stringifedJson = JSON.stringify(json, null, 2)
// console.log(stringifedJson);
$.ajax({
type: "POST",
url: "/Consumer/ExportData",
data:
{
"data": stringifedJson,
"format": format
},
success: function () {
console.log("Ajax Request done");
},
error: function (req, status, error) {
alert(error);
}
});
};
And this is the Action : [HttpPost]
public EmptyResult ExportData(String data, String format)
{
XslCompiledTransform xt = new XslCompiledTransform();
string exportWithRoot = string.Format("{{ records: {{ record: {0} }} }}", data);
XmlNode xml = JsonConvert.DeserializeXmlNode(exportWithRoot);
//XmlTextReader xtr = new XmlTextReader(xml.OuterXml, XmlNodeType.Element, null);
//DataSet ds = new DataSet();
//ds.ReadXml(xtr);
switch (format)
{
case "xls":
this.Response.Clear();
this.Response.ClearContent();
this.Response.ClearHeaders();
this.Response.ContentType = "application/vnd.ms-excel";
this.Response.AddHeader("Content-Disposition", "attachment;filename=submittedData.xls");
xt.Load(Server.MapPath("~/App_Data/Excel.xsl"));
xt.Transform(xml, null, this.Response.OutputStream);
this.Response.End();
break;
case "csv":
this.Response.Clear();
this.Response.ClearContent();
this.Response.ClearHeaders();
this.Response.ContentType = "application/octet-stream";
this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
xt.Load(Server.MapPath("~/App_Data/Csv.xsl"));
xt.Transform(xml, null, this.Response.OutputStream);
this.Response.End();
break;
}
return null;
}
PS: In POST Response for ExportDataToCsv I obtain this text :"18719","african investment","Company","6","ACTIVATED"
"18721","PLAZA ? SICAF","PLAZA ? SICAF Investment Company","6","ACTIVATED"
"18722","PLAZA","PLAZA ? Construction","6","ACTIVATED"
"18723","A.C.T","A.C.T Materials","6","ACTIVATED"
"18725","AMI","Ateliers Mécaniques Industriels","6","ACTIVATED"
"18755","Achraf","Achraf","",""
"18758","Achraf2","Achraf2","6","ACTIVATED"
"25231","25244","African services","6","ACTIVATED"
"26113","26131","Counterparty","6","ACTIVATED"
"27741","Best company","Bachraoui","",""
"27742","Imagine limited","Picture and design","",""
"27749","BILDCO","BILDCO","39","Client"
"27770","ABC Bank","ABC Bank","",""
"27772","Access Bank","Access Bank","",""
"27799","Temp.LE COQ","LE COQ","",""
Cheers, Thx
Last edited by wadhah; Dec 16, 2011 at 12:40 PM.