我正在使用下面的代码从 excel 表中读取数据并将数据添加到数据表中,但是在读取文件并检查数据表中的数据时,一些数据丢失了。我一直在看这个和不知道为什么会这样。
excelConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
" ;Extended Properties=Excel 12.0";
try
{
// Create Connection to Excel Workbook
using (OleDbConnection connection =
new OleDbConnection(excelConnectionString))
{
connection.Open();
System.Data.DataTable dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] excelSheet = new String[dt.Rows.Count];
int sheet = 0;
foreach (DataRow row in dt.Rows)
{
excelSheet[sheet] = row["Table_Name"].ToString();
sheet++;
}
for (int i = 0; i < excelSheet.Length; i++)
{
OleDbCommand command = new OleDbCommand
("Select * FROM [" + excelSheet[i] + "]", connection);
adapter.SelectCommand = command;
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}