0

I have website, where user can download excel generated via EPPLus extension.

using (ExcelPackage pck = new ExcelPackage())
{
      ExcelWorksheet ws = pck.Workbook.Worksheets.Add("dates");

      // Create table from dataTable with header
      ws.Cells["A1"].LoadFromCollection(dates, true, TableStyles.Medium6);

      string dateformat = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
      ws.Column(2).Style.Numberformat.Format = dateformat;

      // Autofit Columns               
      ws.Cells[ws.Dimension.Address].AutoFitColumns();

      // Send to browser
      fileBytes = pck.GetAsByteArray();
}
return File(fileBytes, "application/vnd.ms-excel", "dates.xlsx");

Download works fine. Downloaded excel has two columns, the second column is a datetime and I am formatting it as a ShortDate

string dateformat = System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
ws.Column(2).Style.Numberformat.Format = dateformat;

When I open the excel, for United States Region it works fine (datetime value match short date in region settings)

enter image description here enter image description here

But when I change region to Czech

enter image description here enter image description here

The dates in excel does not match the region settings for short date, because day and month is swapped. Why this is happening and how can I solve this issue ? What am I missing ?

Muflix
  • 6,192
  • 17
  • 77
  • 153
  • Are you restarting the application in between changing the system culture? If not, you should try that. The default culture for all future threads is set when the application first starts. – WSC May 21 '20 at 12:16
  • You might find this helpful: https://stackoverflow.com/questions/1542409/how-to-get-current-regional-settings-in-c – WSC May 21 '20 at 12:18
  • Yes, I am restarting browser and the excel application before downloading the file again. The region is changed at the user side not at the server side, so I think there is no need to clear culture cache before each client request. – Muflix May 22 '20 at 05:38
  • It seems that `System.Globalization.DateTimeFormatInfo.CurrentInfo.ShortDatePattern` is taking format from the server region, but `CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern` did not helped. I don't know how to get CultureInfo of the user. Is that even possible, does the client browser send this information to the server or I have to ask user which language he prefers ? – Muflix May 22 '20 at 06:21

1 Answers1

0

In your Controller you can capture the current users language settings from the Request Object, like so:

string language= Request.UserLanguages.FirstOrDefault() ?? "en-US";

If, for whatever reason, the collection is empty, you set a fallback language, here it is US English. You then pass this string to your Excel function, and the first things you do in that function, is to create a new CultureInfo object with the users language and change it in the current thread:

CultureInfo currentCulture = new CultureInfo(language, false);
Thread.CurrentThread.CurrentCulture = currentCulture;

You can now style the cells of your choice, with the correct format by using the DateTimeFormatInfo.CurrentInfo type:

// Load collection into excel AND FORMAT THE CELLS
workSheet.Cells["A1"].LoadFromCollection(dates, true, TableStyles.Medium6).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
Lasse Holm
  • 245
  • 1
  • 5
  • 14
  • Thank you for reponse, I got `HttpRequest does not contain a definition for UserLanguages` exception. I also tried `IRequestCultureFeature` middleware but with no luck. – Muflix May 25 '20 at 13:16
  • I was able to get browser supported language with `HttpContext.Request.GetTypedHeaders().AcceptLanguage` but this can differ to user system settings which are used when excel is open. – Muflix May 25 '20 at 13:40
  • 1
    Sorry @Muflix, I totally missed the fact that you are using asp.net core, not framework. I don't have much experience with core, but found this related SO question, that might provide the solution you are looking for: [Link...](https://stackoverflow.com/questions/49381843/get-browser-language-in-aspnetcore2-0) – Lasse Holm May 25 '20 at 14:54
  • But when browser language differ to users system settings, excel will not work correctly, so I think now, that it has no solution at all :/ only to let user to choose language himself in the web application directly. – Muflix May 26 '20 at 07:50
  • I found interesting thing. Edge Legacy generates Accept Language header attribute based on Preffered Language setting in OS (this can solve my excel issue) but ! Chromium browsers (Chrome, Edge Chromium) generate that attribute based on language setting in the browser (that is cause of my issue). – Muflix May 31 '20 at 10:26