首页 > 解决方案 > 从所有 Excel 工作表中创建一个数组

问题描述

我有一个通过 OpenFileDialog 函数连接到 Excel 数据库的 C# 代码。Excel 文件有太多工作表,我想将它们的所有名称放在一个数组中并将它们添加到组合框。例如,这是第一个 Excel 文档。 图片

有什么建议么?

标签: c#sqlexcel

解决方案


您可以尝试以下方法:

Accord.IO.ExcelReader reader = new Accord.IO.ExcelReader("youFile.xlsx");
string[] worksheets = reader.GetWorksheetList();

确保包括

using Accord.IO;

您可以通过搜索 Accord 和 Accord.IO 从 NuGet 获取 Accord。

然后只需填写组合框的项目

foreach (string worksheet in worksheets)
{
    myComboBox.Items.Add(worksheet);
}

推荐阅读