There is an ADO.NET provider for Excel. This means that if you have a dataset, you can use two DataAdapters to move data from one place to another: from Oracle to Excel, from SQL to Excel, from Excel to Oracle, etc.
DataSet DA, DA. DataAdapters - OleDbDataAdapter, SqlDataAdapter, OracleDataAdapter .. , .
CSV XML. Office Automation, PIA . ADO.NET.
Excel, Microsoft.Jet.OLEDB oledb.
.
:
System.Data.DataSet ds1;
const string ConnStringSql= "Provider=sqloledb;Data Source=dinoch-8;Initial Catalog=Northwind;Integrated Security=SSPI;" ;
const string OutputFilename= "ExtractToExcel.xls";
const string ConnStringExcel=
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + OutputFilename + ";" +
"Extended Properties=\"Excel 8.0;HDR=yes;\"";
const string sqlSelect="SELECT top 10 ProductId, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock, GETDATE() as Extracted from Products order by UnitPrice";
const string sqlInsert="INSERT INTO Extracto (ProductId, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock, Extracted) VALUES (@ProductId, @ProductName, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @Extracted)";
private void ReadFromSql()
{
var ConnSql= new System.Data.OleDb.OleDbConnection(ConnStringSql);
var da1 = new System.Data.OleDb.OleDbDataAdapter();
da1.SelectCommand= new System.Data.OleDb.OleDbCommand(sqlSelect);
da1.SelectCommand.Connection= ConnSql;
ds1= new System.Data.DataSet();
da1.Fill(ds1, "Extracto");
}
private void InsertIntoExcel()
{
foreach (System.Data.DataRow r in ds1.Tables[0].Rows)
{
r.SetModified();
}
var da2 = new System.Data.OleDb.OleDbDataAdapter();
da2.UpdateCommand= new System.Data.OleDb.OleDbCommand(sqlInsert);
da2.UpdateCommand.Connection= ConnExcel;
da2.UpdateCommand.Parameters.Add("@ProductId", System.Data.OleDb.OleDbType.Integer, 4, "ProductId");
da2.UpdateCommand.Parameters.Add("@ProductName", System.Data.OleDb.OleDbType.VarWChar, 40, "ProductName");
da2.UpdateCommand.Parameters.Add("@QuantityPerUnit", System.Data.OleDb.OleDbType.VarWChar, 20, "QuantityPerUnit");
da2.UpdateCommand.Parameters.Add("@UnitPrice", System.Data.OleDb.OleDbType.Currency, 8, "UnitPrice");
da2.UpdateCommand.Parameters.Add("@UnitsInStock", System.Data.OleDb.OleDbType.SmallInt, 2, "UnitsInStock");
da2.UpdateCommand.Parameters.Add("@Extracted", System.Data.OleDb.OleDbType.Date, 8, "Extracted");
da2.Update(ds1, "Extracto");
}
Excel, , Excel Automation , .
XLS ( XLSX) . .., ilve. XLS .
XLS / XLS, , . XLS /. , , ..