如何使用oledb将数据表导出到C#中? [英] How to export data table to excel in C# using oledb?

查看:93
本文介绍了如何使用oledb将数据表导出到C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Oledb将数据表导出到c#中excel?

 尝试 
{
string filelocation = @ E:\ Alex \\Arab_Test_Oledb.xls;
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null ;
// string connStr =Provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ filelocation +;扩展属性= \Excel 8.0; HDR =是; IMEX = 2 \;
MyConnection = 系统.Data.OleDb.OleDbConnection( provider = Microsoft.Jet.OLEDB.4.0; Data Source = + filelocation + ;扩展属性= Excel 8.0;);

MyConnection.Open();

myCommand.Connection = MyConnection;

sql = 插入[Sheet1 $](EMPLOYEE_ID,ADDRESS1)值('5 , 'E');

myCommand.CommandText = sql;

myCommand.ExecuteNonQuery();

MyConnection.Close();

}
catch (例外情况)
{

}





我的尝试:



我的代码是如下:

尝试

{

string filelocation = @E:\ Alexh \ Arab_Test_Oledb.xls;

System.Data.OleDb.OleDbConnection MyConnection;

System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();

string sql = null;

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

MyConnection = new System.Data.OleDb.OleDbConnection(provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ filelocation +; Extended Properties = Excel 8.0;);



MyConnection.Open();



myCommand.Connection = MyConnection;



sql =插入[ Sheet1 $](EMPLOYEE_ID,ADDRESS1)值('5','e');



myCommand.CommandText = sql;



myCommand.ExecuteNonQuery();



MyConnection.Close();



}

catch(例外情况)

{



}

解决方案

(EMPLOYEE_ID,ADDRESS1)值('5','e');

myCommand.CommandText = sql;

myCommand.ExecuteNonQuery();

MyConnection.Close();

}
catch (例外情况)
{

}





我的尝试:



我的代码是如下:

尝试

{

string filelocation = @E:\ Alexh \ Arab_Test_Oledb.xls;

System.Data.OleDb.OleDbConnection MyConnection;

System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();

string sql = null;

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

MyConnection = new System.Data.OleDb.OleDbConnection(provider = Microsoft.Jet.OLEDB.4.0; Data Source =+ filelocation +; Extended Properties = Excel 8.0;);



MyConnection.Open();



myCommand.Connection = MyConnection;



sql =插入[ Sheet1


(EMPLOYEE_ID,ADDRESS1)值('5','e');



myCommand.CommandText = sql ;



myCommand.ExecuteNonQuery();



MyConnection.Close();



}

catch(例外情况)

{



}


将此引用以将数据表导出为ex​​cel

c# - Insert通过OleDb使用Microsoft Access数据库引擎将DataTable导入Excel - Stack Overflow [ ^ ]

How to export data table to excel in c# using Oledb?
I want to transfer data in data table to excel in c# using Oledb provider. I am able to insert few cells using below code:

try
            {
                string filelocation = @"E:\Alex\Arab_Test_Oledb.xls";
                System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                string sql = null;
                //string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=Excel 8.0;");

                MyConnection.Open();

                myCommand.Connection = MyConnection;

                sql = "Insert into [Sheet1$] (EMPLOYEE_ID,ADDRESS1) values('5','e')";

                myCommand.CommandText = sql;

                myCommand.ExecuteNonQuery();

                MyConnection.Close();

            }
            catch (Exception ex)
            {
                
            }



What I have tried:

My code is as below:
try
{
string filelocation = @"E:\Alex\Arab_Test_Oledb.xls";
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
//string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=Excel 8.0;");

MyConnection.Open();

myCommand.Connection = MyConnection;

sql = "Insert into [Sheet1$] (EMPLOYEE_ID,ADDRESS1) values('5','e')";

myCommand.CommandText = sql;

myCommand.ExecuteNonQuery();

MyConnection.Close();

}
catch (Exception ex)
{

}

解决方案

(EMPLOYEE_ID,ADDRESS1) values('5','e')"; myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } catch (Exception ex) { }



What I have tried:

My code is as below:
try
{
string filelocation = @"E:\Alex\Arab_Test_Oledb.xls";
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
//string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=Excel 8.0;");

MyConnection.Open();

myCommand.Connection = MyConnection;

sql = "Insert into [Sheet1


(EMPLOYEE_ID,ADDRESS1) values('5','e')";

myCommand.CommandText = sql;

myCommand.ExecuteNonQuery();

MyConnection.Close();

}
catch (Exception ex)
{

}


refer this for exporting datatable to excel
c# - Insert DataTable into Excel Using Microsoft Access Database Engine via OleDb - Stack Overflow[^]


这篇关于如何使用oledb将数据表导出到C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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