[CLOSED] hide a field while exporting data to Excel

  1. #1

    [CLOSED] hide a field while exporting data to Excel

    Hello,
    I am trying to export some data from a Store to an Exel file. Currently my code works fine exporting all the RecordField in the Store.
    However, I only need to export 3 out of the 4 fields - I do not want to export the RecordField named "SomeID". Can you show me how to not export
    certain fields in the Store?

    Thank you very much in advance for your help.

    Below is my code and xsl for your review:
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test2.aspx.cs" Inherits="Web.Pages.test2" %>
    
    <%@ Register Assembly="Ext.Net" TagPrefix="ext" Namespace="Ext.Net" %>
    
    <script runat="server">    
        protected void PeriodStore_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            try
            {
                var json = ExcelData.Value.ToString();
                var eSubmit = new StoreSubmitDataEventArgs(json, null);
                var xml = eSubmit.Xml;
    
                if (xml.InnerText == "")
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Title = "No Data to Export",
                        Message = "No records available for selection",
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.INFO
                    });
                    return;
                }
                Response.Clear();
    
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Disposition", "attachment; filename=submittedDataTest.xls");
                var xtExcel = new System.Xml.Xsl.XslCompiledTransform();
                xtExcel.Load(Server.MapPath("../Resources/Excel.xsl"));
    
                xtExcel.Transform(xml, null, Response.OutputStream);
                Response.End();
            }
            catch (Exception ex)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Title = "Error",
                    Message = ex.Message,
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR
                });
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.BindData();
            }
        }
    
        private void BindData()
        {
            var store = this.gridRuns.GetStore();
    
            store.DataSource = this.Data;
            store.DataBind();
        }
    
        private object[] Data
        {
            get
            {
                DateTime now = DateTime.Now;
    
                return new object[]
                {
                    new object[] { "3m Co", 71.72, 0.02, 0.03},
                    new object[] { "Alcoa Inc", 29.01, 0.42, 1.47 },
                };
            }
        }
    </script>
    
    <script type="text/javascript">
        var exportToExcel = function() {
            ExcelData.setValue(Ext.encode(RunStore.getRecordsValues({ selectedOnly: false })));
            RunStore.submitData(true);
        };  
    
    </script>
    
    <!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 id="Head1" runat="server">
        <title>Ext.NET Example</title>
    </head>
    <body>
        <form id="Form1" runat="server">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <ext:Button runat="server" Icon="PageExcel">
            <listeners>
                <Click Fn="exportToExcel" />
            </listeners>
        </ext:Button>
        <ext:GridPanel ID="gridRuns" runat="server" Cls="my-grid" Title="Runs" Margins="0 0 0 0"
            Icon="ScriptGo" Height="445" Width="463" BodyCssClass="x-grid3-row-selected x-grid-group-title"
            StripeRows="true" TrackMouseOver="true" Frame="false" Border="false" Padding="0"
            HideBorders="true">
            <store>
                <ext:Store ID="RunStore" runat="server" AutoLoad="true" AutoDataBind="true" WarningOnDirty="false" OnSubmitData="PeriodStore_Submit">
                    <DirectEventConfig IsUpload="true" />
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="ShiftName" Type="String" />
                                <ext:RecordField Name="RunId" Type="Int" />
                                <ext:RecordField Name="LineId" Type="Int" />
                                 <ext:RecordField Name="SomeId" Type="Int" />
                                                                    
                             </Fields>
                                                               </ext:ArrayReader>
                                                        </Reader>
                                                       
                                                    </ext:Store>
                                                </store>
        </ext:GridPanel>
        <ext:Hidden ID="ExcelData" runat="server" />
        </form>
    </body>
    </html>
    <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>
    Last edited by Daniil; Jun 20, 2012 at 4:49 PM. Reason: [CLOSED]
  2. #2
    Hi,

    Please see the Click listener of the "Save: filter fields (exclude 'Last Update')" button here:
    https://examples1.ext.net/#/GridPane...s/Save_Filter/

    The filterField option can be applied for the getRowsValues as well.
  3. #3
    Thank you for the quick reply.

    The example you referred me to excludes only one field. Can you show me how to make it work with excluding multiple fields in the record?
    I have about 10-15 fields that I need to exclude from the record.

    Thanks again for your help!

    Quote Originally Posted by Daniil View Post
    Hi,

    Please see the Click listener of the "Save: filter fields (exclude 'Last Update')" button here:
    https://examples1.ext.net/#/GridPane...s/Save_Filter/

    The filterField option can be applied for the getRowsValues as well.
  4. #4
    Well, you just need to return false from a filterField function.

    Example
    function (r, name, value) { 
        return name != 'field1' || name != 'field2';
     }
  5. #5
    OK. Thank you for your help!

    Quote Originally Posted by Daniil View Post
    Well, you just need to return false from a filterField function.

    Example
    function (r, name, value) { 
        return name != 'field1' || name != 'field2';
     }

Similar Threads

  1. Exporting Grid Data for Excel, CSV, XML, HTML, PDF [EXAMPLE Codes]
    By fatihunal in forum Examples and Extras
    Replies: 6
    Last Post: Oct 07, 2016, 1:50 PM
  2. Exporting gridpanel content to excel
    By sonlas7y20 in forum 1.x Help
    Replies: 2
    Last Post: Dec 13, 2011, 8:01 PM
  3. exporting to excel with gridfilters on
    By norphos in forum 1.x Help
    Replies: 0
    Last Post: Oct 18, 2011, 1:14 PM
  4. Replies: 0
    Last Post: Sep 05, 2011, 10:01 AM
  5. [CLOSED] Excel exporting issue
    By Pablo_Azevedo in forum 1.x Legacy Premium Help
    Replies: 9
    Last Post: Oct 20, 2010, 5:12 PM

Tags for this Thread

Posting Permissions