简体   繁体   English

从C#应用程序创建Excel文件

[英]creating excel file from an c# application

I am trying to create an excel file form c# application. 我正在尝试创建一个Excel文件形式的C#应用​​程序。

my code is as follows, 我的代码如下

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.....
}

I am getting the following error in the highlighted line above as, 我在上面突出显示的行中收到以下错误,

Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. 无法嵌入互操作类型'Microsoft.Office.Interop.Excel.ApplicationClass'。 Use the applicable interface instead. 请改用适用的界面。

please help me out of this problem! 请帮我解决这个问题!

Use Excel.Application instead of Excel.ApplicationClass 使用Excel.Application而不是Excel.ApplicationClass

xlApp = new Excel.Application();

Update: You are referencing an interop assembly with "Embed Interop Types" set to true . 更新:您所引用的互操作程序集的“嵌入互操作类型”设置为true According to this post Class types contain metadata and code, while interfaces only contain metadata. 根据这篇文章, Class类型包含元数据代码,而接口仅包含元数据。 It is safe to embed metadata but not anything that can potentially contain executable code. 嵌入元数据是安全的,但不能嵌入任何可能包含可执行代码的内容。

Replace 更换

xlApp = new Excel.ApplicationClass();

with

xlApp = new Excel.Application();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM