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

Hi,

I want to design a asp.net 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?

Thanks.
WhyDidntItWorkAsked:
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.

P1ST0LPETECommented:
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=192.168.1.1;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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnExportData" runat="server" Text="Export Data" OnClick="ExportData_Click" />
    </div>
    </form>
</body>
</html>

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())
            {
                try
                {
                    command.Connection = new SqlConnection("Server=192.168.1.1;Database=DatabaseName;User ID=sa;Password=xxxx;");
                    command.CommandText = "SELECT * FROM [Table] ORDER BY [Column]";
                    command.Connection.Open();

                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    adapter.Fill(table);
                }
                catch(Exception ex)
                {
                    //error happened while getting data:
                    string error = ex.ToString();
                }
            }

            GridView grid = new GridView();
            grid.DataSource = table;
            grid.DataBind();

            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            grid.RenderControl(htmlWriter);

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=ExcelFileName.xls");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/excel";
            Response.Write(stringWriter.ToString());
            Response.Flush();
        }
    }
}

Open in new window



Also, see these other links for further info and examples:
- http://forums.asp.net/t/1185493.aspx
- http://msdn.microsoft.com/en-us/library/as2f1fez.aspx
- http://attemptsatprogramming.blogspot.com/2011/08/sending-dynamic-file-to-client-in.html
- http://www.codeproject.com/KB/webforms/DAtaGridExportToExcel.aspx
- http://www.dotnetspider.com/forum/168566-How-Export-DataTable-Values-Excel.aspx
0

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.
0
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
ASP.NET

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.