在 c# .net 4.0 中使用 XLS 的主要问题
我有一个 xls 文件,我想使用 c# 读取并填充数据表中的信息。我使用的代码是:
public static DataTable GetExcelData(string excelFilePath)
{
OleDbConnection objConn = null;
string oledbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 10.0;";
objConn = new OleDbConnection(oledbConnectionString);
if (objConn.State == ConnectionState.Closed)
{
objConn.Open();
}
var objCmdSelect = new OleDbCommand("Select * from [Sheet1$]", objConn);
var objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
var objDataset = new DataSet();
objAdapter.Fill(objDataset, "ExcelDataTable");
objConn.Close();
return objDataset.Tables[0];
}
填充此数据表后,我需要删除包含标题信息的前 5 行左右,并循环遍历填充 Access 数据库表的数据表。我对这个或其他建议的 10,000 种方法都没有运气。有谁有任何信息可以帮助我。我正在运行 VS2010 .net 4.0 框架。任何和所有的帮助将不胜感激。
谢谢, 约翰
I have an xls file i would like to read using c# and populate the information in a data table. The code I am using is :
public static DataTable GetExcelData(string excelFilePath)
{
OleDbConnection objConn = null;
string oledbConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 10.0;";
objConn = new OleDbConnection(oledbConnectionString);
if (objConn.State == ConnectionState.Closed)
{
objConn.Open();
}
var objCmdSelect = new OleDbCommand("Select * from [Sheet1$]", objConn);
var objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
var objDataset = new DataSet();
objAdapter.Fill(objDataset, "ExcelDataTable");
objConn.Close();
return objDataset.Tables[0];
}
Once this data table is populated, I need to remove the first 5 or so rows which contain header information, and loop through the data table populating an access database table. I have had no luck with this or any of the other 10,000 ways suggested. Does anyone have any information that can help me. I am running VS2010 .net 4.0 framework. Any and all help would be super appreciated.
Thanks,
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在尝试使用 OLEDB 将 Excel 数据放入 DataTable 时遇到了很大的麻烦。我最终通过切换到使用 Excel Interop 的解决方案解决了该问题。请参阅此答案以获取进一步的说明和示例代码:
从 Excel 导入:一些单元格变为空
I've had a great deal of trouble trying to get Excel data into a DataTable using OLEDB. I finally solved the issue by switching to a solution that uses Excel Interop. Please see this answer for further explanation and sample code:
Importing from Excel: some cells become null