C# read excel


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
(2) is it possible to use index to access a certain content in the datatable (like data in row 1, column 5).
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

anarki_jimbelSenior DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anarki_jimbelSenior DeveloperCommented:
Have a look at some examples for rows and columns in the dataTable class:

anarki_jimbelSenior DeveloperCommented:
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");


            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";

            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");

Open in new window

rmtogetherAuthor Commented:
thank you guys.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.