Aug 02, 2012, 1:06 PM
[CLOSED] [MVC] Export Excel return "BADRESPONSE: Unexpected token <"
Hi,
In relation with this post: http://forums.ext.net/showthread.php...ll=1#post59599
I try to export data in the grid to excel format. But it always return: "BADRESPONSE: Unexpected token <"
Here is my example code:
View:
And if I select only some columns in the gridpanel. How can I export to the excel these columns, not all columns
Thank you in advance
In relation with this post: http://forums.ext.net/showthread.php...ll=1#post59599
I try to export data in the grid to excel format. But it always return: "BADRESPONSE: Unexpected token <"
Here is my example code:
View:
@{
Layout = null;
}
@using System.Data
@model System.Data.DataTable
@{
Layout = null;
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("col1", typeof(String)));
table.Columns.Add(new DataColumn("col2", typeof(String)));
DataRow dr = table.NewRow();
dr["col1"] = "row1";
dr["col2"] = "row1";
table.Rows.Add(dr);
DataRow dr2 = table.NewRow();
dr2["col1"] = "row2";
dr2["col2"] = "row2";
table.Rows.Add(dr2);
}
@Html.X().ResourceManager()
@(Html.X().GridPanel().ID("Panel1").Store(
s => s.Add(Html.X().Store().Model(
m => m.Add(Html.X().Model().Fields(
f =>
{
f.Add(Html.X().ModelField().Name("col1"));
f.Add(Html.X().ModelField().Name("col2"));
}))).DataSource(table))
)
.ColumnModel(c =>
{
c.Add(Html.X().Column().DataIndex("col1").Text("ID"));
c.Add(Html.X().Column().DataIndex("col2").Text("Company"));
})
.TopBar(b => b.Add(Html.X().Toolbar().Height(25)
.Items(i =>
{
i.Add(Html.X().ToolbarFill());
i.Add(Html.X().Button().Text("ToExcel").Icon(Icon.PageCode)
.DirectEvents(d =>
{
d.Click.Url = "/Export/ToExcel";
d.Click.IsUpload = true;
d.Click.EventMask.ShowMask = true;
d.Click.ExtraParams.Add(new Parameter()
{
Name = "data",
Value = "#{Panel1}.getRowsValues()",
Mode = ParameterMode.Raw,
Encode = true,
});
d.Click.ExtraParams.Add(new Parameter()
{
Name = "format",
Value = "xls",
Mode = ParameterMode.Value
});
}));
})))
)
Controller:
public class ExportController : Controller
{
[HttpPost]
public ActionResult ToExcel(string data, string format)
{
var xt = new XslCompiledTransform();
var submitData = new Ext.Net.SubmitHandler(data);
XmlNode xml = submitData.Xml;
var s = new StringBuilder();
var settings = new XmlWriterSettings()
{
ConformanceLevel = ConformanceLevel.Auto
};
XmlWriter writer;
FileContentResult result = null;
switch (format)
{
#region Excel
case "xls":
xt.Load(Server.MapPath("~/Resources/xsl/Excel.xsl"));
writer = XmlWriter.Create(s, settings);
xt.Transform(xml, writer);
result = new FileContentResult(Encoding.UTF8.GetBytes(s.ToString()), "application/vnd.ms-excel");
result.FileDownloadName = "Temp.xls";
break;
#endregion
#region CSV
case "csv":
xt.Load(Server.MapPath("~/Resources/xsl/Csv.xsl"));
writer = XmlWriter.Create(s, settings);
xt.Transform(xml, writer);
result = new FileContentResult(Encoding.UTF8.GetBytes(s.ToString()), "application/octet-stream");
result.FileDownloadName = "Temp.csv";
break;
#endregion
case "xml":
result = new FileContentResult(Encoding.UTF8.GetBytes(xml.OuterXml), "application/xml");
result.FileDownloadName = string.Format("Temp.xml");
return result;
}
return result;
}
Can you tell me how to resolve this problem.And if I select only some columns in the gridpanel. How can I export to the excel these columns, not all columns
Thank you in advance
Last edited by Daniil; Aug 07, 2012 at 4:58 AM.
Reason: [CLOSED]