如何读取excel文件的列名 [英] how to read column names of a excel file

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

问题描述

hiiii ....我想读取excel文件的列名并在下拉列表中显示它们。那该怎么办?

请帮助我。

hiiii.... i want to read column names of excel file and display them in a dropdown list. for that what to do?
pls help me.

推荐答案

参见使用OLEDB读取和写入Excel [ ^ ]。


string strFilePath = string.Empty;
string ConnString = string.Empty;
string strCols = string.Empty;
string strRowVal = string.Empty;	   
strFilePath = Server.MapPath("~/upload/csvfiles" );
if (strFileType.ToString().Trim() == ".csv")
{
	ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";Extended Properties=Text;";
}
Conn = new OleDbConnection(ConnString);
Conn.Open();

string tablename = "";
if (strFile.IndexOf(''/'') > 0)
{
	tablename = strFile.Substring(strFile.LastIndexOf("/") + 1);
}
else
{
	Int32 intLastIndex = 0;
	intLastIndex = strFile.LastIndexOf("\\");
	tablename = strFile.Substring(intLastIndex + 1);
}
DataTable dt = new DataTable();
dt = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);



OleDbCommand ocmd = new OleDbCommand("SELECT * FROM [" + tablename + "]", Conn);

oda = new OleDbDataAdapter(ocmd);
DataSet dsIn = new DataSet();
oda.Fill(dsIn);
Conn.Close();
foreach (DataColumn DtColumn in dsIn.Tables[0].Columns)
{
	strCols = strCols + DtColumn.ColumnName.Trim() + ":";
}

strCols = strCols.Substring(0, strCols.Length - 1);
strColArr = strCols.Split('':'');


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

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