[CLOSED] Import Excel into Grid

  1. #1

    [CLOSED] Import Excel into Grid

    Hi, I'm using ASP.NET MVC and need import the content of a Excel File (using FileUploadField) into a Grid, but I'm getting successfull.

    I want to load the grid after the user click in a button. I had read in another post that is not possible populate a store with JSON, right? But I'm not to use the "drag and drop solution". What do I do?



    
    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
    
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Xml.Xsl" %>
    <%@ Import Namespace="System.Xml" %>
    <%@ Import Namespace="System.Linq" %>
    <%@ Import Namespace="System.Threading" %>
    <%@ 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">
    <script type="text/javascript" src="../../Scripts/jquery-1.5.1.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript" src="../../Scripts/SimpleProgress-fx.js?v1"></script>
    <script type="text/javascript" src="../../Scripts/jquery.simplemodal-1.4.2.js"></script>
    <script runat="server">
    
    </script>
    <html>
    <head runat="server">
        <title>UploadView</title>
        </head>
    <body>
        <form id="form" runat="server">
        <ext:ResourceManager ID="resourceUploadView" runat="server" />
        <ext:FormPanel ID="formUpload" runat="server" AutoDataBind="true">
            <Items>
                <ext:FileUploadField ID="fileUpload" runat="server" Width="300" />
            </Items>
            <Buttons>
                <ext:Button ID="btnCarregar" runat="server" Width="80" Text="Incluir">
                    <DirectEvents>
                        <Click Before="return #{formUpload}.getForm().isValid();" Url="../../Handler/LerExcel.ashx"
                            CleanRequest="true" Method="POST" Success="#{gridExcel}.store.reload();">
                        </Click>
                    </DirectEvents>
                </ext:Button>
            </Buttons>
        </ext:FormPanel>
        <ext:GridPanel Layout="FitLayout" ID="gridExcel" runat="server" StripeRows="true"
          Title="Excel">
            <Store>
                <ext:Store ID="storeExcel" runat="server" WarningOnDirty="false" AutoDataBind="true"
                    AutoLoad="false">
                    <Reader>
                        <ext:JsonReader Root="Data" TotalProperty="TotalRecords">
                            <Fields>
                                <ext:RecordField Name="Codigo" />
                                <ext:RecordField Name="Nome" />
                                <ext:RecordField Name="Data" />
                                <ext:RecordField Name="Obs" />
                            </Fields>
                        </ext:JsonReader>
                    </Reader>
                    <DirectEventConfig IsUpload="true" />
                </ext:Store>
            </Store>
            <ColumnModel ID="columnmodelStatusContrato" runat="server">
                <Columns>
                    <ext:Column ColumnID="Codigo" Header="Código" DataIndex="Codigo" Width="80" />
                    <ext:Column Header="Nome" DataIndex="Nome" Width="120" />
                    <ext:Column Header="Data" DataIndex="Nome" Width="120" />
                    <ext:Column Header="Obs" DataIndex="Obs" Width="60" />
                </Columns>
            </ColumnModel>
            <LoadMask ShowMask="true" />
        </ext:GridPanel>
           </form>
    </body>
    </html>

    The "LerExcel" event returns a JSON string.

    Thank you
    Last edited by Daniil; Mar 13, 2012 at 2:24 PM. Reason: [CLOSED]
  2. #2
    Hi,

    Please clarify why do you use FileUploadField?

    I had read in another post that is not possible populate a store with JSON, right?
    Well, it's wrong. On client side you can use the Store loadData method:
    http://docs.sencha.com/ext-js/3-4/#!...ethod-loadData

    Generally, what about to use HttpProxy, something like this:
    https://examples1.ext.net/#/GridPane...rting/Handler/

    An .ashx Handler should return a respective JSON according to the Store Reader.
  3. #3
    Daniil, I want my system "read" the excel file that the user uploaded.

    So, my handler returns a JSON to populate the Excel file. This should happen when the button is clicked.

    See my Handler:

            public void ProcessRequest(HttpContext context)
            {
    
                System.Web.HttpPostedFile file = context.Request.Files[0];
    
                 ... (reading the file)
    
                DataSet result = "..." (contains the value of excel)
    
                Paging<Dados> dados = MontarLista(result);
    
                string json = JSON.Serialize(dados);
    
                context.Response.Write(json);
            }
    
            private Paging<Dados> MontarLista(DataSet result)
            {
                List<Dados> dados = new List<Dados>();
    
                foreach (DataRow row in result.Tables[0].Rows)
                {
                    if (row["Codigo"].ToString() == "")
                        continue;
                    Dados dado = new Dados();
    
                    dado.Codigo = row["Codigo"].ToString();
                    dado.Nome = row["Nome"].ToString();
                    dado.Data = row["Data"].ToString();
                    dado.Obs = row["Obs"].ToString();
    
    
                    dados.Add(dado);
                }
    
                return new Paging<Dados>(dados, dados.Count);
            }

    json string value:

    {"Data":[{"Codigo":"1","Nome":"SD","Data":"aaa","Obs":"ss"} ,{"Codigo":"2","Nome":"dfa","Data":"adsf","Obs":"f dsa"}],"TotalRecords":2}
  4. #4
    What do I do?

    Thank you
  5. #5
    Quote Originally Posted by mcfromero View Post
    What do I do?

    Thank you
    There is no functionality within Ext.NET to read Excel files. There might be other 3rd party libraries available to help with Excel processing tasks.

    Hope this helps.
    Geoffrey McGill
    Founder
  6. #6
    Hi,

    To read the Excel is not the problem. I'm doing it already.

    My problem is populate a grid after user click in a button. My button call a "Handler", so this handler create a JSON string.



    Thank you
  7. #7
    Well, as I suggested before (in the post #2) you should use the Store loadData method.
    Success="#{gridExcel}.getStore().loadData(result);"
    Here is the full sample.

    Example Page
    <%@ Page Language="C#" %>
     
    <%@ 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>Ext.NET Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
                <Store>
                    <ext:Store runat="server" AutoLoad="false">
                        <Reader>
                            <ext:ArrayReader>
                                <Fields>
                                    <ext:RecordField Name="test1" />
                                    <ext:RecordField Name="test2" />
                                    <ext:RecordField Name="test3" />
                                </Fields>
                            </ext:ArrayReader>
                        </Reader>
                    </ext:Store>
                </Store>
                <ColumnModel runat="server">
                    <Columns>
                        <ext:Column Header="Test1" DataIndex="test1" />
                        <ext:Column Header="Test2" DataIndex="test2" />
                        <ext:Column Header="Test3" DataIndex="test3" />
                    </Columns>
                </ColumnModel>
            </ext:GridPanel>
            <ext:Button runat="server" Text="Load">
                <DirectEvents>
                    <Click Url="Test.ashx" Success="GridPanel1.getStore().loadData(result);" />
                </DirectEvents>
            </ext:Button>
        </form>
    </body>
    </html>
    Example Handler
    using System;
    using System.Web;
    using Ext.Net;
     
    namespace Work
    {
        public class Test : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                object[] data = new object[] 
                { 
                    new object[] { "test1", "test2", "test3" },
                    new object[] { "test4", "test5", "test6" },
                    new object[] { "test7", "test8", "test9" },
                };
    
                context.Response.Write(JSON.Serialize(data));
                context.Response.End();
            }
     
            public bool IsReusable
            {
                get { return false; }
            }
        }
    }
    Last edited by Daniil; Mar 13, 2012 at 2:23 PM.
  8. #8
    Worked!
    Thank you Daniil.

Similar Threads

  1. Import excel to Datagrid
    By xborderland in forum 1.x Help
    Replies: 3
    Last Post: Nov 03, 2016, 6:31 AM
  2. Export the grid to excel
    By Mr.Techno in forum 1.x Help
    Replies: 34
    Last Post: Aug 23, 2013, 11:39 AM
  3. [CLOSED] how to export few columns from grid store to excel?
    By rnachman in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Mar 28, 2012, 8:32 PM
  4. Excel Export in Grid
    By BLOZZY in forum 1.x Help
    Replies: 0
    Last Post: Oct 09, 2011, 11:37 AM
  5. Paste from Excel into Grid?
    By rthiney in forum 1.x Help
    Replies: 3
    Last Post: Sep 14, 2009, 7:11 PM

Posting Permissions