【发布时间】:2012-07-31 01:12:25
【问题描述】:
我有一个 18 列的 Excel 表。其中 5 列具有相同的列名 CALL_REASON。但是当我使用 Microsoft.ACE.OLEDB.12.0 将该 Excel 表加载到数据集中时。对于具有相同名称的列,数据集中的列名称会更改。它被加载为CALL_REASON,CALL_REASON1,CALL_REASON2,CALL_REASON3,CALL_REASON4。它们在 excelsheet 中显示为 CALL_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");
}
【问题讨论】:
-
你能给我们看看代码吗,包括 SQL 查询?直觉是
bydesign。
标签: c# excel ado.net excel-2007 import-from-excel