C#如何新建Excel
此处通过添加Office COM组件的方式,来建立Excel表
方法/步骤
-
项目,右击 引用——添加引用(COM组件,需要安装office才有)
-
程序,添加引用
-
类,添加代码
[DllImport("User32.dll", CharSet = CharSet.Auto)]
-
初始化
Excel.Application EApp; //Excel应用程序
Excel.Workbook EWBook; //Excel文档
Excel.Worksheet WSheet;
EApp = new Excel.ApplicationClass();
-
新建表
object nothing = Missing.Value;
EWBook = EApp.Workbooks.Add(nothing);
WSheet = EWBook.Worksheets[1] as Excel.Worksheet;
-
保存工作簿
WSheet.SaveAs(EPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
EWBook.Save();
-