我的最终目标是从 .xlsx 文件中检索数据并将它们加载到 .mdb (Microsoft Access) 文件中。我的代码现在是这样的:
String fileName = "1.xlsx";
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"");
conn.Open();
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string cmdText = "SELECT * FROM [sheet1$A3:B3]";
DataSet ds;
using (OleDbCommand cmd = new OleDbCommand(cmdText))
{
cmd.Connection = conn;
OleDbDataAdapter adpt = new OleDbDataAdapter(cmd);
ds = new DataSet();
adpt.Fill(ds, "sheet1");
}
但这是我知道工作表中有多少行的时候。如果我不知道这些信息怎么办?我能以某种方式设法知道工作表中有多少行吗?