当我尝试使用OLEDB从Excel中选择所有数据时。我收到错误
查询表达式“创建者”中的语法错误(缺少运算符)这是因为列名中有空格吗?
查询为:
选择代码,名称,创建者,日期从 [模板$]
public DataTable GetExcelDataToTable(string filename, string dataExchangeSelectedColum)
{
//List<DataExchangeDefinition> dataExchange = new List<DataExchangeDefinition>();
string extension = Path.GetExtension(filename);
string connstring = string.Empty;
DataTable ExcelData = null;
try
{
switch (extension)
{
case ".xls":
connstring = string.Format(ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString, filename);
break;
case ".xlsx":
connstring = string.Format(ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString, filename);
break;
}
using (OleDbConnection connExcel = new OleDbConnection(connstring))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = connExcel;
connExcel.Open();
var dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
connExcel.Close();
var firstSheet = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
cmd.CommandText = "SELECT " + dataExchangeSelectedColum + " FROM [" + firstSheet + "]";
ExcelData = new DataTable();
OleDbDataAdapter oda = new OleDbDataAdapter();
oda.SelectCommand = cmd;
oda.Fill(ExcelData);
}
}
}
catch (Exception ex)
{
throw ex;
}
return ExcelData;
}
这是我尝试的代码,这里包含它们是“代码,名称,创建者,日期”的列dataExchangeSelectedColum
吃鸡游戏
慕哥6287543
相关分类