[英]Need a solution to Retrieve data from Excel in C# without HRESULT:0x800A03EC exception
I need to retrieve data from an Excel file and insert the data into a database. 我需要从Excel文件中检索数据并将数据插入数据库中。 Im trying now to just retrieve the data but i keep getting an HRESULT:0x800A03EC exception error.
我现在正试图只检索数据,但我一直收到HRESULT:0x800A03EC异常错误。
my code: 我的代码:
public void ReadFile()
{
try
{
Excel.Application ep = new Excel.Application();
Excel.Workbook ewb = ep.Workbooks.Open(@"C:/Temp/Copy of AGCO Transport Schedule.xlsx");
Excel.Worksheet ews = ewb.Sheets[1];
Excel.Range range = ews.UsedRange;
int rowCount = range.Rows.Count;
int columnCount = range.Columns.Count;
for (int i = 1; i < rowCount; i++)
{
for (int j = 1; j < columnCount; j++)
{
string str = (string)(range.Cells[i, j] as Excel.Range).Value2;
Console.WriteLine(str);
}
}
}
catch (Exception e)
{
Console.WriteLine("YOLO "+e.Message);
}
}
}
After getting the solution for the code above from a user here. 从此处的用户那里获得上述代码的解决方案之后。 The loop indexes should be changed to 1 instead of 0. I have corrected the code above.
循环索引应更改为1而不是0。我已经更正了上面的代码。 But the thing is that I need to select only 2 columns from the data in Excel, so i went on using sql for retrieving the data from the Excel file.
但问题是,我只需要从Excel中的数据中选择2列,所以我继续使用sql从Excel文件中检索数据。 But this time i keep getting "failed to create file" error and this happens when i try to open my connection.
但是这一次我一直收到“无法创建文件”错误,当我尝试打开连接时会发生这种情况。
my code: 我的代码:
public void ReadExcelFile()
{
string connectionString = string.Format(ConfigurationManager.ConnectionStrings["ExcelConnection"].ConnectionString.Replace("'", "\""), "@C:/Temp/Copy.xlsx");
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
try
{
connection.Open();
string sqlCmd = "SELECT * FROM [Ark1$]";
using (OleDbCommand command = new OleDbCommand(sqlCmd, connection))
{
command.CommandType = System.Data.CommandType.Text;
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader.ToString());
}
}
}
}
catch (Exception exception)
{
Console.WriteLine("ERROR in ReadExcelFile() method. Error Message : " + exception.Message);
}
}
}
can anyone help me with this ? 谁能帮我这个 ?
Try by starting loop counting from 1 on both for loops. 尝试从两个for循环的1开始计数。 Index for excel file is starting from 1. I'ts not zero based index i suppose.
excel文件的索引从1开始。我想不是基于零的索引。
Something i would like to add: Your method is SLOW - really, really slow. 我想补充一点:您的方法很慢-真的,真的很慢。 To speed it up, you have two alternatives: Use EPPLUS, it is a fast and free library for excel reading/writing.
为了加快速度,您有两种选择:使用EPPLUS,它是用于Excel读写的快速免费库。 Oder get the data from excel in one go, simply "pull" the data from your range object into an array (dim values() = range.value2 in VB.net) and iterate over that array.
奥德(Oder)一口气从excel中获取数据,只需将范围对象中的数据“拉”入一个数组(VB.net中的dim values()= range.value2)并遍历该数组。 That is a lot faster than enumerating each cell.
这比枚举每个单元格要快得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.