Creating a soft copy report from a database connected to an website?

Posted on 2011-10-07
Last Modified: 2012-05-12

I want to design a website that is connected to a database. What's the easist way to export information fom the tables or queries into a soft copy report like Excel or text file?

Question by:WhyDidntItWork
    LVL 10

    Accepted Solution

    This is the quick and easy approach.  Note you have to provide the database IP address, database name, username and password into this line:

    command.Connection = new SqlConnection("Server=;Database=DatabaseName;User ID=sa;Password=xxxx;");

    Html Markup:
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication4.WebForm2" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
            <asp:Button ID="btnExportData" runat="server" Text="Export Data" OnClick="ExportData_Click" />

    Open in new window

    Code Behind:
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    namespace WebApplication4
        public partial class WebForm2 : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)
            protected void ExportData_Click(object sender, EventArgs e)
                DataTable table = new DataTable();
                using (SqlCommand command = new SqlCommand())
                        command.Connection = new SqlConnection("Server=;Database=DatabaseName;User ID=sa;Password=xxxx;");
                        command.CommandText = "SELECT * FROM [Table] ORDER BY [Column]";
                        SqlDataAdapter adapter = new SqlDataAdapter(command);
                    catch(Exception ex)
                        //error happened while getting data:
                        string error = ex.ToString();
                GridView grid = new GridView();
                grid.DataSource = table;
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
                Response.AddHeader("content-disposition", "attachment; filename=ExcelFileName.xls");
                Response.Charset = "";
                Response.ContentType = "application/excel";

    Open in new window

    Also, see these other links for further info and examples:

    Author Closing Comment

    Wow! Thanks for the detailed response. I should have given more guidance as I wasn't initially looking for code example.  At this point, I was just looking for a sense of general direction so I can do more investigating of my own.  However, you read my mind and was able to push me in the right direction.  Much appreciated for the help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now