• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

C# read excel

Hi,

I have two question about using C# to access excel. Please help me about this thanks

(1) after read excel file into datatable, how can I transfer some column of the data table to an array
or
(2) is it possible to use index to access a certain content in the datatable (like data in row 1, column 5).
0
rmtogether
Asked:
rmtogether
  • 3
3 Solutions
 
anarki_jimbelCommented:
To convert a row into array is pretty simple:
 - create an array of proper size (number of columns)
 - iterate through columns using 'for' loop in the row and put values into the array.

Accessing columns and rows by index - possible.You do like below (e.g.):

string value = myDataTable.Rows[rowIndex].Columns[colIndex].

You may also access columns by their names
0
 
anarki_jimbelCommented:
Have a look at some examples for rows and columns in the dataTable class:

http://msdn.microsoft.com/en-us/library/system.data.datatable_properties.aspx
0
 
anarki_jimbelCommented:
Consider a snippet:

Output like here:

1      aaa      Hello      
2      bbb      Good bye      
3      ccc      Cool!      
4      ddd      Cheers!      
5      eee      Thank you      
        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("ID");
            DataColumn dc2 = new DataColumn("Name");
            DataColumn dc3 = new DataColumn("Message");

            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);

            DataRow dr1 = dt.NewRow();
            DataRow dr2 = dt.NewRow();
            DataRow dr3 = dt.NewRow();
            DataRow dr4 = dt.NewRow();
            DataRow dr5 = dt.NewRow();

            dr1[0] = 1;
            dr2[0] = 2;
            dr3[0] = 3;
            dr4[0] = 4;
            dr5[0] = 5;
            dr1[1] = "aaa";
            dr2[1] = "bbb";
            dr3[1] = "ccc";
            dr4[1] = "ddd";
            dr5[1] = "eee";
            dr1[2] = "Hello";
            dr2[2] = "Good bye";
            dr3[2] = "Cool!";
            dr4[2] = "Cheers!";
            dr5[2] = "Thank you";


            dt.Rows.Add(dr1);
            dt.Rows.Add(dr2);
            dt.Rows.Add(dr3);
            dt.Rows.Add(dr4);
            dt.Rows.Add(dr5);
            
            object[] arrayOfAllRowArrays = new object[dt.Rows.Count];
           
            for(int j = 0;j<dt.Rows.Count;j++)
            {
                object[] rowArray = new object[dt.Columns.Count];
                for (int i = 0; i < rowArray.Length ; i++)
                {
                    rowArray[i] = dt.Rows[j][i];
                }
                arrayOfAllRowArrays[j] = rowArray;
            }
            // print to output
            foreach (object o in arrayOfAllRowArrays)
            {
                object[] row = (object[]) o;
                foreach (object cell in row)
                {
                    System.Diagnostics.Debug.Write(cell.ToString()+ "\t");
                }
                System.Diagnostics.Debug.WriteLine("");
            }
        }

Open in new window

0
 
rmtogetherAuthor Commented:
thank you guys.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now