Can I save a file directly to a filestream and avoid disk usage?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dicoy
    New Member
    • Aug 2010
    • 4

    Can I save a file directly to a filestream and avoid disk usage?

    Hi, I'm using VS 2005 and C# to make a website.

    I got a page that exports my report to an xls file (first reading a template, then poulating all the fields, then saving it to another file and finally sending it through a "Response.Write File").

    What I'm trying to do is save the whole xls file to the response.writef ile and avoid disk usage.

    here's the working code I got (the relevant part):
    Code:
    objBook.SaveAs(fPath + "\\" + fName, Excel.XlFileFormat.xlWorkbookNormal,
    null, null, false, false, Excel.XlSaveAsAccessMode.xlShared,
    false, false, null, null, null);
    
    objBook.Close(Excel.XlSaveAction.xlDoNotSaveChanges, null, null);
    
    objApp.Workbooks.Close();
    
    objApp.Quit();
    
    objBook =null;
    objSheet =null;
    objApp =null;
    range =null;
    GC.Collect();
    
    Response.AppendHeader("content-disposition", 
    "attachment; filename=" + fName);
    
    Response.ContentType = "application/x-msdownload";
    Response.WriteFile(fPath + "\\" + fName);
    and what I want to do would be something like:
    Code:
    FileStream fsXLS = new FileStream();
    
    objBook.SaveAs(fsXLS, Excel.XlFileFormat.xlWorkbookNormal,
    null, null, false, false, Excel.XlSaveAsAccessMode.xlShared,
    false, false, null, null, null);
    
    objBook.Close(Excel.XlSaveAction.xlDoNotSaveChanges, null, null);
    
    objApp.Workbooks.Close();
    
    objApp.Quit();
    
    objBook =null;
    objSheet =null;
    objApp =null;
    range =null;
    GC.Collect();
    
    Response.AppendHeader("content-disposition", 
    "attachment; filename=" + fName);
    
    Response.ContentType = "application/x-msdownload";
    Response.WriteFile(fsXLS);
    Obviously that doesn't work, but I cant find any examples explaining what I want to do.
  • dicoy
    New Member
    • Aug 2010
    • 4

    #2
    Aparentlly I would be closer to what I want by using a MemoryStrem instead of a FileStream. Still not sure how to use it though.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Use a MemoryStream, it can be created from a byte[] and it can also be used anywhere a Stream is accepted

      Comment

      • dicoy
        New Member
        • Aug 2010
        • 4

        #4
        MemoryStream

        The problem is I don't know/can't find a way to make excel interop write the file to a memorystream.

        Comment

        Working...