目前,我正试图从存储在服务器机器上的excel文件中检索数据。这是密码。
public void ImportDataFromExcel(string excelPath)
{
string connString ="server=D0123;uid=abc;pwd=abc@123;database=MYDB";
string SqlTable = "xx_fields";
string excelQuery = "Select FieldName,FieldType,Length,Decimal from [Sheet1$]";
try {
string excelConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+excelPath+";Extended Properties="+"\"Excel 12.0;HDR=Yes;IMEX=1\"";
OleDbConnection oleDbconn = new OleDbConnection(excelConnStr);
oleDbconn.Open();
OleDbCommand oleDbcmd = new OleDbCommand(excelQuery,oleDbconn);
OleDbDataReader dr = oleDbcmd.ExecuteReader();
SqlBulkCopy bulkCopy = new SqlBulkCopy(connString);
bulkCopy.DestinationTableName = SqlTable;
while(dr.Read()) {
bulkCopy.WriteToServer(dr);
}
oleDbconn.Close();
} catch(Exception e) {
Response.Write("<script>alert('Error : '" + e.Message + ");</script>");
}
}但是,每当我试图打开excel连接字符串时,我都会收到一个错误:"Expected )“。我试过换衣服:
string excelConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+excelPath+";Extended Properties="+"\"Excel 12.0;HDR=Yes;IMEX=1\"";至
string excelConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+excelPath+";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1""";我的Excel文件的格式如下:
FieldName FieldType Length Decimal
Name String 10
Date Numeric 8 0
Address String 40
Age Numeric 2 0
Price Numeric 8 2我还是会犯同样的错误。怎么回事?在最后一个注意事项中,此C#代码运行在私有引擎上,因此不能使用任何或其他库,如。
发布于 2014-01-02 02:54:38
连接字符串应该是
string excelConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
excelPath + ";Extended Properties=" + "\"Excel 12.0;HDR=Yes;IMEX=1\"";更新:
http://www.connectionstrings.com/ace-oledb-12-0/展示了这一点
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";您的excelPath应该是服务器上的物理路径。您可以使用Server.MapPath()获取物理路径。但是,对于这个函数,excelPath是参数,所以在调用该函数之前,需要确保excelPath。
https://stackoverflow.com/questions/20875495
复制相似问题