object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
excel.Visible = false;
Excel.Workbooks oBooks = excel.Workbooks;
//打开指定文件
Excel.Workbook oBook = oBooks.Add(Server.MapPath("1-1.xls"));
excel.DisplayAlerts = false;
//写入数据
for (int i = 10; i < 25; i++)
{
for (int j = 4; j < 17; j ++)
{
excel.Cells[i,j] = i + j;
}
}
//保存文件
oBook.SaveAs(Server.MapPath("test.xls"),missing,missing,missing,missing,missing,XlSaveAsAccessMode.xlExclusive,
missing,missing,missing,missing,missing);
excel.DisplayAlerts = true;
//关闭进程,垃圾回收
oBook.Close(missing,Server.MapPath("Book1.xls"),missing);
if(oBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (oBook);
oBook = null;
if(oBooks != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (oBooks);
oBooks = null;
if(excel != null)
{
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
excel = null;
}
GC.Collect();