Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:09:05+00:00 2026-05-29T07:09:05+00:00

I am trying to implement a file download link for an Open XML Excel

  • 0

I am trying to implement a file download link for an Open XML Excel spreadsheet in MVC 2. If I write directly to the Response it works as below…

var data = MooseMartAdoManager.ExecuteSet("Report.StagAllCasesFullDetail");
         var str = ExcelExportManager.GenerateSpreadsheet(data) as MemoryStream;
         Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
         Response.AddHeader(
            "content-disposition",
            "attachment;filename=StagFullDetail" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".xlsx");
         str.WriteTo(Response.OutputStream);

But seeing as I have taken the time to abstract all logic away about how the data is collected and the report is created for code reuse and IOC, it would be nice to be able to work out why this dosent work…

  public FileStreamResult GetExcelDetailExport()
  {
     var data = MooseMartAdoManager.ExecuteSet("Report.StagAllCasesFullDetail");
     var file = ExcelExportManager.GenerateSpreadsheet(data);

     return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
        "StagFullDetail" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".xlsx");
  }

All I get is zero bytes file in txt format.

So what is the “correct” MVC way to handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:09:05+00:00Added an answer on May 29, 2026 at 7:09 am

    One reason that you might be getting this problem is that the file here is at the end of the stream:

    var file = ExcelExportManager.GenerateSpreadsheet(data); // it is at the end of stream
    

    I suggest you set to 0:

    file.Position = 0;
    

    Your previous code works since you use str.WriteTo which ignores the current position.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement an Asynchronous file download from a web server using
While trying to implement an MVC file upload example on Scott Hanselman's blog. I
I'm trying to find a way to implement file manager functionality in an mvc
i'm trying to implement an xsl file to an xml doc. however when i
I was trying to implement a download link and put it beside one of
What I am trying to implement is force download of a file through PHP.
I am trying to implement Gmail style drag-and-drop file upload in ASP.NET MVC. I
I am trying to implement file upload/download via a javascript application written with Backbone.js
I'm trying to understand (and possibly implement) how some file downloaders detect a download
I have an application where I am trying to implement a safe file overwrite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.