如何读取excel指定的列?
现在想读取excel,要在每个excel文件插入第一行,然后依次写上A、B、C、D....
command.CommandText = @ "SELECT A,C FROM [shee1$] ";
这样就读取了A,C两列。但每个excel文件都要人工插入一行很麻烦,如何不用插入而读取A,C两列呢?
------解决方案--------------------读出到 table中,列名应该是F1,F2,Fn 这样的排序
------解决方案--------------------command.CommandText = @ "SELECT * FROM [shee1$] ";
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
string a = System.Convert.ToString(dr[0]); //A字段所顺序号,从0 开始
string c = System.Convert.ToString(dr[2]); //C字段的顺序号,从0 开始
}
------解决方案--------------------//开始导入
//建立EXCEL的连接
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
string sqlStr = "select * from [Sheet1$] ";
OleDbCommand myCmd = new OleDbCommand(sqlStr, objConn);
OleDbDataAdapter myda = new OleDbDataAdapter(myCmd);
DataSet myDs = new DataSet();
myda.Fill(myDs, "Temp ");
DataGrid1.DataSource = myDs;
先打开EXCEL文件,放到myDs里,然后用myDs.Tables[0].Rows[i].ItemArray[i].ToString()
第1行第1列就是myDs.Tables[0].Rows[0].ItemArray[0].ToString()