使用 reader.GetName 和 OLEDB Excel 提供程序读取列名称问题

发布于 2024-09-13 04:10:35 字数 1457 浏览 2 评论 0原文

我在检索 Excel 工作表中的列名称时遇到问题。

我有一个 Excel 工作表,第一行只有 3 个单元格,其中包含以下 3 个值:

  • in A1: A

  • in B1: B

  • in C1: ABC

当我尝试执行我的方法时,标签显示:

  • A,B,A#B#C

而不是:

  • A,B,ABC

我的代码:

    protected void btnExecute_Click(object sender, EventArgs e)
    {
        string fullFileName = @"C:\TEST.xls";
        List<string> columns = new List<string>();

        string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", fullFileName);

        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            conn.Open();

            // Retrieves the first sheet
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string firstSheet = dt.Rows[0]["TABLE_NAME"].ToString();

            // Retrieves the list column name
            string query = string.Format("SELECT TOP 1 * FROM [{0}]", firstSheet);
            OleDbCommand cmd = new OleDbCommand(query, conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            for (int i = 0; i < reader.FieldCount; i++)
            {
                columns.Add(reader.GetName(i));
            }
        }

        lblCols.Text = string.Join(",", columns.ToArray());
    }

你有想法吗解决这个问题??

提前致谢。

丹尼尔

I have an issue to retrieve the columns names in an Excel sheet.

I have an Excel sheet with only 3 cells in the first row with these 3 values:

  • in A1: A

  • in B1: B

  • in C1: A.B.C

When I try to execute my method the label shows:

  • A,B,A#B#C

And not:

  • A,B,A.B.C

My Code:

    protected void btnExecute_Click(object sender, EventArgs e)
    {
        string fullFileName = @"C:\TEST.xls";
        List<string> columns = new List<string>();

        string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", fullFileName);

        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            conn.Open();

            // Retrieves the first sheet
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string firstSheet = dt.Rows[0]["TABLE_NAME"].ToString();

            // Retrieves the list column name
            string query = string.Format("SELECT TOP 1 * FROM [{0}]", firstSheet);
            OleDbCommand cmd = new OleDbCommand(query, conn);
            OleDbDataReader reader = cmd.ExecuteReader();
            for (int i = 0; i < reader.FieldCount; i++)
            {
                columns.Add(reader.GetName(i));
            }
        }

        lblCols.Text = string.Join(",", columns.ToArray());
    }

Do you have an idea to fix this issue??

Thanks in advance.

Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-09-20 04:10:35

试试这个我通过堆栈溢出发布的OleDBAdapter Excel QA

我刚刚对此进行了测试,它从您描述的示例 .xls 中获取了“ABC”。
即在底部添加以下内容以进行快速测试:

Object o = ds.Tables["xlsImport"].Rows[0]["LocationID"];
Object oa = ds.Tables["xlsImport"].Rows[0]["PartID"];
Object row0Col3 = ds.Tables["xlsImport"].Rows[0][2];

string valLocationID = o.ToString();
string valPartID = oa.ToString();
string rowZeroColumn3 = row0Col3.ToString();

Try this OleDBAdapter Excel QA I posted via stack overflow.

I just tested this and it picked up "A.B.C" from a sample .xls you described.
i.e. add the following to the bottom for a quick test:

Object o = ds.Tables["xlsImport"].Rows[0]["LocationID"];
Object oa = ds.Tables["xlsImport"].Rows[0]["PartID"];
Object row0Col3 = ds.Tables["xlsImport"].Rows[0][2];

string valLocationID = o.ToString();
string valPartID = oa.ToString();
string rowZeroColumn3 = row0Col3.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文