阅读Excel文件问题

时间:2018-02-04 05:57:29

标签: c#

我正面临着一个问题,即使用c#以编程方式读取excel文件(.xls).Issue是读取过程中标题行[第二行]下方的行。我使用下面的代码来读取文件。有谁能告诉我如何解决这个问题。

string myexceldataquery = "select [Business ID],[Customer Name] from [Sheet1$]";   
string sexcelconnectionstring = @"provider=microsoft.jet.oledb.4.0;data source=" + path + ";extended properties=" + "\"excel 8.0;HDR=Yes;IMEX=1;\"";
string ssqlconnectionstring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

string sclearsql = "truncate table " + sqltable;
SqlConnection sqlconn = new SqlConnection(ssqlconnectionstring);
SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();

OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
oledbconn.Open();
OleDbDataReader dr = oledbcmd.ExecuteReader();
SqlBulkCopy bulkcopy = new SqlBulkCopy(ssqlconnectionstring);
bulkcopy.DestinationTableName = sqltable;
while (dr.Read())
{
    bulkcopy.WriteToServer(dr);
}
dr.Close();
oledbconn.Close();

0 个答案:

没有答案