Create excel file test.xlsx to directory c:\
Test code
ReadExcelFile();Reference
Microsoft.Office.Interop.ExcelAdd namespace to souce code
using Microsoft.Office.Interop.Excel;using System.Reflection;
Add function to souce code
private void ReadExcelFile(){
ApplicationClass app = new ApplicationClass();
Workbook book = null;
Worksheet sheet = null;
Range range = null;
try
{
app.Visible = true;
app.ScreenUpdating = false;
app.DisplayAlerts = false;
book = app.Workbooks.Open(@"C:\test1.xls", Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value);
sheet = (Worksheet)book.Worksheets[1];
string value = sheet.get_Range("A1", "A1").Value2;
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
range = null;
sheet = null;
if (book != null)
book.Close(false, Missing.Value, Missing.Value);
book = null;
if (app != null)
app.Quit();
app = null;
}
}
沒有留言:
張貼留言