-
read from an excel
I wanna read the values of each cell in the excel thru my c# code
im using the following line of code but its not working
pla tell me whats wrong here and suggest me the correct solution thnx
Code:
excel = new Microsoft.Office.Interop.Excel.Application();
excelworkbook = (Microsoft.Office.Interop.Excel._Workbook)(excel.Workbooks.Add("C:\\A.xls");
excelworksheet = (Microsoft.Office.Interop.Excel._Worksheet)excelworkbook.Sheets[1];
for (int i = 1; i <= excelworksheet.UsedRange.Columns.Count; i++)
tdbExcel.Columns[i - 1].Text = excelworksheet.UsedRange.get_Item(1, i).ToString();
-
Re: read from an excel
What is the error(s) or specify more then "not working". ;)
Perhaps my C# Excel FAQ may help?
http://vbforums.com/showthread.php?t=406639
-
Re: read from an excel
{"Object reference not set to an instance of an object."}
this is the error that m getting :(
-
Re: read from an excel
On what line of code?
"excel" is a reserved word as I would suggest not using it and rename it to something else.
Are you using the "using" imports at the top of your class file?
-
Re: read from an excel
i m getting this error at following line
Code:
tdbExcel.Columns[i - 1].Text = excelworksheet.UsedRange.get_Item(1, i).ToString();
no Excel is a reserve word while excel is not and secondly m not using "Using" imports for Excel as i have added refrence of excel
that is working fine for writing excel but to read it ... its causng problem
-
Re: read from an excel
Then it could be that your tdbExcel is not created?