How to create CSV and excel file from GridResult [Ext.NET MVC]

Page 3 of 4 FirstFirst 1234 LastLast
  1. #21
    Please set up:
    btn.DirectEvents.Click.CleanRequest = true;
  2. #22
    Thanks a lot, it worked like a charm within the latest sample we provided on this post.
    We added the CleanRequest property to our application and that worked too (data parameter is not null anymore), but the download window does not appear.

    Do you have any idea why the download window is not shown?
  3. #23
    Is the issue reproducible in all browsers?
  4. #24
    Quote Originally Posted by Daniil View Post
    Is the issue reproducible in all browsers?
    The attachment below is a printscreen showing the error when I click on Excel button within (IE8,Chrome) ...
    Click image for larger version. 

Name:	Untitled.png 
Views:	135 
Size:	74.4 KB 
ID:	3615
  5. #25
    Seems you forgot to set Encode to true for the "data" parameter.
    dataP.Encode = true;
  6. #26
    Quote Originally Posted by Daniil View Post
    Seems you forgot to set Encode to true for the "data" parameter.
    dataP.Encode = true;
    No it's Implemented bellow is the fct generating the toolbar
       public Toolbar GenerateToolbar()
            {
                Toolbar toolbar = new Toolbar();
                toolbar.ID = "_toolbar";
                ToolbarFill toolbarFill = new ToolbarFill();
                toolbarFill.ID = "_toolbarFill";
                Button excelButton = new Button();
                excelButton.Icon = Icon.PageExcel;
                excelButton.ID = "_excelButton";
                excelButton.Text = "To Excel";
                Parameter dataParam = new Parameter();
                dataParam.Name = "data";
                dataParam.Value = "#{_gridSearch}.getRowsValues()";
                dataParam.Mode = ParameterMode.Raw;
                dataParam.Encode = true;
                Parameter formatXlsParam = new Parameter();
                formatXlsParam.Name = "format";
                formatXlsParam.Mode = ParameterMode.Value;
                formatXlsParam.Value = "xls";
                ParameterCollection xlsPrms = new ParameterCollection();
                xlsPrms.Add(dataParam);
                xlsPrms.Add(formatXlsParam);
                excelButton.DirectEvents.Click.IsUpload = true;
                excelButton.DirectEvents.Click.Url = "/Consumer/ExportData";
                excelButton.DirectEvents.Click.CleanRequest = true;
                excelButton.DirectEvents.Click.ExtraParams.AddRange(xlsPrms);
                Button csvButton = new Button();
                csvButton.Icon = Icon.PageAttach;
                csvButton.ID = "_csvButton";
                csvButton.Text = "To CSV";
                Parameter formatCsvParam = new Parameter();
                formatCsvParam.Name = "format";
                formatCsvParam.Mode = ParameterMode.Value;
                formatCsvParam.Value = "csv";
                ParameterCollection csvPrms = new ParameterCollection();
                csvPrms.Add(dataParam);
                csvPrms.Add(formatCsvParam);
                csvButton.DirectEvents.Click.IsUpload = true;
                csvButton.DirectEvents.Click.Url = "/Consumer/ExportData";
                csvButton.DirectEvents.Click.CleanRequest = true;
                csvButton.DirectEvents.Click.ExtraParams.AddRange(csvPrms);
                toolbar.Items.Add(excelButton);
                toolbar.Items.Add(csvButton);
                toolbar.Items.Add(toolbarFill);
                return toolbar;
            }
  7. #27
    How does look a DirectEvent response?
  8. #28
    Quote Originally Posted by Daniil View Post
    How does look a DirectEvent response?
    Hi again,
    The attachment below is a printscreen showing the DirectEvents response :
    Click image for larger version. 

Name:	Untitled.jpg 
Views:	128 
Size:	98.5 KB 
ID:	3616
  9. #29
    Thanks, but, unfortunately, no idea.

    Any chance to get a sample to reproduce?
  10. #30
    Hi,

    This is a sample where "data" parameter is not empty but the download window does not apprea.

    Here is the Controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using POCO;
    using Microsoft.Practices.Unity;
    using BusinessCrud;
    using Ext.Net.MVC;
    using System.Reflection;
    using Services.InstanceManager;
    using System.Collections;
    using Ext.Net;
    using System.Xml.Linq;
    using ViewEngine;
    using System.Data.Common;
    using System.Data;
    using System.Web.Script.Serialization;
    using Newtonsoft.Json;
    using System.Xml.Xsl;
    using System.Xml;
    using System.IO;
    using System.Text;
    using System.Xml.XPath;
    using Services.SQLRequestManager;
    
    namespace Presentation.Controllers
    {
        public class ConsumerController : Controller
        {
            //
            // GET: /Consumer/
            public ActionResult Index()
            {
                return View();
            }
    
            [HttpGet]
            public ActionResult Search()
            {
                return View();
            }
    
            [NonAction]
            public GridPanel GenerateGridPnl()
            {
                GridPanel gridPanel = new GridPanel();
                gridPanel.AutoHeight = true;
                gridPanel.AutoWidth = true;
                gridPanel.TrackMouseOver = true;
                gridPanel.ID = "_gridSearch";
                gridPanel.Listeners.Command.Fn = "commandHandler";
                gridPanel.LoadMask.ShowMask = true;
                Store store = new Store();
                store.ID = "_store";
                store.UseIdConfirmation = true;
                store.RemoteSort = true;
                HttpProxy httpProxy = new HttpProxy();
                httpProxy.Url = "/Consumer/GetData";
                store.Proxy.Add(httpProxy);
                RecordField recordField = null;
                RecordField recordField1 = null;
                Parameter limitParameter = null;
                Parameter startParameter = null;
                Column column = null;
                PagingToolbar pagingToolbar = null;
                Ext.Net.JsonReader jsonReader = new Ext.Net.JsonReader();
                jsonReader.Root = "data";
                jsonReader.TotalProperty = "total";
                recordField = new RecordField();
                recordField1 = new RecordField();
                recordField.Name = "counterparty_id";
                recordField1.Name = "counterparty_shortname";
                jsonReader.Fields.Add(recordField);
                jsonReader.Fields.Add(recordField1);
                column = new Column();
                Column column1 = new Column();
                column1.ColumnID = "counterparty_shortname";
                column1.DataIndex = "counterparty_shortname";
                column1.Header = "counterparty_shortname";
                column.ColumnID = "counterparty_id";
                column.DataIndex = "counterparty_id";
                column.Header = "counterparty_id";
                column.Width = 150;
                gridPanel.ColumnModel.Columns.Add(column);
                gridPanel.ColumnModel.Columns.Add(column1);
                RowSelectionModel rowSelection = new RowSelectionModel();
                rowSelection.SingleSelect = true;
                gridPanel.SelectionModel.Add(rowSelection);
                store.Reader.Add(jsonReader);
                store.RemotePaging = true;
                pagingToolbar = new PagingToolbar();
                pagingToolbar.PageSize = 15;
                pagingToolbar.AutoWidth = true;
                gridPanel.BottomBar.Add(pagingToolbar);
                limitParameter = new Parameter();
                limitParameter.Name = "limit";
                limitParameter.Value = "15";
                limitParameter.Mode = ParameterMode.Raw;
                startParameter = new Parameter();
                startParameter.Name = "start";
                startParameter.Value = "0";
                startParameter.Mode = ParameterMode.Raw;
                store.BaseParams.Add(limitParameter);
                store.BaseParams.Add(startParameter);
                Toolbar toolbar = new Toolbar();
                toolbar.ID = "_toolbar";
                toolbar.AutoWidth = true;
                ToolbarFill toolbarFill = new ToolbarFill();
                toolbarFill.AutoWidth = true;
                toolbarFill.ID = "_toolbarFill";
                Button excelButton = new Button();
                excelButton.Icon = Icon.PageExcel;
                excelButton.ID = "_excelButton";
                excelButton.Text = "To Excel";
                //excelButton.Listeners.Click.Handler = "submitValue(#{_gridSearch},'xls');";
                Parameter dataParam = new Parameter();
                dataParam.Name = "data";
                dataParam.Value = "#{_gridSearch}.getRowsValues()";
                dataParam.Mode = ParameterMode.Raw;
                dataParam.Encode = true;
                Parameter formatXlsParam = new Parameter();
                formatXlsParam.Name = "format";
                formatXlsParam.Mode = ParameterMode.Value;
                formatXlsParam.Value = "xls";
                ParameterCollection xlsPrms = new ParameterCollection();
                xlsPrms.Add(dataParam);
                xlsPrms.Add(formatXlsParam);
                excelButton.DirectEvents.Click.IsUpload = true;
                excelButton.DirectEvents.Click.Url = "/Consumer/ExportData";
                excelButton.DirectEvents.Click.CleanRequest = true;
                excelButton.DirectEvents.Click.ExtraParams.AddRange(xlsPrms);
                Button csvButton = new Button();
                csvButton.Icon = Icon.PageAttach;
                csvButton.ID = "_csvButton";
                csvButton.Text = "To CSV";
                Parameter formatCsvParam = new Parameter();
                formatCsvParam.Name = "format";
                formatCsvParam.Mode = ParameterMode.Value;
                formatCsvParam.Value = "csv";
                ParameterCollection csvPrms = new ParameterCollection();
                csvPrms.Add(dataParam);
                csvPrms.Add(formatCsvParam);
                csvButton.DirectEvents.Click.IsUpload = true;
                csvButton.DirectEvents.Click.Url = "/Consumer/ExportData";
                csvButton.DirectEvents.Click.CleanRequest = true;
                csvButton.DirectEvents.Click.ExtraParams.AddRange(csvPrms);
                toolbar.Items.Add(excelButton);
                toolbar.Items.Add(csvButton);
                toolbar.Items.Add(toolbarFill);
                gridPanel.TopBar.Add(toolbar);
                gridPanel.Store.Add(store);
                return gridPanel;
            }
    
            public ContentResult LoadSearchResultGridPnl(String containerID)
            {
                ContentResult cr = new ContentResult();
                cr.Content = string.Format("<script>{0}</script>",
                  GenerateGridPnl()
                  .ToScript(
                  RenderMode.AddTo,
                  containerID));
                return cr;
            }
    
            public AjaxStoreResult GetData()
            {
                List<Counterparty> items = new List<Counterparty>();
                Counterparty cpty1 = new Counterparty();
                cpty1.counterparty_id = 1;
                cpty1.counterparty_shortname = "ShortName";
                items.Add(cpty1);
                Counterparty cpty2 = new Counterparty();
                cpty2.counterparty_id = 1;
                cpty2.counterparty_shortname = "ShortName";
                items.Add(cpty2);
                var query = (from q in items
                             select new
                             {
                                 q.counterparty_id,
                                 q.counterparty_shortname,
                             }).ToList();
                return new AjaxStoreResult(query);
            }
    
            [HttpPost]
            public ActionResult ExportData(String data, String format)
            {
                XslCompiledTransform xt = new XslCompiledTransform();
                //string exportWithRoot = string.Format("{{ records: {{ record: {0} }} }}", data);
                //XmlNode xml = JsonConvert.DeserializeXmlNode(exportWithRoot);
                Ext.Net.SubmitHandler submitData = new Ext.Net.SubmitHandler(data);
                XmlNode xml = submitData.Xml;
                StringBuilder s = new StringBuilder();
                XmlWriterSettings settings = new XmlWriterSettings()
                {
                    ConformanceLevel = ConformanceLevel.Auto
                };
                XmlWriter writer = null;
                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;
            }
        }
    }
    Here is the partial view that holds the GridPanel:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <ext:Panel ID="_gridPnlCpty" Border="false" Header="false" runat="server" AutoHeight="true"
        Layout="FitLayout">
        <AutoLoad Url="/Consumer/LoadSearchResultGridPnl">
            <Params>
                <ext:Parameter Name="containerID" Value="#{_gridPnlCpty}" Mode="Value" />
            </Params>
        </AutoLoad>
    </ext:Panel>
    Hope this helps and thank you.
    Last edited by wadhah; Dec 23, 2011 at 2:35 PM.
Page 3 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. [CLOSED] multiple file upload and file size at client side
    By mirwais in forum 1.x Legacy Premium Help
    Replies: 24
    Last Post: Dec 15, 2014, 5:44 AM
  2. [CLOSED] file upload - file name is empty
    By stoque in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: May 11, 2011, 8:06 PM
  3. Replies: 0
    Last Post: May 25, 2010, 2:10 AM
  4. Replies: 2
    Last Post: May 15, 2009, 9:41 AM
  5. Replies: 3
    Last Post: Nov 27, 2008, 12:52 PM

Posting Permissions