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

Page 4 of 4 FirstFirst ... 234
  1. #31
    I have no the Counterparty class definition.

    I'm afraid I can't help without a sample which I could run on my side.
  2. #32
    I forgot to add the Counterparty class, here it is [Counterparty.cs]:

    public class Counterparty
        {
            public int counterparty_id
            {
                get;
                set;
            }
    
            public string counterparty_shortname
            {
                get;
                set;
            }
    
            public Counterparty()
            {
    
            }
        }
  3. #33
    How do you load the partial view?
  4. #34
    Just use it as a simple view [Search.aspx]:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewPage<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>
    Use this URL to load the view /Consumer/Search.
  5. #35
    I just need a view where you load that partial view.
  6. #36
    Quote Originally Posted by Daniil View Post
    I just need a view where you load that partial view.
    Hi,
    Just use this view as sample aspx page [Search.aspx] :
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Search</title>
    </head>
    <body>
        <div>
            <ext:ResourceManager ID="ResourceManager" runat="server" />
            <ext:Panel ID="_gridPnlCpty" Title="Grid" runat="server" AutoHeight="true" Layout="FitLayout">
                <AutoLoad Url="/Consumer/LoadSearchResultGridPnl">
                    <Params>
                        <ext:Parameter Name="containerID" Value="#{_gridPnlCpty}" Mode="Value" />
                    </Params>
                </AutoLoad>
            </ext:Panel>
        </div>
    </body>
    </html>
    You will need this files under folders called Resources/xsl :
    [Excel.xsl]
    <xsl:stylesheet version="1.0"
    xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="urn:my-scripts"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

    <xsl:template match="/">
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:html="http://www.w3.org/TR/REC-html40">
    <xsl:apply-templates/>
    </Workbook>
    </xsl:template>


    <xsl:template match="/*">
    <Worksheet>
    <xsl:attribute name="ss:Name">
    <xsl:value-of select="local-name(/*/*)" />
    </xsl:attribute>
    <Table x:FullColumns="1" x:FullRows="1">
    <Row>
    <xsl:for-each select="*[position() = 1]/*">
    <Cell>
    <Data ss:Type="String">
    <xsl:value-of select="local-name()" />
    </Data>
    </Cell>
    </xsl:for-each>
    </Row>
    <xsl:apply-templates/>
    </Table>
    </Worksheet>
    </xsl:template>


    <xsl:template match="/*/*">
    <Row>
    <xsl:apply-templates/>
    </Row>
    </xsl:template>


    <xsl:template match="/*/*/*">
    <Cell>
    <Data ss:Type="String">
    <xsl:value-of select="." />
    </Data>
    </Cell>
    </xsl:template>


    </xsl:stylesheet>
    [Csv.xsl]
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />

    <xsl:template match="records">
    <xsl:apply-templates select="record" />
    </xsl:template>

    <xsl:template match="record">
    <xsl:for-each select="*">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>"</xsl:text>

    <xsl:if test="position() != last()">
    <xsl:value-of select="','" />
    </xsl:if>
    </xsl:for-each>
    <xsl:text>
    </xsl:text>
    </xsl:template>

    </xsl:stylesheet>
    When you load this view through this URL you will get the Grid
    Last edited by wadhah; Dec 24, 2011 at 7:21 AM.
  7. #37
    I have reproduced this issues an a sample project just send me your email because I can't upload it [Because the size is large > 2 mega]
  8. #38
    Sorry again for the missing files. Here is the same sample but now it is full.

    The view [Search.aspx]:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Search</title>
    </head>
    <ext:ResourceManager ID="ResourceManager" runat="server" />
    <body>
        <ext:Button ID="Button" runat="server" Text="render partial">
            <DirectEvents>
                <Click Url="/Consumer/ViewUserControl">
                </Click>
            </DirectEvents>
        </ext:Button>
    </body>
    </html>
    The partial view [GridUserControl.ascx]:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <%@ 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>
    The Counterparty class [Counterparty.cs]:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MvcApplication.Controllers
    {
        public class Counterparty
        {
            public int counterparty_id
            {
                get;
                set;
            }
    
            public string counterparty_shortname
            {
                get;
                set;
            }
    
            public Counterparty()
            {
    
            }
        }
    }
    The Controller [ConsumerController.cs]:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Ext.Net;
    using System.Xml;
    using System.Text;
    using System.Xml.Xsl;
    using MvcApplication.Results;
    using MvcApplication.Controllers;
    
    namespace Presentation.Controllers
    {
        public class ConsumerController : Controller
        {
            //
            // GET: /Consumer/
    
            [HttpGet]
            public ActionResult Search()
            {
                return View();
            }
    
            [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("~/Views/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("~/Views/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;
            }
    
            [NonAction]
            public GridPanel GenerateGridPnl()
            {
                GridPanel gridPanel = new GridPanel();
                gridPanel.Height=400;
                gridPanel.Width = 400;
                gridPanel.TrackMouseOver = true;
                gridPanel.ID = "_gridSearch";
                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);
            }
    
            public ActionResult GridUserControl()
            {
                Ext.Net.MVC.PartialViewResult pr = new Ext.Net.MVC.PartialViewResult();
                return pr;
            }
    
    
        }
    }
    Export Excel template [Excel.xsl]. For this sample, just put the Excel.xsl file under Views folder:

    <xsl:stylesheet version="1.0"
        xmlns="urn:schemas-microsoft-com:office:spreadsheet"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     xmlns:user="urn:my-scripts"
     xmlns:o="urn:schemas-microsoft-com:office:office"
     xmlns:x="urn:schemas-microsoft-com:office:excel"
     xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    
      <xsl:template match="/">
        <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:o="urn:schemas-microsoft-com:office:office"
          xmlns:x="urn:schemas-microsoft-com:office:excel"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">
          <xsl:apply-templates/>
        </Workbook>
      </xsl:template>
    
    
      <xsl:template match="/*">
        <Worksheet>
          <xsl:attribute name="ss:Name">
            <xsl:value-of select="local-name(/*/*)" />
          </xsl:attribute>
          <Table x:FullColumns="1" x:FullRows="1">
            <Row>
              <xsl:for-each select="*[position() = 1]/*">
                <Cell>
                  <Data ss:Type="String">
                    <xsl:value-of select="local-name()" />
                  </Data>
                </Cell>
              </xsl:for-each>
            </Row>
            <xsl:apply-templates/>
          </Table>
        </Worksheet>
      </xsl:template>
    
    
      <xsl:template match="/*/*">
        <Row>
          <xsl:apply-templates/>
        </Row>
      </xsl:template>
    
    
      <xsl:template match="/*/*/*">
        <Cell>
          <Data ss:Type="String">
            <xsl:value-of select="." />
          </Data>
        </Cell>
      </xsl:template>
    
    
    </xsl:stylesheet>

    Export CSV template [Csv.xsl]. For this sample, just put the Csv.xsl file under Views folder:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text" />
    
      <xsl:template match="records">
        <xsl:apply-templates select="record" />
      </xsl:template>
    
      <xsl:template match="record">
        <xsl:for-each select="*">
          <xsl:text>"</xsl:text>
          <xsl:value-of select="." />
          <xsl:text>"</xsl:text>
    
          <xsl:if test="position() != last()">
            <xsl:value-of select="','" />
          </xsl:if>
        </xsl:for-each>
        <xsl:text>
    </xsl:text>
      </xsl:template>
    
    </xsl:stylesheet>
  9. #39
    Thanks for the full example.

    The problem is the fact that AJAX file downloading requires a <form>.

    To fix the problem you can:

    1. Add
    <form id="form1" runat="server" class="x-hide-display">
    </form>
    into Search.aspx.

    and

    2. Set up
    excelButton.DirectEvents.Click.FormID = "form1";
  10. #40
    It works thx :)
Page 4 of 4 FirstFirst ... 234

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