November 19, 2004

ASP.NET Export To Excel

I was ask about how to do an export to Excel from ASP.NET yesterday. Since I've done this a few times now, I thought I'd post the code. To use it though, read up on HttpHandlers...

using System;
using System.Web;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using System.IO;
using System.Web.SessionState;

namespace DiamondB.TextApp
{
/// <summary>
/// Export a SQL query to excel from ASP.NET
/// </summary>
public class ExportToExcel: IHttpHandler, IRequiresSessionState
{
public ExportToExcel()
{
//
// TODO: Add constructor logic here
//
}

#region IHttpHandler Members

/// <summary>
/// This is the main entry point. You can read about this in other places
/// from more reliable sources than me. The context object contains
/// the session, user, and other objects you are used to.
/// </summary>
public void ProcessRequest(HttpContext context)
{
string sHtml = "";
try
{
string sSQL = CreateSQLQuery();
sHtml = RenderGrid(sSQL);
}
catch(Exception ex)
{
sHtml = "<body>There was a problem executing the query.<br>"+ex.Message+"</body>";
}

// return the results to the browser
context.Response.Clear();
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.Charset = "";
context.Response.Write(sHtml);
context.Response.End();
}

/// <summary>
/// DESCRIPTION: This property defines whether another HTTP handler can
/// reuse this instance of the handler.
///
/// NOTE: false is always returned since this handler is synchronous
/// and is not pooled.
/// </summary>
public bool IsReusable
{
get { return false;}
}

#endregion
/// <summary>
/// I have specialized dynamic queries that I create on the fly. That is why I have
/// the query creation routine separate. For most cases this is probably not needed.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private string CreateSQLQuery()
{
string sQuerySQL = "SELECT * FROM SCHEMA.TABLE"
return sQuerySQL;
}

/// <summary>
/// This code takes the SQL query, executes it, pumps the data into a data grid,
/// grabs the HTML out of the grid, and returns. Simple, right!
/// Also left in this code is some of my query logging code. Every time the query
/// is run I log the query, how long it took to execute, and how many rows were returned.
/// But that is just me.
/// </summary>
private string RenderGrid(string sql)
{
// I just put this here so something is outputted.
string sHTML = "<html><body><pre>" + sql + "</pre></body></html>";

int iRows = -1;
double dSeconds = 0.0;
DateTime dtStart;
try
{
string sConn = ConfigurationSettings.AppSettings["OracleConnectionString"];
OracleConnection oConn = new OracleConnection(sConn);
OracleCommand oCmd = new OracleCommand(sql, oConn);
oConn.Open();
try
{
dtStart = DateTime.Now; // get the current time
// get the data
DataSet ds = new DataSet();
OracleDataAdapter da = new OracleDataAdapter(oCmd);
da.Fill(ds);
// check how long the query took
TimeSpan ts = DateTime.Now - dtStart;
dSeconds = ts.TotalSeconds;
// setup and load the grid.
System.Web.UI.WebControls.DataGrid oGrid = new
System.Web.UI.WebControls.DataGrid();
oGrid.HeaderStyle.BackColor = System.Drawing.Color.Silver;
oGrid.HeaderStyle.ForeColor = System.Drawing.Color.Black;
oGrid.DataSource = ds;
oGrid.DataBind();

// get the html from the grid
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHTMLWriter = new
System.Web.UI.HtmlTextWriter(oStringWriter);
oGrid.RenderControl(oHTMLWriter);
sHTML = oStringWriter.ToString();
oHTMLWriter.Close();
oStringWriter.Close();
// get the number of rows returned
iRows = ds.Tables[0].Rows.Count;
}
finally
{
oConn.Close();
}
}
catch(Exception ex)
{
sHTML += ex.Message;
}
return sHTML;
}
}
}

November 16, 2004

Got my HotMail space...

So I finally got my extra 250 meg for my hotmail account.

Now the question is: does it matter? I've already got a 1 gig gmail account. (and 6 invites to anyone who needs one).

Well, let the spam begin.

November 10, 2004

November 08, 2004

My living hell

Or one version of it....

This post is a little late in coming, I should have posted it about a week or two before Halloween.
What is the problem, my Christmas decorations are already up!!! And not that they are up recently, no this happened BEFORE Halloween (or Reformation Day for those inclined).

Thank the Lord I have a fake tree, but for the love of sanity, this is to much. This has to be the only time my house has beat the Mall for Christmas decorations.

This is the type of crap that happens when your wife gets pregnant. If any of you were wondering if that whole nesting thing really happens -- IT DOES!!! No amount of progressive women’s movement for the last 100 years can change thousands of years of God-wired instinct!

What is nesting, nesting is your wife/significant female other trying to get the house ready for baby, in any way, shape, or form. This includes Christmas decorations for a baby that isn't suppose to come until May!

I think I've lost some control again.

November 03, 2004

I'm back

I did the one thing that will give any good conservative heartburn: I went to Seattle right before an election. I was there to watch the Seattle Seahawks pound the Carolina Panthers into the turf (we amazing weather while there as well - sunny every day).

But while there for the weekend I saw every liberal propaganda sign there was on every corner. There were people lined up around the stadium for blocks with signs. Do note: I did see one "Veterans for Bush" supporter. We tried to give him a little support as we passed.

But all said, everything went fine. I can cant say we were accosted by anyone but panhandlers (we just don’t see those in Boise), and they seem to be rather non-partial. The weather was great, the game was great -- we had tickets right in front of the cheerleaders, 20 yard line, 6 rows up.

Now that was last weekend. This is Wednesday, Nov 3, 2004. One day after the election. Kerry just conceded the vote to Bush. Daschle is gone. Republicans hold majorities in all branches of government. Hopefully they can keep tabs on Bush's spending. Because now they have no-one else to blame.