1. Original Entry + Comments2. Write a Comment3. Preview Comment
New comments for this entry are disabled.


November 12, 2003  |  HTML --> Excel in ASP.NET  |  2383 hit(s)

I ran across this 2x in about 15 minutes, and it seemed like fun to try. You can "convert" your data to a downloadable spreadsheet in ASP.NET by 1) outputting it as an HTML table and 2) setting the page's ContentType to "application/vnd.ms-excel". (References/credit here and here.)

If you have Excel installed, try it by clicking here, where you get the 10 most recent blog entries (abbreviated) in a spreadsheet.

The code:
<%@ Page Language="VB" Debug="true" EnableViewState="False" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

Sub Page_Load()
Dim ds As Dataset = GetBlog()
Dim dr As Datarow
Dim tableString As New StringBuilder
tableString.Append("<table border=1>")
tableString.Append("<thead><th>Title</th><th>EntryDate</th>" & _
"<th>Text</th></thead>")
For Each dr in ds.Tables(0).Rows
tableString.Append("<tr><td>" & dr("title") & _
"</td><td>" & dr("EntryDateTime") & "</td><td>" & _
Server.HtmlEncode(dr("ShortDescription")) & "</td></tr>")
Next
tableString.Append("</table>")
Literal1.Text = tableString.ToString()
Response.ContentType = "application/vnd.ms-excel"
End Sub

Function GetBlog() As System.Data.DataSet
Dim connectionString As String = _
ConfigurationSettings.AppSettings("connectionString")
Dim dbConnection As New SqlConnection(connectionString)
Dim queryString As String = "SELECT top 10 Title, EntryDateTime, " & _
" Substring(Description, 0, 25) As ShortDescription FROM [blog] " & _
" ORDER BY EntryDateTime DESC"
Dim dbCommand As New SqlCommand(queryString, dbConnection)
Dim dataAdapter As New SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Literal id="Literal1" runat="server"></asp:Literal>
</form>
</body>
</html>




Colt   12 Nov 03 - 11:26 PM

I'm more lazy than you and I just databind to an instance of DataGrid, RenderControl() to a StringBuilder object and then write it out:

E.g.
// DataBinding
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
dgd.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();


 
Mike   12 Nov 03 - 11:41 PM

Nice. Makes the whole page 22 lines long.

Implemented: http://www.mikepope.com/blog/fun/blogasss2.aspx


 
Erik   02 Dec 03 - 12:41 AM

Hi,

Found your blog through google and saw the code for showing the blog entries in a spreadsheet. When I open http://www.mikepope.com/blog/fun/blogasss2.aspx I see that the entrydatetime format looks different for December 1st. (12-1-2003 18:26 and 11/30/200311:44:39 PM) For the December 1st entree Excel says the cell property is Adjusted while for the November entries the cell properties are Standard. I was wondering if this is an Excel issue or something else and I'm also curious if you also see this.(i'm using Excel 2000 90.6926 SP3 in Dutch)

Greetings,

Erik


 
Mike   02 Dec 03 - 7:11 AM

Hi, Erik. When I open the page, I see the same format for all the dates (basically, mm/dd/yyyy hh:mm). I'm using Excel 2000 9.0.3821 SR-1.

I've seen questions before about how Excel interprets the information in the Web page. All the information being fed into the spreadsheet is initially typed as String, I guess, since that's what is in the HTML being generated. Clearly Excel has internal rules for how to convert the information. It's hard to imagine why Excel might treat two similar dates in so different a fashion as you illustrate, isn't it?

The question might be one that could be fruitfully pursued in some sort of Excel forum, perhaps. If you like, I'd be happy to send you the source code for the page and the raw data.


 
Erik   03 Dec 03 - 1:31 AM

Hi Mike, Thanks for the feedback! I just saw the same thing happen in a script I made in classic asp so it's an excel thing for sure.
Keep up the good work and I'll keep enjoying your blog!

Greetings from Holland!

Erik


 
michael   21 Jan 05 - 8:16 AM

Hi,

only excel opens for me eventhough the infor is stored in literal nothing in there in excel
bye
mike


 
pallavi   23 Jan 05 - 10:53 PM

hello
I also want to export whole aspx page as it is (including chart,images,grid) to excel, i have tried the code..with refering many sites and u'r content

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "" ' Turn off the view state.
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
response.Buffer = False
me.RenderControl(hw) 'to export whole form content
Response.Write(tw.ToString()) ' End the response.
Response.Flush()
Response.Close()

whole form content get exported to excel but not in proper format,
I mean all images or content display on each other.
is there any solution that aspx page as it is get exported(also in proper format and allignment), plz email me!


 
Sachin   18 Mar 05 - 9:51 PM

Hello,every body
I want to export the contents of datagrid to Excel Sheet and referring all the answers,I have written the following code:

Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Response.Buffer = False
DataGrid1.RenderControl(hw)
Me.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
But when I am calling this code,the empty Excel Sheet arises ,whether I am missing any part of the code or is there any mistake in my code? If you are getting my problem ,please help me as early as possible

Thanks in advance for your help,

Regards,
Sachin


 
mike   19 Mar 05 - 8:04 AM

Hi, Sachin. This blog is not really a tech support forum. I suggest that you post your question on the ASP.NET forums at:

http://asp.net/Forums/Default.aspx?tabindex=1&tabid=39

Good luck!

-- Mike


 
Sachin   20 Mar 05 - 7:51 PM

Hi Mike thank you very much,
will you please tell me some more websites' URLs ,from where I can get the solutions for my ASP.NET problems.
Thanks,
with regards,
Sachin.