I have the data coming from Entity Data model table on my ASP.NET page. Now I have to export this data into Excel on button click.
If it is using OLEDB, it is straight forward as it is here: http://csharp.net-informations.com/excel/csharp-excel-oledb-insert.htm
Here is my function to read data from inquiries table:
var model = from i in myEntity.Inquiries
where i.User_Id == 5
orderby i.TX_Id descending
select new {
RequestID = i.TX_Id,
CustomerName = i.CustomerMaster.FirstName,
RequestDate = i.RequestDate,
Email = i.CustomerMaster.MS_Id,
DocDescription = i.Document.Description,
ProductName = i.Product.Name
You can still insert into the Excel spreadsheet using the same technique as identified in the linked article.
Just use the following psuedo code
try
{
System.Data.OleDb.OleDbConnection MyConnection ;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
MyConnection.Open();
myCommand.Connection = MyConnection;
myCommand.CommandText = "Insert into [Sheet1$] (id,name) values('@p1', '@p2')";
myCommand.Parameters.Add("@p1", OleDbType.VarChar, 100);
myCommand.Parameters.Add("@p2", OleDbType.VarChar, 100);
// define query to entity data model
var model = from i in myEntity.Inquiries select i;
foreach(var m in model)
{
cmd.Parameters["@p1"].Value = m.RequestID;
cmd.Parameters["@p2"].Value = m.CustomerName;
// .. Add other parameters here
cmd.ExecuteNonQuery();
}
}