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


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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WhyDidntItWorkAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.