使用GetOleDbSchemaTable读取Excel文件中的工作表顺序 [英] Order of sheets in Excel file while reading with GetOleDbSchemaTable

查看:911
本文介绍了使用GetOleDbSchemaTable读取Excel文件中的工作表顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用如下的ACE.OLEDB来读取C#中的excel文件。
我的Excel文件有以下表格,按以下顺序:

I am using an ACE.OLEDB as below to read an excel file in C#. My Excel file has the following Sheets and in below order:

Sheet1,Sheet1(2),Sheet2

Sheet1, Sheet1 (2), Sheet2

我的代码中的变量sheetName将'Sheet1(2)'作为第一页而不是'Sheet1'。

The variable sheetName in my code takes 'Sheet1 (2)' as first sheet instead of 'Sheet1'.

我的问题是如何确定单的顺序?

My question is how does it determines the order of sheets?

string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
// Extra blank space cannot appear in Office 2007 and the last version. And we need to pay attention on semicolon.


using (OleDbConnection conn = new OleDbConnection(connstring))
{
    conn.Open();
    System.Data.DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] {null, null, null, "Table"});
    string firstSheetName = sheetsName.Rows[0][2].ToString();
    string sql = string.Format("select * from [{0}]", firstSheetName);
    OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
    DataSet set = new DataSet();
    ada.Fill(set);


推荐答案

根据 / a>,主持人说我恐怕OLEDB不像Excel那样保留表单顺序。

虽然Google搜索,但我发现这个SO答案可能会帮助你。

While Googling though, I found this SO answer that might help you out.

这篇关于使用GetOleDbSchemaTable读取Excel文件中的工作表顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆