我已经编写了下面的代码来将数据从 excel 文件写入数据表,但是由于某种原因,在写入数据表时,索引 0 和 1 处的行的数据不显示。有没有人知道为什么会这样..
var excelDataTable = new DataTable();
var excelAdapter = new OleDbDataAdapter();
var excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + excelFileName + ";Extended Properties=Excel 12.0;";
// Create Connection to Excel Workbook
using (var excelConnection = new OleDbConnection(excelConnectionString))
{
excelConnection.Open();
var dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt != null)
{
var excelSheet = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
excelSheet[i] = row["Table_Name"].ToString();
i++;
}
var command = new OleDbCommand
("Select * FROM [" + excelSheet[0] + "]", excelConnection); // should be first sheet not the name of the sheet, should be index
excelAdapter.SelectCommand = command;
}
excelAdapter.Fill(excelDataTable);
excelConnection.Close();
}