How to get single value from excel sheet using c#

Posted by Seenuvasan under C# on 8/25/2011 | Points: 10 | Views : 30525 | Status : [Member] | Replies : 2
Hi,


How to get a single cell value from excel sheet using c#
Example:

i want to retrive "c5" cell value from excel sheet .


Please guide me

Thanks,
Seen



Responses

Posted by: Ndebata on: 8/25/2011 [Member] Starter | Points: 25

Up
0
Down
Try to use Interop.Excel
add a reference to it.
using XLS = Microsoft.Office.Interop.Excel; 


XLS.Application myapp=new XLS.ApplicationClass();
XLS.Workbook wb = myapp.Workbooks.Open(@"d:\Mapping.xls");
XLS.Worksheet sheet = (XLS.Worksheet)wb.Worksheets.get_Item(1);

var cell = (XLS.Range)sheet.Cells[3,5];
Console.Write(cell.Value2);


Seenuvasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kunalverma31 on: 8/26/2011 [Member] Starter | Points: 25

Up
0
Down
You can name the Cell as "ABC" and retrieve value using below code
string ConnectionString = string.Format("Data Source='{0}';Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES;'", path);
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = @"SELECT * FROM " + "ABC";
connection.Open();
}

Seenuvasan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response