1

I use ClosedXML to generate Excel file, taking data from Microsoft SQL Server. And I write the Date in this fotmat: DDDD D-MM-yyyy.The problem is that it writes in English. I want to set Italian language as default.

This is how I generate Excel file:

 foreach (Attivita attivita in listAttivita)
    {
        worksheet.Cell(index, 1).Value = attivita .Data;
        worksheet.Cell(index, 1).Style.NumberFormat.Format = "DDDD D-MM-yyyy";
        worksheet.Cell(index, 2).Value = attivita.Ord;
        worksheet.Cell(index, 3).Value = attivita.Straord;
        worksheet.Cell(index, 4).Value = attivita.Per;
        worksheet.Cell(index, 5).Value = attivita.Fer;
        worksheet.Cell(index, 6).Value = attivita.Mala;
        worksheet.Cell(index, 7).Value = attivita.Infor;
        worksheet.Cell(index, 8).Value = attivita.Cassa;
        index++;
      }

Allso I set CultureInfo in Stratup.cs but I'm not sure if I'm doing something wrong:

public void ConfigureServices(IServiceCollection services)
        {
            
            CultureInfo[] supportedCultures = new[]
            {
                new CultureInfo("it-IT")
                //new CultureInfo("en")
            };

            services.Configure<RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture("it-IT");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
                options.RequestCultureProviders = new List<IRequestCultureProvider>
                {
                    new QueryStringRequestCultureProvider(),
                    new CookieRequestCultureProvider()
                };
            });
         } 

Anycase this doesn't worked for me when I published the project in IIS.


Update

dddd d-mm-yyyy = "Thursday 28-August-2014"

enter image description here


Update 2:

Value of attivita.Data :

attivita.Data = {01/12/2020 00:00:00}

When I do this :

worksheet.Cell(index, 1).Value = attivita.Data.ToString("dddd, d-MMMM-yyyy", new CultureInfo("it-IT"));

The result is this : enter image description here


And when I do this :

   worksheet.Cell(index, 1).Value = attivita.Data.ToString("dddd, d-mmmm-yyyy", new CultureInfo("it-IT"));

The result is this : enter image description here

It should be like this . The only problem are the 00 that put there. And I can't get why is not working with this dddd, d-MMMM-yyyy that seems to be correct.

Any suggestions how to write the Date in italian, and set Italian as default language ?

ewardz1
  • 29
  • 9

1 Answers1

0

When you set a DateTime in a cell, Excel display according to user's culture (generally Windows settings). You can convert in C# the DateTime to text to force format in Excel.

Example :

var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Data Types");

var co = 2;
var ro = 1;

ws.Cell(++ro, co).Value = "Plain Text:";
ws.Cell(ro, co + 1).Value = "Hello World.";

ws.Cell(++ro, co).Value = "Plain Date:";
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2);

ws.Cell(++ro, co).Value = "Plain Date as text in IT:";
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2).ToString(new CultureInfo("it-IT"));

ws.Cell(++ro, co).Value = "Plain Date as text in IT with custom format:";
ws.Cell(ro, co + 1).Value = new DateTime(2010, 9, 2).ToString("dddd, dd MMMM, yyyy", new CultureInfo("it-IT"));

// Resize columns 
ws.Columns(2, 3).AdjustToContents();

workbook.SaveAs("Test.xlsx");
vernou
  • 6,818
  • 5
  • 30
  • 58