Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Export To Excel

Posted on September 25, 2013October 26, 2015 By David Kittell

Add GridView, Datasource, and Button

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="exporttest.aspx.cs" EnableEventValidation="false" Inherits="ExportTest" %>
<form id="form1" runat="server">

<asp:gridview runat="server" id="gvExport" allowpaging="True" autogeneratecolumns="False" datakeynames="id" datasourceid="dsExport">
</asp:gridview>

<asp:sqldatasource id="dsExport" runat="server" conflictdetection="CompareAllValues" connectionstring="<%$ ConnectionStrings:DbConnection %>"  selectcommand="SELECT top 100 * FROM Users">
</asp:sqldatasource>

<asp:button id="btnExport" runat="server" onclick="Button1_Click" text="Export To Excel" />

</form>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ExportTest : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{

	}
	protected void Button1_Click(object sender, EventArgs e)
	{
		Response.Clear();
		gvExport.AllowPaging = false;
		gvExport.DataBind();

		Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");

		Response.ContentType = "application/vnd.xls";

		System.IO.StringWriter stringWrite = new System.IO.StringWriter();

		System.Web.UI.HtmlTextWriter htmlWrite =
		new HtmlTextWriter(stringWrite);

		gvExport.RenderControl(htmlWrite);

		Response.Write(stringWrite.ToString());

		Response.End();
	}

	public override void VerifyRenderingInServerForm(Control control)
	{

	}
}
Originally Posted on September 25, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

C# ASPX Code CSharp

Post navigation

Previous post
Next post

Related Posts

Facebook Event List

Posted on December 19, 2013October 26, 2015

This is not my work (Mike Dalisay), simply posting as I found this code useful. After creating an app, we’ll have the following codes on our index.php file <style type=’text/css’> body{ font-family: "Proxima Nova Regular","Helvetica Neue",Arial,Helvetica,sans-serif; } .clearBoth{ clear: both; } .event{ background-color: #E3E3E3; margin: 0 0 5px 0; padding:…

Read More

Remove HTML Tags

Posted on October 23, 2013October 26, 2015

This script will remove all HTML tags from a given column in a table, useful if you need to simply have the text. IF OBJECT_ID(N’dbo.RemoveHTMLTags’) IS NOT NULL DROP FUNCTION dbo.RemoveHTMLTags GO CREATE FUNCTION [dbo].[RemoveHTMLTags] (@HTMLText VARCHAR(MAX)) RETURNS VARCHAR(MAX) AS BEGIN DECLARE @Start INT DECLARE @End INT DECLARE @Length INT…

Read More
Code

Ubuntu – WordPress Install

Posted on September 2, 2015May 31, 2017

These steps are based on Ubuntu 14.10 Server 64-bit but can be applied to virtually any Debian based UNIX distribution. Originally Posted on September 2, 2015Last Updated on May 31, 2017 All information on this site is shared with the intention to help. Before any source code or program is…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes