Click here to Skip to main content
11,578,524 members (60,836 online)
Rate this: bad
Please Sign up or sign in to vote.
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)
        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.BufferOutput = false;
        string archiveName = "customer.xlsx";
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + archiveName);
        Response.Charset = "";
        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");
    public void ShowData()
        SqlDataAdapter adp = new SqlDataAdapter("select catname,catid,creationdate,pagename from quizcats  ", ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
        DataSet ds = new DataSet();
        GridView1.DataSource = ds;

the .xls file can be easily created with this code but whenever i change the extension to .xlsx then the file can't be open in excel 2007..please point out the error and suggest me something with the same coding..
Posted 17-Dec-12 22:26pm
Edited 17-Dec-12 22:40pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Please follow the link

Export Gridview Data to Excel in ASP.NET[^]

Change to .xlsx in place of .xls.

vijay cyrus at 18-Dec-12 5:25am
just by changing the extension to .xlsx it is unable to open the file ....

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

  Print Answers RSS
0 Peter Leow 135
1 OriginalGriff 113
2 Richard MacCutchan 85
3 Afzaal Ahmad Zeeshan 75
4 Frankie-C 75
0 OriginalGriff 933
1 Sergey Alexandrovich Kryukov 740
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 403

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100