从 excel ASP.Net 获取工作表名称
Get sheets names from excel ASP.Net
我正在获取按下一个代码的字母顺序排序的工作表名称。
OleDbConnection connection = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=\"Excel 8.0;HDR=No;\"", filePath));
OleDbCommand command = new OleDbCommand();
DataTable tableOfData = null;
command.Connection = connection;
try
{
connection.Open();
tableOfData = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string tablename = tableOfData.Rows[0]["TABLE_NAME"].ToString();
tableOfData = new DataTable();
command.CommandText = "Select * FROM [" + tablename + "]";
tableOfData.Load(command.ExecuteReader());
}
catch (Exception ex)
{
}
我只想按原顺序阅读。有什么想法吗?
经过多次搜索我发现 "OLEDB" 不支持按原始顺序阅读表格(仅按字母顺序阅读表格名称)但是有很多方法支持按原始顺序阅读表格名称 excel 文件。
帮助 link: Reading sheets in original order from excel file,
OLEDB does not preserve the sheet order as they were in Excel.
我正在获取按下一个代码的字母顺序排序的工作表名称。
OleDbConnection connection = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=\"Excel 8.0;HDR=No;\"", filePath));
OleDbCommand command = new OleDbCommand();
DataTable tableOfData = null;
command.Connection = connection;
try
{
connection.Open();
tableOfData = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string tablename = tableOfData.Rows[0]["TABLE_NAME"].ToString();
tableOfData = new DataTable();
command.CommandText = "Select * FROM [" + tablename + "]";
tableOfData.Load(command.ExecuteReader());
}
catch (Exception ex)
{
}
我只想按原顺序阅读。有什么想法吗?
经过多次搜索我发现 "OLEDB" 不支持按原始顺序阅读表格(仅按字母顺序阅读表格名称)但是有很多方法支持按原始顺序阅读表格名称 excel 文件。
帮助 link: Reading sheets in original order from excel file,
OLEDB does not preserve the sheet order as they were in Excel.