Results 1 to 6 of 6

Thread: Open file in browser

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    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:
    1. Dim oExcel As New Excel.Application
    2.         Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
    3.         Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
    4.         Dim oCells As Excel.Range
    5.         Dim sFile As String, sTemplate As String
    6.         sFile = Server.MapPath(Request.ApplicationPath) & "RESULTS\" & CType(Qid, String) & " - " & CType(Session("ReportName"), String) & " RESULTS.xls"
    7.         sTemplate = Server.MapPath(Request.ApplicationPath) & _
    8.           "\HPTResults.xlt"
    9.         Dim rFile As String 'Results File
    10.         rFile = "\\bls2mbr25\RESULTS\" & CType(Qid, String) & " - " & CType(Session("ReportName"), String) & " RESULTS.xls"
    11.  
    12.         oExcel.Visible = False : oExcel.DisplayAlerts = False
    13.  
    14.         'Start a new workbook
    15.         oBooks = oExcel.Workbooks
    16.         oBooks.Open(Server.MapPath(Request.ApplicationPath) & _
    17.         "\HPTResults.xlt") 'Load template
    18.         oBook = oBooks.Item(1)
    19.         oSheets = oBook.Worksheets
    20.         oSheet = CType(oSheets.Item("rawdata"), Excel.Worksheet)
    21.  
    22.         'Do all the data export here.
    23.         'Export Team Answers
    24.         Dim x As Integer
    25.         Dim y As Integer
    26.         Dim range As String
    27.         For x = 0 To Answers.GetUpperBound(0)
    28.             y = x + 2
    29.             range = "A" & y & ":D" & y
    30.             oSheet.Range(range).Value = Answers(x)
    31.         Next
    32.  
    33.         'Export TeamLeader Answers
    34.         For x = 0 To Leader.GetUpperBound(0)
    35.             y = x + 2
    36.             range = "E" & y & ":G" & y
    37.             oSheet.Range(range).Value = Leader(x)
    38.         Next
    39.  
    40.         oSheet.SaveAs(sFile)
    41.         oBook.Close()
    42.  
    43.         'Quit Excel and thoroughly deallocate everything
    44.         oExcel.Quit()
    45.  
    46.         ReleaseComObject(oSheet)
    47.         ReleaseComObject(oSheets) : ReleaseComObject(oBook)
    48.         ReleaseComObject(oBooks) : ReleaseComObject(oExcel)
    49.         oExcel = Nothing : oBooks = Nothing : oBook = Nothing
    50.         oSheets = Nothing : oSheet = Nothing
    51.         System.GC.Collect()
    52.  
    53.         Label7.Text = rFile
    54.         Response.Redirect(rFile) 'Send the user to the file
    55.  
    56.     End Sub

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Open file in browser

    Your code seems fine, but what is the problem?

  3. #3

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  5. #5

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    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

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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:
    1. 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
  •  



Click Here to Expand Forum to Full Width