How can Export data from GridPanel to Excel

  1. #1

    [Razor] Error occurs when export Excel file from GridPanel

    My Controller is here:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.IO;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using System.Xml;
    using System.Xml.Xsl;
    
    namespace Layout.Controllers
    {
        public class ExportExcelController : Controller
        {
            public ActionResult Index()
            {
                return View("ViewAlarmReport","Index");
            }
            [HttpPost]
            public ActionResult ToExcel(string data)
            {
                Ext.Net.SubmitHandler submitData = new Ext.Net.SubmitHandler(data);
                XmlNode xml = submitData.Xml;
    
                XslCompiledTransform xtCsv = new XslCompiledTransform();
                xtCsv.Load(Server.MapPath("/Resources/xls/Excel.xls"));
                StringBuilder s = new StringBuilder();
                XmlWriterSettings settings = new XmlWriterSettings()
                {
                    ConformanceLevel = ConformanceLevel.Auto
                };
                XmlWriter writer = XmlWriter.Create(s, settings);
                xtCsv.Transform(xml, writer);
    
                var result = new FileContentResult(Encoding.UTF8.GetBytes(s.ToString()), "application/octet-stream");
                result.FileDownloadName = "Temp.csv";
                return result;
            }
        }
    }
    My View Here:
    @{
        DateTime now2 = DateTime.Now;
        var dataAlarmRe = new object[]
        {
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
        };
    Html.X()...................................}).Html.X().Button().ID("Export_Excel").IconUrl("../../Content/images/excel.gif").DirectEvents(DirEvAlarmExcel =>
                    {
                        DirEvAlarmExcel.Click.Url = Url.Action("ToExcel", "ExportExcel");
                        DirEvAlarmExcel.Click.IsUpload = true;
                        DirEvAlarmExcel.Click.EventMask.ShowMask = true;
                        DirEvAlarmExcel.Click.ExtraParams.Add(new Parameter()
                        {
                            Name = "data",
                            Value = "#{AlarmGridPanel}.getRowsValues()",
                            Mode = ParameterMode.Raw,
                            Encode = true,
                        });
                        DirEvAlarmExcel.Click.ExtraParams.Add(new Parameter()
                        {
                            Name = "format",
                            Value = "xls",
                            Mode = ParameterMode.Value
                        });
                    }));//Alarm Button Excel------------------------
    Html.X()...................................}).Items(
            AlarmItemGrid => {
                AlarmItemGrid.Add(Html.X().GridPanel().ID("AlarmGridPanel").Title("Alarm Details").Border(false)
                .Store(AlarmItemGridStore => {
                    AlarmItemGridStore.Add(Html.X().Store().AutoLoad(true).Model(AlarmItemGridStoreModel => {
                        AlarmItemGridStoreModel.Add(Html.X().Model().Fields(AlarmItemGridStoreField => {
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-device-name"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-type"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-gps-time"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-recive-time"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-fix"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-location"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-latitude"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-longtitude"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-speed"));
                        }));
                    }).DataSource(dataAlarmRe));
                })
                .ColumnModel(coModelAlarm =>
                {
                    coModelAlarm.Add(Html.X().Column().Text("Device Name").DataIndex("grid-alarm-device-name"));
                    coModelAlarm.Add(Html.X().Column().Text("Alarm Type").DataIndex("grid-alarm-type"));
                    coModelAlarm.Add(Html.X().Column().Text("GPS Time").DataIndex("grid-alarm-gps-time"));
                    coModelAlarm.Add(Html.X().Column().Text("Recived Time").DataIndex("grid-alarm-recive-time"));
                    coModelAlarm.Add(Html.X().Column().Text("Fix").DataIndex("grid-alarm-fix"));
                    coModelAlarm.Add(Html.X().Column().Text("Location").DataIndex("grid-alarm-location"));
                    coModelAlarm.Add(Html.X().Column().Text("Speed").DataIndex("grid-alarm-latitude"));
                    coModelAlarm.Add(Html.X().Column().Text("Latitude").DataIndex("grid-alarm-longtitude"));
                    coModelAlarm.Add(Html.X().Column().Text("Longtitude").DataIndex("grid-alarm-speed"));
                }));
            }
        )//Alarm Item Grid
    When I click the button to Export, It have error:

    Click image for larger version. 

Name:	error.jpg 
Views:	445 
Size:	99.9 KB 
ID:	4909
    Last edited by delta; Oct 10, 2012 at 9:05 AM.
  2. #2
    Quote Originally Posted by delta View Post
    My Controller is here:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.IO;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using System.Xml;
    using System.Xml.Xsl;
    
    namespace Layout.Controllers
    {
        public class ExportExcelController : Controller
        {
            public ActionResult Index()
            {
                return View("ViewAlarmReport","Index");
            }
            [HttpPost]
            public ActionResult ToExcel(string data)
            {
                Ext.Net.SubmitHandler submitData = new Ext.Net.SubmitHandler(data);
                XmlNode xml = submitData.Xml;
    
                XslCompiledTransform xtCsv = new XslCompiledTransform();
                xtCsv.Load(Server.MapPath("/Resources/xls/Excel.xls"));
                StringBuilder s = new StringBuilder();
                XmlWriterSettings settings = new XmlWriterSettings()
                {
                    ConformanceLevel = ConformanceLevel.Auto
                };
                XmlWriter writer = XmlWriter.Create(s, settings);
                xtCsv.Transform(xml, writer);
    
                var result = new FileContentResult(Encoding.UTF8.GetBytes(s.ToString()), "application/octet-stream");
                result.FileDownloadName = "Temp.csv";
                return result;
            }
        }
    }
    My View Here:
    @{
        DateTime now2 = DateTime.Now;
        var dataAlarmRe = new object[]
        {
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
            new object[] {"35 RUTA", "Input 2 inactive", now2, now2, "Valid", "41.744833,44.779036", "41.744833", "44.779036", "0.00"},
        };
    Html.X()...................................}).Html.X().Button().ID("Export_Excel").IconUrl("../../Content/images/excel.gif").DirectEvents(DirEvAlarmExcel =>
                    {
                        DirEvAlarmExcel.Click.Url = Url.Action("ToExcel", "ExportExcel");
                        DirEvAlarmExcel.Click.IsUpload = true;
                        DirEvAlarmExcel.Click.EventMask.ShowMask = true;
                        DirEvAlarmExcel.Click.ExtraParams.Add(new Parameter()
                        {
                            Name = "data",
                            Value = "#{AlarmGridPanel}.getRowsValues()",
                            Mode = ParameterMode.Raw,
                            Encode = true,
                        });
                        DirEvAlarmExcel.Click.ExtraParams.Add(new Parameter()
                        {
                            Name = "format",
                            Value = "xls",
                            Mode = ParameterMode.Value
                        });
                    }));//Alarm Button Excel------------------------
    Html.X()...................................}).Items(
            AlarmItemGrid => {
                AlarmItemGrid.Add(Html.X().GridPanel().ID("AlarmGridPanel").Title("Alarm Details").Border(false)
                .Store(AlarmItemGridStore => {
                    AlarmItemGridStore.Add(Html.X().Store().AutoLoad(true).Model(AlarmItemGridStoreModel => {
                        AlarmItemGridStoreModel.Add(Html.X().Model().Fields(AlarmItemGridStoreField => {
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-device-name"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-type"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-gps-time"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-recive-time"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-fix"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-location"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-latitude"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-longtitude"));
                            AlarmItemGridStoreField.Add(Html.X().ModelField().Name("grid-alarm-speed"));
                        }));
                    }).DataSource(dataAlarmRe));
                })
                .ColumnModel(coModelAlarm =>
                {
                    coModelAlarm.Add(Html.X().Column().Text("Device Name").DataIndex("grid-alarm-device-name"));
                    coModelAlarm.Add(Html.X().Column().Text("Alarm Type").DataIndex("grid-alarm-type"));
                    coModelAlarm.Add(Html.X().Column().Text("GPS Time").DataIndex("grid-alarm-gps-time"));
                    coModelAlarm.Add(Html.X().Column().Text("Recived Time").DataIndex("grid-alarm-recive-time"));
                    coModelAlarm.Add(Html.X().Column().Text("Fix").DataIndex("grid-alarm-fix"));
                    coModelAlarm.Add(Html.X().Column().Text("Location").DataIndex("grid-alarm-location"));
                    coModelAlarm.Add(Html.X().Column().Text("Speed").DataIndex("grid-alarm-latitude"));
                    coModelAlarm.Add(Html.X().Column().Text("Latitude").DataIndex("grid-alarm-longtitude"));
                    coModelAlarm.Add(Html.X().Column().Text("Longtitude").DataIndex("grid-alarm-speed"));
                }));
            }
        )//Alarm Item Grid
    When I click the button to Export, It have error:

    Click image for larger version. 

Name:	error.jpg 
Views:	445 
Size:	99.9 KB 
ID:	4909

    Hello :)

    Have you manage to resolve this issue ?

    Regards
    ish
  3. #3
    In your code, please check what this line fragment resolves to:

    Server.MapPath("/Resources/xls/Excel.xls")
    I'm just guessing, but it looks like you are attempting to load a file that does not exist. I think you may need to check that Server.MapPath is going where you think it is. Depending on how you deploy your application, you might need to use the tilde character ~ to ensure that MapPath maps from the root of your application, rather than the root of your host.

    Edit: oops, didn't realize the post was a month old.

Similar Threads

  1. Export grid panel data to excel
    By sumesh in forum 1.x Help
    Replies: 1
    Last Post: Sep 23, 2013, 9:07 AM
  2. Export grid panel data to excel
    By sumesh in forum 1.x Help
    Replies: 0
    Last Post: May 19, 2012, 6:01 AM
  3. Export data to excel - in order columns
    By tanju_yayak in forum 1.x Help
    Replies: 2
    Last Post: Mar 22, 2012, 6:40 AM
  4. Data Export Excel Column Names
    By BLOZZY in forum 1.x Help
    Replies: 0
    Last Post: Feb 02, 2012, 2:02 PM
  5. [CLOSED] Export Data To Excel in Remote Paging GridPanel
    By pdcase in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Oct 11, 2010, 8:49 PM

Posting Permissions