using Microsoft.Office.Interop.Excel;
using System.Reflection;
public class MyConsole
{
public static void Main()
{
Application app = new ApplicationClass();
try
{
app.Visible = false;
Workbook wBook = app.Workbooks.Add(true);
Worksheet wSheet = wBook.Worksheets[1] as Worksheet;
wSheet.Cells[1, 1] = "this is a test";
app.DisplayAlerts = false;
app.AlertBeforeOverwriting = false;
wBook.Save();
app.Save("C:\\abc.xls");
}
catch { }
finally
{
app.Quit();
app = null;
}
}
}
ApplicationClass,設 Visible = false 背景執行。Workbooks.Add(true) 新增活頁簿(或用 Open 開啟既有檔)。Worksheets[1](索引從 1 開始)。Cells[row, col]。DisplayAlerts = false、AlertBeforeOverwriting = false。Workbook.Save() 後 Application.Save(path)。finally 區塊確保 Quit() 並釋放參考。下一篇將詳細說明儲存權限問題、進程殘留、多工作表操作等進階主題。