xRalf
asked on
reading Excel via Microsoft.Office.Interop.Excel
Hello,
I'd like to read data from Excel file via Microsoft.Office.Interop.E xcel. I want to get every column from Excel file into one string array.
thanks
I'd like to read data from Excel file via Microsoft.Office.Interop.E
thanks
Oups, sorry I forgot then :
ObjWorkSheet = (Microsoft.Office.Interop. Excel.Work sheet)ObjW orkBook.Sh eets[1];
And then for example let's play with ObjWorkSheet.Cells collection.
ObjWorkSheet = (Microsoft.Office.Interop.
And then for example let's play with ObjWorkSheet.Cells collection.
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...
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.E xcel.Appli cation ObjExcel = new Microsoft.Office.Interop.E xcel.Appli cation();
Workbook ObjWorkBook;
Worksheet ObjWorkSheet;
ObjWorkBook = ObjExcel.Workbooks.Open(@" C:\Temp\Te mp.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.Work sheet)ObjW orkBook.Sh eets[1];
foreach (Range r in ObjWorkSheet.Rows)
{
System.Array myvalues = (System.Array)r.Cells.Valu e2;
int a = 1;
}
Microsoft.Office.Interop.E
Workbook ObjWorkBook;
Worksheet ObjWorkSheet;
ObjWorkBook = ObjExcel.Workbooks.Open(@"
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.
foreach (Range r in ObjWorkSheet.Rows)
{
System.Array myvalues = (System.Array)r.Cells.Valu
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;
}
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.
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(",");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Microsoft.Office.Interop.E
Microsoft.Office.Interop.E
ObjWorkBook = ObjExcel.Workbooks.Open(@"
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.