C# 调用Excel
C#调用Excel需要和COM进行交互,COM是提供接口的一种机制,是用于不同语言的交互。
调用Excel的步骤:
1.在C#项目中添加库引用 Microsoft Office 16.0 Object Library
2.在代码中引用Excel
3.对Excel操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Excel = Microsoft.Office.Interop.Excel; using missing = System.Reflection.Missing; namespace ComExcelLesson { class Program { static void Main(string[] args) { var excel = new Excel.Application(); excel.Visible = true; Excel.Workbook workBook = excel.Workbooks.Add(); ((Excel.Range)excel.Cells[1, 1]).Font.FontStyle = "Bold"; ((Excel.Range)excel.Cells[1, 1]).Value2 = "Hello word"; workBook.SaveAs(@"d:\tmp.xlsx",Password:"123");//此函数有诸多参数,里面可以用missing.value代替即可,missing为using的引用 } } }