Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用.NET Core 2.1在C#中创建.xlsx文件?_C#_.net_Excel Interop - Fatal编程技术网

如何使用.NET Core 2.1在C#中创建.xlsx文件?

如何使用.NET Core 2.1在C#中创建.xlsx文件?,c#,.net,excel-interop,C#,.net,Excel Interop,我从网上获取了以下代码,成功地使用.NET Framework 4.6.1编写了以下代码: using System; using Excel = Microsoft.Office.Interop.Excel; class E { public static void Main(string[] args) { try { Excel.Application excel; Excel.Workb

我从网上获取了以下代码,成功地使用.NET Framework 4.6.1编写了以下代码:

using System;
using Excel = Microsoft.Office.Interop.Excel;

class E
{
    public static void Main(string[] args)
    {

        try
        {

            Excel.Application excel;
            Excel.Workbook workbook;
            Excel.Worksheet sheet;

            excel = new Excel.Application();
            excel.Visible = true;

            workbook = excel.Workbooks.Add();
            sheet = (Excel.Worksheet)workbook.ActiveSheet;

            sheet.Cells[1, 1] = "Number";
            sheet.Cells[1, 2] = "English";
            sheet.Cells[1, 3] = "German";
            sheet.Cells[1, 4] = "Formula";
        }
        catch (Exception e)
        {
            Console.WriteLine($"Error: {e.Message} Line: {e.Source}");
        }
    }
}
因此,当我使用.NETCore2.1执行它时,它会打开Excel,但不会写入单元格。这些表格不是只读的,我检查过了


谢谢你的帮助

它在哪个操作系统上运行?Microsoft.Office.Interop.Excel仅在Windows上兼容,并且计算机需要安装Excel。如果我看到相同的问题,请升级到.NET Core 3.0以解决此问题。但我不知道为什么它在.NET Core 2.1中不起作用。对不起。为什么不调用save方法?也许Kenan不想保存电子表格。。。。不需要保存。@WilliamW是的,安装了Excel的Windows。