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

  • Home
  • SEARCH
  • 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 8509871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:33:05+00:00 2026-06-11T03:33:05+00:00

When I try to generate an Excel file using EPPlus, Excel give me the

  • 0

When I try to generate an Excel file using EPPlus, Excel give me the following error message:

Excel cannot open the file ‘myfilename.xlsx’ because the file format or file extension is not valid. Verify the the file has not been corrupted and that the file extension matches the format of the file.

Here’s my code:

public ActionResult Index()
{
    using (ExcelPackage package = new ExcelPackage())
    {
        // I populate the worksheet here.  I'm 90% sure this is fine
        // because the stream file size changes based on what I pass to it.

        var stream = new MemoryStream();
        package.SaveAs(stream);

        string fileName = "myfilename.xlsx";
        string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        var cd = new System.Net.Mime.ContentDisposition
        {
            Inline = false,
            FileName = fileName
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(stream, contentType, fileName);
    }
}

Any idea what I’m doing wrong?

  • 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-06-11T03:33:07+00:00Added an answer on June 11, 2026 at 3:33 am

    All you need to do is reset the stream position. stream.Position = 0;

    You shouldn’t write directly to the Response, it’s not the MVC way. It doesn’t follow the correct MVC pipeline and it tightly couples your controller action code to the Response object.

    When you add a file name as the 3rd parameter in File(), MVC automatically adds the correct Content-Disposition header… so you shouldn’t need to add it manually.

    The short of it is, this is what you want:

    public ActionResult Index()
    {
        using (ExcelPackage package = new ExcelPackage())
        {
            // I populate the worksheet here.  I'm 90% sure this is fine
            // because the stream file size changes based on what I pass to it.
    
            var stream = new MemoryStream();
            package.SaveAs(stream);
    
            string fileName = "myfilename.xlsx";
            string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    
            stream.Position = 0;
            return File(stream, contentType, fileName);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have generated excel file. When I try open it, I get error Problems
Hi all I'm using Open XML and ExtremML to generate an Excel File on
I'm using the following code to generate an Excel file using Microsoft.Interop.Excel. The problem
I'm trying to generate excel using c# following is the code snippet for it
I try to generate some Excel sheets in Java application using JExcelAPI (v. 2.6.3)
One of our applications allows users to generate an Excel file using an Excel
I'm using this code to generate an Excel file. It should be simple to
I try to generate a csv file through a lot of functions like that
I'm trying to generate an excel file with some validations, I've read the poi
I have a script that generates a csv file using the following code: header('Content-type:

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.