使用Microsoft.ACE.OLEDB.12.0加载到数据集时,Excel数据列名称发生更改

我有一个18列的Excel表。 在这5列中有相同的列名CALL_REASON 。 但是,当我加载到Excel数据集使用Microsoft.ACE.OLEDB.12.0。 数据集中的列名更改为具有相同名称的列。 它正在加载为CALL_REASON,CALL_REASON1,CALL_REASON2,CALL_REASON3,CALL_REASON4 。 它们出现在Excel CALL_REASON,CALL_REASON,CALL_REASON,CALL_REASON,CALL_REASONCALL_REASON,CALL_REASON,CALL_REASON,CALL_REASON,CALL_REASON

 String properties = "Excel 8.0; HDR=YES; IMEX=1;";//properties set for connection to excel string sSourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\uploads\" + fileName + ";Extended Properties=\"" + properties + "\""; sSourceConnection = new OleDbConnection(sSourceConstr);//creating the OLEDB connection try { //select statement to select data from the first excel sheet string sql = string.Format("Select * FROM [{0}]", "Sheet1$"); //commands to fill the dataset with excel data OleDbDataAdapter excelAdapter = new OleDbDataAdapter(); OleDbCommand command = new OleDbCommand(sql, sSourceConnection); sSourceConnection.Open(); excelAdapter.SelectCommand = command; excelAdapter.Fill(surveyItemDetails, "ExcelDataTable"); } 

数据表中不能有相同名称的字段。 实际上,甚至不能使用CALL_REASONCALL_reason ,例如,请参阅DataColumnCollection.Add文档中的“ Exceptions部分:

DuplicateNameException

该集合已经有一个具有指定名称的列。 (比较不区分大小写。)

列名必须是唯一的要求是非常有意义的。 DataRow.Field<String>("CALL_REASON")应该返回什么,如果你被允许有多个这样的名字的列?

访问可能会更改您的列名称。 你可以在数据库中有相同名称的字段吗? 我不这么认为。 当你做一个select,除非它是*,那么如果有很多同名的字段,你将如何select调用原因。试验用excel将带有列的数据导出到一个CSV文件中。 然后尝试将CS​​V文件导入到Access。 有一个向导可以逐步询问您是否要保留列名称,select一个字段作为键或让Access为您创build一个。 追加或创build表等…访问将告诉你,如果它不能使用列名称,并为您创build一个。