问题
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