How to get sheet name from Excel

半城伤御伤魂 提交于 2019-12-23 12:56:12

问题


How do I get sheet name from Excel and add them to my comboBox list? I can't seem to add it in my code as it is in public static

public static DataTable ExcelToDataTable (string fileName)
{
    using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
    {
        using (var reader = ExcelReaderFactory.CreateReader(stream))
        {
            var result = reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                UseColumnDataType = true,
                ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = true
                }
            });
            DataTableCollection table = result.Tables;
            DataTable resultTable = table["Sheet1"];                 
            return resultTable;
        }
    }
}

回答1:


If I understand what you want! you can use this code:

var sheetNames = result.Tables
    .OfType<DataTable>()
    .Select(c => c.TableName)
    .ToArray();



回答2:


You should use the TableName property

result.Tables[0].TableName

Tables is a collection with all your sheets so you can do a loop and pick all sheets names by index



来源:https://stackoverflow.com/questions/48286317/how-to-get-sheet-name-from-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!