Link to home
Start Free TrialLog in
Avatar of xRalf
xRalf

asked on

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
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

           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.
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.
Avatar of xRalf
xRalf

ASKER

>> ... 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...
... Or ObjWorkSheet.Columns :)
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;
            }
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

Avatar of xRalf

ASKER

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.
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

ASKER CERTIFIED SOLUTION
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial