using System;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
class ExcelAutomate
{
public static void RunExcel()
{
const string wbName = "C:\\My Documents\\Book1.xls";
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWB = xlApp.Workbooks.Open(wbName, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Excel.Worksheet xlWS = (Excel.Worksheet)xlWB.Worksheets.get_Item("Sheet1");
Excel.Range rng = xlWS.get_Range("A1", Type.Missing);
// Show Excel to the User:
xlApp.Visible = true;
// Return the Adddress:
MessageBox.Show(rng.get_Address(Type.Missing, Type.Missing,
Excel.XlReferenceStyle.xlA1 ,
Type.Missing, Type.Missing));
// Return the Value held:
MessageBox.Show(rng.get_Value(Type.Missing).ToString());
// Return the Text as formatted in the Cell:
MessageBox.Show(rng.Text.ToString());
// Close down Excel cleanly:
rng = null;
xlWS = null;
xlWB.Close(false, System.Type.Missing, System.Type.Missing);
xlApp.Quit();
xlApp = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}