【问题标题】:how read excel file in c# console? [closed]如何在 C# 控制台中读取 excel 文件? [关闭]
【发布时间】:2020-10-16 07:13:51
【问题描述】:

我如何在 C# 中打开一个 excel 文件并逐个单元格读取,但只是在控制台中而不是在 windowsForm 中?


using System;

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

【问题讨论】:

  • 项目类型(控制台、Windows 窗体、库等)与您与 MS Office 应用程序互操作的方式无关。阅读basics,从那里开始工作,当你遇到困难时,回到这里提出更集中的问题。
  • 我的建议是完全不要使用 Interop。您可以将开源库用作EPPlus(开源版本)、SpreadsheetLightExcelDataReader,或者使用 OleDb 提供程序将 Excel 工作表作为数据表进行查询。 Office Interop 需要 Office 安装,其他选项不需要。

标签: c# excel console


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2023-04-03
    • 1970-01-01
    • 2012-11-26
    • 2015-11-29
    相关资源
    最近更新 更多