如何在使用C#中的oledb读取.xlsx格式的excel文件时解决错误或无响应? [英] How to resolve the error or no response while reading from excel file in .xlsx format using oledb in C#?

查看:140
本文介绍了如何在使用C#中的oledb读取.xlsx格式的excel文件时解决错误或无响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用C#中的Oledb读取.xlsx格式的excel文件时解决错误或无响应?



我正在通过Oledb从Excel读取数据连接。我收到错误试图读取写保护的内存。这通常表明其他内存已损坏。或者对于使用.xlsx格式的excel文件将数据填充到数据表时没有响应。否则使用.xls格式化它工作正常。



我的代码如下:

How to resolve the error or no response while reading from excel file in .xlsx format using Oledb in c#?

I am reading the data from Excel via Oledb connection. I am getting the error "attempted to read write protected memory. this is often an indication that other memory is corrupt"." or with no response on filling data to data table for excel files with .xlsx format. Else with .xls format it works fine.

My code is as below:

if (fileExtension == ".xls" || fileExtension == ".XLS")
              {
                  connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

              }
              else if (fileExtension == ".xlsx" || fileExtension == ".XLSX")
              {
                  connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
              }


              DataTable dt = new DataTable();


              System.Data.OleDb.OleDbConnection MyConnection;
              System.Data.DataSet ds;
              System.Data.OleDb.OleDbDataAdapter MyCommand;
              MyConnection = new System.Data.OleDb.OleDbConnection(connStr);
              MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
              ds = new System.Data.DataSet();
              MyCommand.Fill(dt);
              MyConnection.Close();





我尝试过:



我的代码如下:

if(fileExtension ==。xls|| fileExtension ==。XML)

{

connStr =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ filelocation +; Extended Properties = \Excel 8.0; HDR = Yes; IMEX = 2 \\ \\;



}

else if(fileExtension ==。xlsx|| fileExtension ==。xLSX)

{

connStr =Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ filelocation +;扩展属性= \Excel 12.0; HDR =是; IMEX = 2 \;

}





DataTable dt = new DataTable();





System.Data.OleDb.OleDbConnection MyConnection;

System.Data.DataSet ds;

System.Data.OleDb.OleDbDataAdapter MyCommand;

MyConnection = new System.Data.OleDb.OleDbConnection(connStr);

MyCommand = new System.Data.OleDb.OleDbDataAdapter(select * from [Sheet1 $],MyConnection);

ds = new System.Data.DataSet();

MyCommand.Fill(dt);

MyConnection.Close();



What I have tried:

My code is as below:
if (fileExtension == ".xls" || fileExtension == ".XLS")
{
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

}
else if (fileExtension == ".xlsx" || fileExtension == ".XLSX")
{
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}


DataTable dt = new DataTable();


System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet ds;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(connStr);
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(dt);
MyConnection.Close();

推荐答案

,MyConnection );
ds = new System.Data.DataSet();
MyCommand.Fill(dt);
MyConnection.Close();
", MyConnection); ds = new System.Data.DataSet(); MyCommand.Fill(dt); MyConnection.Close();





我尝试过:



我的代码如下:

if(fileExtension ==。xls|| fileExtension ==。XML)

{

connStr =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ filelocation +; Extended Properties = \Excel 8.0; HDR = Yes; IMEX = 2 \\ \\;



}

else if(fileExtension ==。xlsx|| fileExtension ==。xLSX)

{

connStr =Provider = Microsoft.ACE.OLEDB.12.0; Data Source =+ filelocation +;扩展属性= \Excel 12.0; HDR =是; IMEX = 2 \;

}





DataTable dt = new DataTable();





System.Data.OleDb.OleDbConnection MyConnection;

System.Data.DataSet ds;

System.Data.OleDb.OleDbDataAdapter MyCommand;

MyConnection = new System.Data.OleDb.OleDbConnection(connStr);

MyCommand = new System.Data.OleDb.OleDbDataAdapter(select * from [Sheet1



What I have tried:

My code is as below:
if (fileExtension == ".xls" || fileExtension == ".XLS")
{
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

}
else if (fileExtension == ".xlsx" || fileExtension == ".XLSX")
{
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}


DataTable dt = new DataTable();


System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet ds;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(connStr);
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1


,MyConnection);

ds = new System。 Data.DataSet();

MyCommand.Fill(dt);

MyConnection.Close();
", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(dt);
MyConnection.Close();


使用以下连接在XLXS的情况下是字符串。



Use the following connection string in case of XLXS as it is.

connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties =\"Excel 12.0 Xml;HDR=YES\";";





请记住只需复制并粘贴。



Remember just copy and paste.


这篇关于如何在使用C#中的oledb读取.xlsx格式的excel文件时解决错误或无响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆