Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to read data in excel file

How to read data in excel file

Scheduled Pinned Locked Moved C#
questionhelptutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hoang Dung
    wrote on last edited by
    #1

    Hi, How do I read data in cells of Excel file by using Microsoft Excel Object 9.0 component? Please help me. Thanks H.Dung

    M K I 3 Replies Last reply
    0
    • H Hoang Dung

      Hi, How do I read data in cells of Excel file by using Microsoft Excel Object 9.0 component? Please help me. Thanks H.Dung

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      I guess there is article about Ecxel and C# in CP. Mazy No sig. available now.

      1 Reply Last reply
      0
      • H Hoang Dung

        Hi, How do I read data in cells of Excel file by using Microsoft Excel Object 9.0 component? Please help me. Thanks H.Dung

        K Offline
        K Offline
        Kannan Kalyanaraman
        wrote on last edited by
        #3

        http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306023[^] HTH Cheers, Kannan

        I 1 Reply Last reply
        0
        • K Kannan Kalyanaraman

          http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q306023[^] HTH Cheers, Kannan

          I Offline
          I Offline
          Ista
          wrote on last edited by
          #4

          same way as vb

          1 Reply Last reply
          0
          • H Hoang Dung

            Hi, How do I read data in cells of Excel file by using Microsoft Excel Object 9.0 component? Please help me. Thanks H.Dung

            I Offline
            I Offline
            Ista
            wrote on last edited by
            #5

            This reads it into a DataTable public void CreateXLSDataTable( String fileName ) { //DataTable tb = new DataTable("XLS"); dtXLS = new DataTable(); Excel.Application ExcelObj = null; ExcelObj = new Excel.Application(); if ( ExcelObj != null ) { Excel.Workbook empBook = ExcelObj.Workbooks.Open(fileName,0,true,5,"","",true, Excel.XlPlatform.xlWindows,"\t",false,false,0,true); Excel.Sheets empSheets = empBook.Worksheets; Excel.Worksheet empSheet = (Excel.Worksheet)empSheets.get_Item(1); bool lineValid = true; int iCount = 1; while ( lineValid ) { iCount++; Excel.Range range = empSheet.get_Range("A"+iCount.ToString(), "X"+iCount.ToString()); System.Array myvalues = (System.Array)range.Cells.Value; string[] strArray = ConvertToStringArray(myvalues); if(strArray[0].Equals("") ) { lineValid = false; continue; } if( iCount != 2 ) { object[] objArray = strArray; dtXLS.Rows.Add(objArray); } else { for(int i=0; i< strArray.Length; i++) { dtXLS.Columns.Add(strArray[i]); } } } } else { Trace.WriteLine("Excel reading Error: could not load the excel file object"); } ExcelObj.Quit(); ExcelObj = null; } private string[] ConvertToStringArray(System.Array values) { string[] theArray = new string[values.Length]; for(int i=1; i<=values.Length; i++ ) { if( values.GetValue(1,i) == null ) theArray[i-1] = ""; else theArray[i-1] = (string)values.GetValue(1,i).ToString().Trim(); } return theArray; } public DataTable XLS { get { return dtXLS; } }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups