Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.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
如何在c#控制台中读取excel文件?_C#_Excel_Console - Fatal编程技术网

如何在c#控制台中读取excel文件?

如何在c#控制台中读取excel文件?,c#,excel,console,C#,Excel,Console,如何在C#中打开excel文件并逐个单元格读取,而不是在Windows窗体中打开控制台 using System; namespace nms { class read_from_excel_file { static void Main(string[] args) { //open excel file //print(cell[1,1]) } } } 您可以添加Nuget包“Microsoft.Office.I

如何在C#中打开excel文件并逐个单元格读取,而不是在Windows窗体中打开控制台


using System;

namespace nms
{
  class read_from_excel_file
  {
    static void Main(string[] args)
    {
         //open excel file 
        //print(cell[1,1])
    }
  }
}

您可以添加Nuget包“Microsoft.Office.Interop.Excel”来实现它

这是一个演示

string strFileName = @"D:\test.xls";
object missing = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
Excel.Workbook workBook = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];

// print cell[1,1]
Console.WriteLine(worksheet.Cells[1, 1].Value);

项目类型(控制台、Windows窗体、库等)与与MS Office应用程序的互操作方式无关。请继续阅读,从那里开始,然后回到这里,在遇到问题时提出更集中的问题。我的建议是根本不要使用互操作。您可以使用开源库作为(开源版本),或者使用OleDb提供程序作为数据表查询Excel工作表。Office Interop需要Office安装,而其他选项则不需要。