The official release of Ext.NET Mobile is now available. Ext.NET Mobile is an ASP.NET component framework for building Phone and Tablet specific mobile web applications. Read More

MVC Razor - export data to excel or csv

  1. #1

    MVC Razor - export data to excel or csv - BADRESPONSE: Invalid character

    Last edited by geoffrey.mcgill; Nov 15, 2012 at 12:20 AM. Reason: please use [CODE] tags
  2. #2
  3. #3
    Last edited by geoffrey.mcgill; Nov 15, 2012 at 12:19 AM. Reason: please use [CODE] tags
  4. #4
    Please wrap all code samples in [CODE] tags.

    Forum Guidelines For Posting New Topics
    More Information Required
    Last edited by RCN; Nov 14, 2012 at 4:41 PM.
  5. #5
    1 - View
    <!DOCTYPE html>
    <head runat="server">
        <script type="text/javascript">
            var exportExcel = function () {
                    url: "/Example/ExportExcel",
                    cleanRequest: true,
                    isUpload: true,
                    params: {
                        data: App.GridPanel1.getRowsValues()
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <ext:Store ID="Store1" runat="server">
                <ext:Model ID="Model1" runat="server">
                        <ext:ModelField Name="ID" />
                        <ext:ModelField Name="Name" />
                        <ext:ModelField Name="Address" />
        <ext:GridPanel ID="GridPanel1" runat="server" Title="Person" Frame="false">
                <ext:Toolbar runat="server">
                        <ext:Button ID="Button1" Text="Export Excel" Icon="PageExcel" runat="server">
                                <Click Handler="exportExcel();" />
                <ext:Store runat="server" ID="Store2">
                        <ext:AjaxProxy Url="/Example/LoadFakeRecords/">
                            <ActionMethods Read="POST" />
                                <ext:JsonReader Root="data" />
                        <ext:Model ID="Model2" runat="server">
                                <ext:ModelField Name="ID" Type="String" />
                                <ext:ModelField Name="Name" Type="String" />
                                <ext:ModelField Name="Address" Type="String" />
                        <ext:DataSorter Property="Common" Direction="ASC" />
            <ColumnModel ID="ColumnModel1" runat="server">
                    <ext:Column ID="Column1" runat="server" Text="ID" DataIndex="ID" />
                    <ext:Column ID="Column3" runat="server" Text="Name" DataIndex="Name" />
                    <ext:Column ID="Column2" runat="server" Text="Address" DataIndex="Address" />
    2 - Actions
    public ActionResult ExportExcel(string data)
        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
        FileContentResult result = null;
        System.Xml.XmlWriter  writer = System.Xml.XmlWriter.Create(s, settings);
        xt.Transform(xml, writer);
        result = new FileContentResult(Encoding.UTF8.GetBytes(s.ToString()), "application/");
        result.FileDownloadName = "Temp.xls";
        return result;
    public StoreResult LoadFakeRecords()
        List<Person> lst = new List<Person>();
        for (int index = 0; index < 20; index++)
            lst.Add(new Person
                ID = index,
                Name = "Name" + index,
                Address = "Address" + index,
        return new StoreResult(lst, lst.Count());
    3 - Utility
    public class Person
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }

    4 - Excel.Xsl: Do not forget to save this file on the following path: ~/Resources/Excel.xsl. Otherwise an exception will be thrown at Actions - Line 14 and you will get a BadResponse
    <xsl:stylesheet version="1.0"
    <xsl:template match="/">
      <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    <xsl:template match="/*">
      <xsl:attribute name="ss:Name">
      <xsl:value-of select="local-name(/*/*)" />
        <Table x:FullColumns="1" x:FullRows="1">
            <xsl:for-each select="*[position() = 1]/*">
              <Cell><Data ss:Type="String">
              <xsl:value-of select="local-name()" />
    <xsl:template match="/*/*">
    <xsl:template match="/*/*/*">
      <Cell><Data ss:Type="String">
        <xsl:value-of select="." />
    Last edited by geoffrey.mcgill; Nov 15, 2012 at 12:21 AM.

Similar Threads

  1. Export grid panel data to excel
    By sumesh in forum 1.x Help
    Replies: 1
    Last Post: Sep 23, 2013, 10:07 AM
  2. How can Export data from GridPanel to Excel
    By delta in forum 2.x Help
    Replies: 2
    Last Post: Nov 14, 2012, 2:07 AM
  3. Export grid panel data to excel
    By sumesh in forum 1.x Help
    Replies: 0
    Last Post: May 19, 2012, 7:01 AM
  4. Export data to excel - in order columns
    By tanju_yayak in forum 1.x Help
    Replies: 2
    Last Post: Mar 22, 2012, 7:40 AM
  5. Data Export Excel Column Names
    By BLOZZY in forum 1.x Help
    Replies: 0
    Last Post: Feb 02, 2012, 3:02 PM

Tags for this Thread

Posting Permissions