如何处理OleDb Excel导入中的无效字符?

我正在将excel文件导入到我的应用程序中,有时工作表的列名也有“$”符号。 我收到这个例外:

System.Data.OleDb.OleDbException was unhandled Message=''6um$'$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long. 

在这张表中,“6um $”是一个列名。

这是我的代码:

 OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString); OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter( "select * from [" + worksheetName + "$]", con); con.Open(); System.Data.DataSet excelDataSet = new DataSet(); cmd.Fill(excelDataSet); con.Close(); 

任何想法如何处理这种情况?

编辑:

我认为这个问题是在$名列。 但事实certificate,问题是有$签名工作表名称!

好吧,我想出了解决scheme:出于某种原因,我重命名我自己的工作表有额外的$符号(不知道为什么,没有$符号在Excel中,但OLEDB返回他们额外的$这样的东西'6um $'$ “)。 我修剪第一个$ off并使用这个代码来检查是否还有额外的$符号:

 char delimiterChars = '$'; string[] words = worksheetName.Split(delimiterChars); worksheetName=words[0]; 
 if (Directory.Exists(serverPath)) { string FileExist = sp + "book1.xlsx"; ; string Exten = Path.GetExtension(FileExist); string g = "sheet1"; if (File.Exists(FileExist)) { if (Exten == ".xlsx") { string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileExist + ";Extended Properties=Excel 12.0"; OleDbConnection oledbConn = new OleDbConnection(connString); try { // Open connection oledbConn.Open(); string query = String.Format("select * from [{0}$]", g); // Create OleDbCommand object and select data from worksheet Sheet1 OleDbCommand cmd = new OleDbCommand(query, oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; // Create a DataSet which will hold the data extracted from the worksheet. DataSet ds = new DataSet(); // Fill the DataSet from the data extracted from the worksheet. oleda.Fill(ds, "sheetdt"); GridView1.DataSource = ds.Tables[0].DefaultView; GridView1.DataBind(); } catch (Exception ex) { LblSuccess.Text = ex.Message; } finally { // Close connection oledbConn.Close(); } } } }