Mine是C#窗口应用程序。当我在本地机器中运行此应用程序时,它给出以下错误:-"初始化字符串的格式不符合从索引 128 开始的规范"。
try
{
string path = System.IO.Path.GetFullPath("E:\09-2013\SalesRep\Openleads.xlsx");
if (Path.GetExtension(path) == ".xls")
{
oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" + path + "Extended Properties="Excel 8.0;HDR=Yes;IMEX=2"");
}
else if (Path.GetExtension(path) == ".xlsx")
{
oledbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=" + path + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
}
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand(); ;
OleDbDataAdapter oleda = new OleDbDataAdapter();
DataSet ds = new DataSet();
cmd.Connection = oledbConn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [Raw Data$]";
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds, "dsSlno");
grvData.DataSource = ds.Tables["dsSlno"].DefaultView;
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds);
grvData.DataSource = ds.Tables[1].DefaultView;
}
catch (Exception ex)
{
}
finally
{
oledbConn.Close();
}
您的路径前面需要一个 @ 符号来转义反斜杠:
其中的"E:"后面有一个以 null 结尾的字符 (\0),导致它在连接到连接字符串时出现问题。
更改此设置:
string path = System.IO.Path.GetFullPath("E: 9-2013SalesRepOpenleads.xlsx");
对此:
string path = System.IO.Path.GetFullPath(@"E: 9-2013SalesRepOpenleads.xlsx");
你应该没事。