26 Ocak 2012 Perşembe

.NET Excel Dosyayı Okuma

Microsoft.Office.Interop.Excel dll ini eklemeniz lazım.
Bu linkten indirebilirsiniz:

Microsoft.Office.Interop.Excel

private static Microsoft.Office.Interop.Excel.Application excelNesnesi = new Microsoft.Office.Interop.Excel.Application();
static object type = Type.Missing;
//relative path for the excel file in the project directory
static string directory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
static string pathMesafe = System.IO.Path.Combine(directory, "ilmesafe.xls");
Workbook theWorkbook = excelNesnesi.Workbooks.Open(pathMesafe, type, type, type, type, type, type,type, type, type, type, type, type, type, type);
//sayfaları al
Sheets sheets = theWorkbook.Worksheets;
//bu örnekte tek bir sayfa olduğu için get_Item(1) dedim
Worksheet worksheet =(Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
//bir aralık belirtiyoruz A'dan CE'e kadar, c1 bir integer mesela: Range(A1, CE1)
Range rangeCity = worksheet.get_Range("A" + (c1 + 1).ToString(), "CE" + (c1 + 1).ToString());
//bu range deki değerleri bir arraye atıyoruz
System.Array myvalues = (System.Array)rangeCity.Cells.Value2;
//bu array dende hangi hücreyi istiyosak alıyoruz c2: integer
string value = myvalues.GetValue(1, c2 + 2).ToString();

Hiç yorum yok:

Yorum Gönder