Создание файла Excel с EPPlus не удается - PullRequest
14 голосов
/ 08 марта 2012

Когда я пытаюсь создать файл Excel с помощью EPPlus, Excel выдает следующее сообщение об ошибке:

Excel не может открыть файл «myfilename.xlsx», так как формат файла или расширение файла недопустимы. Убедитесь, что файл не был поврежден и что расширение файла соответствует формату файла.

Вот мой код:

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);
    }
}

Есть идеи, что я делаю не так?

Ответы [ 3 ]

28 голосов
/ 03 сентября 2012

Все, что вам нужно сделать, это сбросить позицию потока.stream.Position = 0;

Вы не должны писать прямо в ответ , это не путь MVC.Он не следует правильному конвейеру MVC и тесно связывает код действия вашего контроллера с объектом Response.

Когда вы добавляете имя файла в качестве третьего параметра в File(), MVC автоматически добавляет правильные Content-Disposition header ... так что вам не нужно добавлять его вручную.

Короче говоря, это то, что вы хотите:

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);
    }
}
10 голосов
/ 08 марта 2012

Ваш код не показывает, что stream записывается в HttpResponse - предположительно это делается методом File, который вы не опубликовали.

Один из способов работы заключается в следующем:

Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader(
            "content-disposition", String.Format(CultureInfo.InvariantCulture, "attachment; filename={0}", fileName));
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
2 голосов
/ 11 февраля 2014

Как и в ответ Джо, мне все равно пришлось звонить Response.ClearHeaders():

   protected void btnDownload_Click(object sender, EventArgs e)
   {

       ExcelPackage pck = new ExcelPackage();
       var ws = pck.Workbook.Worksheets.Add("Sample2");

       ws.Cells["A1"].Value = "Sample 2";
       ws.Cells["A1"].Style.Font.Bold = true;
       var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
       shape.SetPosition(50, 200);
       shape.SetSize(200, 100);
       shape.Text = "Sample 2 outputs the sheet using the Response.BinaryWrite method";
       Response.Clear();    
       Response.ClearHeaders();
       Response.BinaryWrite(pck.GetAsByteArray());
       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
       Response.AddHeader("content-disposition", "attachment;  filename=Sample2.xlsx");
       Response.End();
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...