现在的位置: 首页 > 综合 > 正文

使用C#创建Excel文档

2012年10月06日 ⁄ 综合 ⁄ 共 749字 ⁄ 字号 评论关闭

//Com Microsoft Excel 12.0 Object Library //2007 ,11.0-2003

using MSExcel = Microsoft.Office.Interop.Excel;

using System.IO;

using System.Reflection;

Class Program

{

  static void Main(string[] args(

  {

    string path;  //文件路径

    MSExcel.Application excelApp;  //Excel应用程序

    MSExcel.WorkBook excelDoc;  //Excel文档

    path = @"c:/test.xlsx";

    excelApp = new MSExcel.ApplicationClass();

    if(File.Exists(path)

    {

      File.Delete(path);

    }

    Object nothing = Missing.Value;

    excelDoc = excelApp.Workbooks.Add(nothing);

    Object format = MSExcel.XlFileFormat.xlWorkbookDefault;

    exceldoc.SaveAs(path,nothing,nothing,nothing,nothing,nothing,

          MSExcel.XlSaveAsAccessMode.xlExclusive,nothing,nothing,nothing,nothing,nothing);

    excelDoc.Close(nothing,nothing,nothing);

    excelApp.Quit();

  }

}

抱歉!评论已关闭.