从c#应用程序创建excel文件

本文关键字:excel 文件 创建 应用程序 | 更新日期: 2023-09-27 18:07:15

我正在尝试创建一个excel文件形式的c#应用程序。

我的代码如下,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

public class ExcelFileCreation
{
    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    ***xlApp = new Excel.ApplicationClass();***
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    //and further lines of code.....
}

我在上面突出显示的行中得到以下错误,

互操作类型'Microsoft.Office.Interop.Excel。ApplicationClass'不能嵌入。请使用合适的接口。

请帮我解决这个问题!

从c#应用程序创建excel文件

Excel.Application代替Excel.ApplicationClass

xlApp = new Excel.Application();

更新:您正在引用一个互操作程序集,其中"嵌入互操作类型"设置为true。根据这篇文章Class类型包含元数据代码,而接口只包含元数据。嵌入元数据是安全的,但不能嵌入任何可能包含可执行代码的内容。

Replace

xlApp = new Excel.ApplicationClass();

xlApp = new Excel.Application();