Create excel file test.xlsx to directory c:\
Test code
WriteExcelFile();Reference
Microsoft.Office.Interop.ExcelAdd namespace to souce code
using Microsoft.Office.Interop.Excel;using System.Reflection;
Add function to souce code
private void WriteExcelFile(){
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);
book = app.Workbooks.Add(Missing.Value);
sheet = (Worksheet)book.Worksheets[1];
sheet.get_Range("A1", "A1").Value2 = "測試";
book.SaveAs(@"C:\test1.xlsx", Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
}
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;
}
}
沒有留言:
張貼留言