0

I have the following function to download an excel file of results passed via LoadFromCollection(). It works locally but once I deploy to my Dev server (Ubuntu) I get a 500 error.

await Task.Yield();

var stream = new MemoryStream();

using (var package = new ExcelPackage(stream))
{
    var workSheet = package.Workbook.Worksheets.Add(excelName);

    workSheet.Row(1).Height = 20;
    workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
    workSheet.Row(1).Style.Font.Bold = true;
    workSheet.Column(6).Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
    workSheet.Column(12).Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
    workSheet.Column(13).Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";

    workSheet.Cells.LoadFromCollection(model.Trips, true).AutoFitColumns();

    package.Save();
}
stream.Position = 0;
return File(stream, "application/octet-stream", excelName);

I've tried a few approaches now from tutorials / stack overflow suggestions and I'm just not sure what the problem is anymore. I'm wondering if it's an issue with the operation system (Windows vs Linux) or the environment configuration?

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Possam
  • 363
  • 4
  • 18
  • 2
    A 500 error can come from any exception thrown in the code. I see you solved your problem, that's good. But next time, show the exception message, with the stacktrace so that it is easier to help you, and be sure where the error comes from. – Guillaume S. Jan 14 '20 at 09:20

1 Answers1

0

The issue appears to be .AutoFitColumns(); Not sure why but removing it solved the issue....

Possam
  • 363
  • 4
  • 18
  • Not sure either. I have used this library and this function many times and seems to work in all environments without any problem. – Samy Sammour Jan 14 '20 at 12:01
  • Try to use this instead: workSheet.Cells.AutoFitColumns(); – Samy Sammour Jan 14 '20 at 12:03
  • 1
    Interesting: https://stackoverflow.com/questions/15642950/auto-column-width-in-epplus/15643058 The problem might be that this function is not supported. Try to set the AutoFit on Columns one by one. – Samy Sammour Jan 14 '20 at 12:04