Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ShowData();
        string dbcon = ConfigurationManager.ConnectionStrings["cn"].ToString();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Excel2007.WorkBook workbook = new Excel2007.WorkBook();

        Response.Clear();
        Response.BufferOutput = false;
        string archiveName = "customer.xls";
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + archiveName);
        Response.Charset = "";
      //  workbook.WorkSheets.Add("FirstSheet");
       // workbook.WorkSheets["FirstSheet"].Cells[0, 1].Value = "first";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false; GridView1.DataBind();
        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        //workbook.Save(Response.OutputStream);
        Response.End();
    }
 
    public void ShowData()
    {
 
        SqlDataAdapter adp = new SqlDataAdapter("select catname,catid,creationdate,pagename from quizcats  ", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}
Posted 17 Dec '12 - 23:31
Edited 17 Dec '12 - 23:34

Comments
vijay cyrus - 18 Dec '12 - 5:34
Somebody help me out with this code to create a file with .xlsx extension...this code works nicely to create .xls file....but the file with .xlsx extension can't be opened in the excel application....
Abhishek Pant - 18 Dec '12 - 5:35
whats the problem?
vijay cyrus - 18 Dec '12 - 5:39
@Abhishek Pant.... I'm not able to create file with extension .xlsx

2 solutions

Please follow and try the link
 
Export Gridview Data to Excel in ASP.NET[^]
  Permalink  
I suggest below article can help you, it can uses a free data export library to export data from database to Excel.
9 Solutions to Export Data to Excel for ASP.NET[^]
Excel to Datatable and Datatable to Excel[^]
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 333
1 OriginalGriff 261
2 Mohammed Hameed 198
3 Mayur_Panchal 153
4 Dave Kreskowiak 125
0 Sergey Alexandrovich Kryukov 8,171
1 OriginalGriff 6,236
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid