C# 填充Excel

时间:2021-10-02 06:18:03

1、添加引用

Microsoft.Office.Interop.Excel;

2、使用命名空间

using Microsoft.Office.Interop.Excel;

3、填充EXCEL单元格方法

private void OpenExcel()
        {
            Microsoft.Office.Interop.Excel.Application excel = new ApplicationClass();
            excel.Visible = true;
            excel.DisplayAlerts = false;
            excel.Workbooks.Open(@"D:\EXCEL\IQC检验报表.xlsx");
            ((_Worksheet) excel.Worksheets[1]).Activate();         
            excel.get_Range("F7",Type.Missing).Value = "测试Excel写入数据";
            excel.get_Range("F7", Type.Missing).Font.Color = Color.Blue;
            excel.ActiveWorkbook.Save();
            excel.Application.Workbooks.Close();
            excel.Application.Quit();
            excel.Quit();
            GC.Collect();
        }