读取Excel文件在C#中列名 [英] read column names from excel file in c#

查看:975
本文介绍了读取Excel文件在C#中列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现其中允许用户上传包含表格数据文件的文件上传功能。在上传文件我想找到表的列名。 ?我怎样才能做到这一点。

I have to implement a file upload feature in which users are allowed to upload files containing tabular data. On uploading the file I want to find the column names of table. How can I do this?

推荐答案

@vc 74我想在代码中指出的一些错误:结果
具有 sheetColumns.Rows 相反的,应该有 sheetColumns.Columns ,因为它已经被引用到DataColumn的类型。结果
要阅读所有现有的Excel文件中的特定工作表的列名的DataRow应参考如下:

@vc 74 I would like to point out some mistake in code:
Instead of having sheetColumns.Rows, there should be sheetColumns.Columns as it was already referencing to DataColumn type.
To read all the column names existing in particular sheet of excel file, DataRow should be referenced as below:

打开连接后,代码去像这样的:

After opening the connection, code goes like this:

 DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[]
{ null,null, sheetName, null });

 List<string> listColumn = new List<string>();
 foreach (DataRow row in dt.Rows)
 {
      listColumn.Add(row["Column_name"].ToString());
 }



listColumn包含现有指定表的列名。

listColumn contains the column names existing in the specified sheet.

这篇关于读取Excel文件在C#中列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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