Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am retrieving data from database , this data i am want to save in word, excel . how i can do this.
Posted
Comments
Andy Lanng 27-Jan-16 6:32am    
do you even google.
It depends. You can use the official office interop api or you can construct the files manually or via a third party API.
Google is replete with examples
google: c# write word document
google: c# write excel document
BillWoodruff 27-Jan-16 7:44am    
What you will have to do will depend on what the data is, and what format it is in. Unless you tell us that, we are just making guesses.

Start by googling "Create Excel spreadsheet c#", this is one of the most commonly-asked questions are there are literally thousands of articles covering it already.

You can use the Excel ODBC driver, or the XML SDK, or you can automate the Excel application itself, but that might not be suitable depending on how the code is actually launched.
 
Share this answer
 
There are multiple ways to export Sql data to Excel, using OLEDB, Interop object or by using memory stream, you can check below snippet
C#
protected void ExportExcel(object sender, EventArgs e)
{

    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    using (XLWorkbook wb = new XLWorkbook())
                    {
                        wb.Worksheets.Add(dt, "Customers");
                        Response.Clear();
                        Response.Buffer = true;
                        Response.Charset = "";
                        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx");
                        using (MemoryStream MyMemoryStream = new MemoryStream())
                        {
                            wb.SaveAs(MyMemoryStream);
                            MyMemoryStream.WriteTo(Response.OutputStream);
                            Response.Flush();
                            Response.End();
                        }
                    }
                }
            }
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900