how adoStream writes from asp to asp.net?

B

Ben

Hello,

I have an asp routine to display an Excel worksheet on an
a web (asp) page. I have created aspx pages but only in
the classroom. Point - I have the basics for aspx. Could
someone show me how I would perform the routine below (asp
routine) in aspx? I have VS 2003. Is there a control I
would use instead of the adoStream, or stick with
adoStream?

<%
If Session("LoggedIn") = True Then
Session("LoggedIn") = False
Response.ContentType ="application/vnd.ms-excel"
Path = "C:\MyFiles\MyExcel.xls"
Set adoStream = Server.CreateObject("ADODB.Stream")
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(Path)
Response.BinaryWrite adoStream.Read()
adoStream.Close: Set adoStream = Nothing
Response.End
Else
response.redirect("FirstPage.asp")
End If
%>

Thanks,
Ben
 
L

Lucas Tam

Is there a control I
would use instead of the adoStream, or stick with
adoStream?

You would use a System.IO stream such as memoryStream to read a file off
the disk and send it through the http response object.
 
B

Ben

May I ask for a hint what that would look like in
comparison to the asp code example using the adodb.stream
object?

Thanks,
B
 
B

Ben

Well, I got this far. Found an example on the net, but I
don't know how to invoke the memorystream - the syntax. I
know how to declare the namespace for System.IO. That
would use the Imports key word. May I request the syntax
for using memorystream to open an Excel file?

Aspx example:

<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(Src as Object, E as EventArgs)
Response.ContentType = "application/vnd.ms-excel"
End Sub
</script>
<html>
<head>
<title>ASP.NET Excel Sample</title>
</head>
<body>

<table>
<thead>
<tr>
<th bgcolor="blue"><font color="white">Name</font></th>
<th bgcolor="blue"><font color="white">Sales</font></th>
<th bgcolor="blue"><font
color="white">Commission</font></th>
</tr>
</thead>
<tbody>
<tr>
<td>Joe Shmo</td>
<td>6420</td>
<td>=(B2 * 0.05)</td>
</tr>
<tr>
<td>Jane Shmo</td>
<td>3675</td>
<td>=(B3 * 0.05)</td>
</tr>
</tbody>
</table>

</body>
</html>

Thanks,
B
 
P

Patrice

ASP.NET provides a method to do so. You could just use :

Response.ContentType="application/vnd.ms-excel"
Response.WriteFile "c:\MyPath\MyFile.xls"

instead of handling explictely file i/o operations...


Patrice

--
 
B

Ben

I tried this method but the page was unreadable. Here is
what I tried:

<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(Src as Object, E as EventArgs)
Response.ContentType = "application/vnd.ms-excel"
Response.WriteFile ("C:\Test\ExcelTest.xls")
End Sub
</script>
<html>
<head>
<title>Excel Test</title>
</head>
<body>

</body>
</html>

-------------------------------------------------------

here is something else I am going to try -- using a byte
array. Any comments/suggestions appreciated.
....
Dim fs As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim bw As New System.IO.BinaryReader(fs)
Dim byt() As Byte
byt = bw.ReadBytes(cint(fs.Length))
Response.ContentType = "application/vnd.ms-excel"
Response.OutputStream.Write(byt, 0, length)
....

Ben
 
B

Ben

Here is something that worked. Any advice appreciated on
hidden gotcha's to look for with this routine:

Imports System.IO

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

Private Sub Page_Load(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.Load
GetFile()
End Sub

Private Sub GetFile()
Dim FilePath As String = "C:\test\ExcelTest.xls"
Dim fs As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim bw As New System.IO.BinaryReader(fs)
Dim byt() As Byte, i As Integer
byt = bw.ReadBytes(CInt(fs.Length))
i = byt.Length()
Response.ContentType = "application/vnd.ms-excel"
Response.OutputStream.Write(byt, 0, i)
Response.OutputStream.Close()
End Sub

End Class

The way this will work is that from another page I will
have multiple listboxes (I have several excel files
categorized in various directories). From listbox1 you
pick a category, listbox2 populates based on that
category, then you select a file, pass that file to the
next page which contains the routine above and display the
contents of the file.
 
P

Patrice

As you want to stream an Excel file, you'll have to remove the HTML code.
Only the file must be sent...

Patrice

--

"Ben" <[email protected]> a écrit dans le message de
I tried this method but the page was unreadable. Here is
what I tried:

<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(Src as Object, E as EventArgs)
Response.ContentType = "application/vnd.ms-excel"
Response.WriteFile ("C:\Test\ExcelTest.xls")
End Sub
</script>
<html>
<head>
<title>Excel Test</title>
</head>
<body>

</body>
</html>

-------------------------------------------------------

here is something else I am going to try -- using a byte
array. Any comments/suggestions appreciated.
....
Dim fs As New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim bw As New System.IO.BinaryReader(fs)
Dim byt() As Byte
byt = bw.ReadBytes(cint(fs.Length))
Response.ContentType = "application/vnd.ms-excel"
Response.OutputStream.Write(byt, 0, length)
....

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top