Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2004, 01:59 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default Exporting to Excel from .NET

In relation to my last question, what I am trying to do is to export a result set to an excel spread sheet so that the user can save, sort, etc. I have done this with classic asp, I looped through the result set and inserted the values into a table, but first setting the content type to Excel. How would this be done using ASP.NET. I would like to do it the correct way instead of using the way I did it previously.

Any help would be great...
Thanks,

Jim



 
Old February 6th, 2004, 03:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

http://p2p.wrox.com/topic.asp?TOPIC_ID=5852
 
Old February 6th, 2004, 04:03 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I am trying to export from .NET to Excel, not the other way around. Thanks

 
Old February 6th, 2004, 04:11 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Jim,

The way you did it is no different for ASP.net. Remember that the end result is the same regardless of it being .net. Just like in ASP you would set the content type:

Response.ContentType = "application/vnd.ms-excel"

Do this in page load. You will probably want to purge the response buffer to clean it all out before you write your HTML table.

Here's a sample, and here's the code for it:

<%@ Page Language="vb" AutoEventWireup="true" %>
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Request("showPlainHTML") Is Nothing Then
        Response.ContentType = "application/vnd.ms-excel"
    End If
End Sub
</script>
<table border="1">
    <tr><td colspan="10"><h1>This is the header</h1></td></tr>
    <tr><td colspan="10" align="center"><a href="webToExcel.aspx?showPlainHTML=true">Show as plain HTML</a></td></tr>
    <tr><td colspan="10" align="center"><a href="webToExcel.aspx">Show in Excel</a></td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td><td>data</td></tr>
    <tr><td colspan="10"><h3>This is the footer</h3></td></tr>
</table>


Peter
------------------------------------------------------
Work smarter, not harder.
 
Old February 6th, 2004, 04:34 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Peter thanks,

  I actually found a way to use the datagrid too. But I think your way will work better, more flexability. Thanks for you help!!!

Jim







Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting to excel michaeltanfeliz Beginning VB 6 2 May 3rd, 2008 05:26 AM
exporting to excel caveatashish Classic ASP Basics 0 November 3rd, 2006 05:08 AM
Exporting to Excel swhite Access VBA 2 September 18th, 2003 09:42 AM
Exporting to Excel JJ Access 1 June 9th, 2003 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.