вот пример, заполняет datagridview данными из этого файла
Код:
private void button1_Click(object sender, EventArgs e)
{
string str;
int r;
int c;
string filename = "1.xls";
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook ExcelWorkBook;
Microsoft.Office.Interop.Excel.Worksheet ExcelWorkSheet;
Microsoft.Office.Interop.Excel.Range ExcelRange;
ExcelWorkBook = ExcelApp.Workbooks.Open(filename);
ExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelWorkBook.Worksheets.get_Item(1);
ExcelRange = ExcelWorkSheet.UsedRange;
for (r = 1; r <= ExcelRange.Rows.Count; r++)
{
dataGridView1.Rows.Add(1);
for (c = 1; c <= 2; c++)
{
str = Convert.ToString((ExcelRange.Cells[r, c] as Microsoft.Office.Interop.Excel.Range).Value2);
dataGridView1.Rows[r - 1].Cells[c - 1].Value = str;
}
}
ExcelWorkBook.Close();
ExcelApp.Quit();
releaseObject(ExcelWorkSheet);
releaseObject(ExcelWorkBook);
releaseObject(ExcelApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the object " + ex.ToString());
}
finally
{
GC.Collect();
}
}