locked
Reading Excel content into asp.net page RRS feed

  • Question

  • User419973999 posted

    Hi ASP.net experts,

                                     I have some numerical data in an EXcel sheet. I need to  get that data and process in my aspx page. I tried to understand  by googling how to fetch an entry from Excel into a variable (Lable for example) in my aspx page but was not successful.  If you can please point me to  simple example or  let my know how to code it, it would really appriciate it. Please note my knowledge in databases is almost nill.


    Cheers,

    Tosho

    Saturday, January 30, 2010 1:18 PM

All replies

  • User187056398 posted

    This may help:

            // using System.Data.OleDb
            OleDbConnection ExcelConection = null;
            OleDbCommand ExcelCommand = null;
            OleDbDataReader ExcelReader = null;
            OleDbConnectionStringBuilder OleStringBuilder = null;
    
            try
            {
                OleStringBuilder =
                    new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
                OleStringBuilder.DataSource = MapPath(@"~\App_Datav\MyExcelWorksheet.xls");
    
                ExcelConection = new OleDbConnection();
                ExcelConection.ConnectionString = OleStringBuilder.ConnectionString;
    
                ExcelCommand = new OleDbCommand();
                ExcelCommand.Connection = ExcelConection;
                ExcelCommand.CommandText = "Select * From [Sheet1$]";
    
                ExcelConection.Open();
                ExcelReader = ExcelCommand.ExecuteReader();
    
                GridView1.DataSource = ExcelReader;
                GridView1.DataBind();
            }
            catch (Exception Args)
            {
                LabelErrorMsg.Text = "Could not open Excel file: " + Args.Message;
            }
            finally
            {
                if (ExcelCommand != null)
                    ExcelCommand.Dispose();
                if (ExcelReader != null)
                    ExcelReader.Dispose();
                if (ExcelConection != null)
                    ExcelConection.Dispose();
            }
    


     

    Saturday, January 30, 2010 1:37 PM
  • User419973999 posted

    Thank you very much Steve.

    The code worked fine, it brought my Excel sheet to ASP.net page that I could see on the client page (web browser). Could you please advise me how I can address elements in the sheet. I need to pull the numbers from the sheet and process them. For example,in Excel, the addresses are A1, A2, D5, E7 etc.

    I would really appreciate if you could please give me an example with reference to your advised  code in your earlier post, it shall get me going.


    Best regards,

    Tosho



    Saturday, January 30, 2010 3:55 PM