如何使用oledb将数据表导出到C#中? [英] How to export data table to excel in C# using oledb?
问题描述
如何使用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(例外情况)
{
}
将此引用以将数据表导出为excel
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屋!