|
-
Aug 30th, 2005, 05:01 AM
#1
Thread Starter
Fanatic Member
Open file in browser
Guys,
This must be a quick fix . . .
I am creating an excel file and saving it back to a share on the web server. I was then trying to open this xls file in the browser (response.redirect) so the user could view the results and do a "save as" to their local machine if required. The ability to view the results in the browser window would be best, alternatively I could just bang a button on screen that would then launch the created file (unc path) in excel, but that gets messy.
In the code below, sFile is the string containing the path/filename we save to, then rFile is the UNC path to that file.
Could anybody suggest a solution to this please ?
Thanks
Bob
VB Code:
Dim oExcel As New Excel.Application
Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
Dim oCells As Excel.Range
Dim sFile As String, sTemplate As String
sFile = Server.MapPath(Request.ApplicationPath) & "RESULTS\" & CType(Qid, String) & " - " & CType(Session("ReportName"), String) & " RESULTS.xls"
sTemplate = Server.MapPath(Request.ApplicationPath) & _
"\HPTResults.xlt"
Dim rFile As String 'Results File
rFile = "\\bls2mbr25\RESULTS\" & CType(Qid, String) & " - " & CType(Session("ReportName"), String) & " RESULTS.xls"
oExcel.Visible = False : oExcel.DisplayAlerts = False
'Start a new workbook
oBooks = oExcel.Workbooks
oBooks.Open(Server.MapPath(Request.ApplicationPath) & _
"\HPTResults.xlt") 'Load template
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item("rawdata"), Excel.Worksheet)
'Do all the data export here.
'Export Team Answers
Dim x As Integer
Dim y As Integer
Dim range As String
For x = 0 To Answers.GetUpperBound(0)
y = x + 2
range = "A" & y & ":D" & y
oSheet.Range(range).Value = Answers(x)
Next
'Export TeamLeader Answers
For x = 0 To Leader.GetUpperBound(0)
y = x + 2
range = "E" & y & ":G" & y
oSheet.Range(range).Value = Leader(x)
Next
oSheet.SaveAs(sFile)
oBook.Close()
'Quit Excel and thoroughly deallocate everything
oExcel.Quit()
ReleaseComObject(oSheet)
ReleaseComObject(oSheets) : ReleaseComObject(oBook)
ReleaseComObject(oBooks) : ReleaseComObject(oExcel)
oExcel = Nothing : oBooks = Nothing : oBook = Nothing
oSheets = Nothing : oSheet = Nothing
System.GC.Collect()
Label7.Text = rFile
Response.Redirect(rFile) 'Send the user to the file
End Sub
-
Aug 30th, 2005, 06:46 AM
#2
Re: Open file in browser
Your code seems fine, but what is the problem?
-
Aug 30th, 2005, 06:59 AM
#3
Thread Starter
Fanatic Member
Re: Open file in browser
Thanks for the response MH,
The problem is, I get a "File not found" error when trying to send the user to the xls file.
Is this because I am using Response.Redirect to a unc path rather than url ?
If I use a hyperlink and do this . . HyperLink1.NavigateUrl = rFile, when it is clicked I get a dialogue asking if I want to open or save the xls file, I think this would be the ideal solution. If they click open, it opens the xls file in the browser, if save then they can save it to their local machine. But how do I do this programatically without the user having to click this hyperlink ???
Thanks again,
Bob
-
Aug 30th, 2005, 11:37 AM
#4
Re: Open file in browser
I know it may sound like a crude workaround, but instead of doing a Response.Redirect, add some javascript to the page load event...
document.location.href='path to xls';
I'm assuming you know how.
-
Aug 31st, 2005, 02:36 AM
#5
Thread Starter
Fanatic Member
Re: Open file in browser
Thanks mate, thats sounds like it would be sweet as . . . but please asume I know nothing . . . especially Javascript !
I will try to find my way around now mate, incase you don't get chance to reply.
Thanks
Jason
-
Aug 31st, 2005, 04:09 AM
#6
Re: Open file in browser
OK. Look at the code you've posted in the first post of this thread. Remove the Response.Redirect from there, and replace it with this:
VB Code:
Page.RegisterStartupScript("myScript","<script language=javascript>document.location.href=" & rFile & ";</script>")
Be sure to check on the value of rfile...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|