首先添加对Com组件Microsoft.Excel 11.0 Object Library的引用。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace Avisnet
{
class ExcelProgram
{
static void
{
ExcelProgram p = new ExcelProgram();
p.FormulaTest();
}
public void FormulaTest()
{
// Starts excel and gets an excel application object.
Excel.Application excel = new Excel.Application();
// Adds a new workbook to the excel application.
Excel.Workbook book = excel.Workbooks.Add(Missing.Value);
Excel.Worksheet sheet = (Excel.Worksheet)book.ActiveSheet;
try
{
Excel.Range range = sheet.get_Range("D2", "D8");
range.Formula = "=
range.NumberFormat = "$0.00";
// Saves and cloeses the workbook;
book.Close(true, "C:\\fx.xls", Missing.Value);
}
catch
{
throw;
}
finally
{
// Exit excel application.
excel.Quit();
}
}
}
}