reading Excel via Microsoft.Office.Interop.Excel

Hello,

I'd like to read data from Excel file via Microsoft.Office.Interop.Excel. I want to get every column from Excel file into one string array.

thanks
LVL 6
xRalfAsked:
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.

kaylanreilorCommented:
           Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook ObjWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet ObjWorkSheet;

            ObjWorkBook = ObjExcel.Workbooks.Open(@"C:\Temp\Temp.xls", false, true,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing);

... Adn then let see what the documentation proposes.
kaylanreilorCommented:
Oups, sorry I forgot then :

ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBook.Sheets[1];
And then for example let's play with ObjWorkSheet.Cells collection.
xRalfAuthor Commented:
>> ... Adn then let see what the documentation proposes.

Well, and this is all this question about. There are many classes and methods in documentation and I want some advice which of them are convenient for my purposes...
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

kaylanreilorCommented:
... Or ObjWorkSheet.Columns :)
kaylanreilorCommented:
OK, you can try something like that :
            Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
            Workbook ObjWorkBook;
            Worksheet ObjWorkSheet;

            ObjWorkBook = ObjExcel.Workbooks.Open(@"C:\Temp\Temp.xls", false, true,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBook.Sheets[1];
            foreach (Range r in ObjWorkSheet.Rows)
            {
                System.Array myvalues = (System.Array)r.Cells.Value2;
                int a = 1;
            }
kaylanreilorCommented:
Re-Oups... Sorry, you said strings :
            ObjWorkBook = ObjExcel.Workbooks.Open(@"C:\Temp\Temp.xls", false, true,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBook.Sheets[1];
            foreach (Range r in ObjWorkSheet.Rows)
            {
                System.Array myvalues = (System.Array)r.Cells.Value2;
                string Value1 = myvalues.GetValue(1, 1) != null ? myvalues.GetValue(1, 1).ToString() : string.Empty;
                int a = 0;
            }

Open in new window

xRalfAuthor Commented:
Thanks,

and could you explain if I have the array

string [] _column1;

how to get the data from column1 in excel into column1? This is not clear for me from the above code.
kaylanreilorCommented:
I propose that you look at the following code in debug (that is idea from the beginning) and then I guess you'll understand quickly what you can do. Look at the strings into the array. This code is buggee don't use it as final version of anything.
            StringBuilder[] arrayStrs = new StringBuilder[ObjWorkSheet.Columns.Count];
            Range r = ObjWorkSheet.Columns;
            for (int i = 0; i < ObjWorkSheet.Columns.Count; ++i)
            {
                arrayStrs[i] = new StringBuilder();
                foreach (Range rc in r.Cells)
                {
                    if (rc.Value2 == null)
                        continue;
                    arrayStrs[i].Append(rc.Value2.ToString());
                    arrayStrs[i].Append(",");
                }
            }

Open in new window

kaylanreilorCommented:
Sorry but I hadn't any Visual Studio nor documentation when I wrote first post... Here is another code snipped closer from what you are looking for:
            int i = 0;
            int noOfDataRowsInExcelFile = ObjWorkSheet.get_Range("A65536", "A65536").get_End(XlDirection.xlUp).Row;
            Range end = ObjWorkSheet.Rows.get_End(XlDirection.xlDown);
            foreach (Range r in ObjWorkSheet.Rows)
            {
                if (i >= noOfDataRowsInExcelFile)
                    break;
                foreach (Range c in r.Columns)
                {
                    if (c.Value2 == null)
                        break;
                    if (arrayStrs[i] == null)
                        arrayStrs[i] = new StringBuilder();
                    arrayStrs[i].Append(c.Value2.ToString());
                    arrayStrs[i].Append(",");
                }
                ++i;
            }

Open in new window

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
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
C#

From novice to tech pro — start learning today.