Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
690
Howto set date format in infragistics excel independent of current culture?
posted

Hi,

I'm using infragistics.excel to export dynamic (lists) data to excel. The date format has to be dd-mmm-yyyy.

This works if system language is english. It doesn't work on german systems, where it would expect tt-mmm-jjjj.

How can I set the correct date format indenpendent of current system culture?

I already tried

            Workbook workBook = new Workbook(WorkbookFormat.Excel2007);
            workBook.Culture = CultureInfo.GetCultureInfo("en-US");

with no success.

the part where I set the dateformat is

...

propertyInfos.ToList().ForEach(p => {
  if (WriteProperty(p, out displayName)) {
    if (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(DateTime?)) {
       ws.Columns[column].CellFormat.FormatString = "DD-MMM-YYYY";
    }

  ws.GetCell("R" + row + "C" + column, CellReferenceMode.R1C1).CellFormat.Font.Height = 10 * 20;
  ws.GetCell("R" + row + "C" + column, CellReferenceMode.R1C1).CellFormat.WrapText = ExcelDefaultableBoolean.True;

  if (p.PropertyType == typeof(decimal?) || p.PropertyType == typeof(DateTime?)) {
    ws.GetCell("R" + row + "C" + column, CellReferenceMode.R1C1).Value = p.GetValue(element, null);
  } else {
    ws.GetCell("R" + row + "C" + column, CellReferenceMode.R1C1).Value = Convert.ToString(p.GetValue(element, null));
  }

  if (Convert.ToString(p.GetValue(element, null)).Length > colWidth[column - 1]) {
    colWidth[column - 1] = Convert.ToString(p.GetValue(element, null)).Length;
  }

  column++;
  }
});

Parents
  • 21795
    Offline posted

    Hello Tobias,

    Thank you for posting in our forum.

    If I understand you correctly you need to export a DateTime value to a cell in Excel with Infragistics Excel Engine. I have tested this by setting the same format string as yours and it is working fine for me when my machine regional settings are set to German language and Germany as location. When I export a DateTime value the format in Excel is TT-MMM-JJJJ. If I switch back my regional settings to English/ US Excel automatically changes the format in the exported workbook to DD-MMM-YYYY.

    In the attached archive you may find the sample project I have tested with. Please run this sample on your machine and let me know what the result is.

    If this sample is not an accurate demonstration of what you are trying to do, please fill free to modify it and send it back, or if you can provide a small, isolated sample application that demonstrates the behavior you are seeing.

    Looking forward to your reply.

Reply Children