Search code examples
excelimportformattingxtrareport

XtraReports Format Cells for Excel import programmatically


When using Designer, I can edit cell's XlsxFormatString property to "#,##0.00" and result is as expected. When I try to do it programmatically as below nothing is changed:

private XRTableCell CreateCell(int width, string text, bool haveColor, string color, bool isBold, DevExpress.XtraPrinting.BorderSide border, bool IsNumeric)
        {
            //MyWorkaround
            if (IsNumeric)
            {
                if (text.Contains(","))
                {
                    if (text.Length > text.IndexOf(',') + 3)
                        text = text.Remove(text.IndexOf(',') + 3);
                }
            }
            //MyWorkaround end 
            XRTableCell cell = CreateCell(width, text, haveColor, color, isBold);
            cell.Borders = border;
            if (IsNumeric)
                cell.XlsxFormatString = "#,##0.00";
            return cell;
        }

Any suggestions to make it proper?


Solution

  • After some research on Devexpress Documents and after dozens of tries, i come through this solution;

    1. If the output won't be used for some calculations:

      XtraReport myReport = new XtraReport();
      myReport.ExportOptions.Xls.TextExportMode = TextExportMode.Text;
      myReport.ExportOptions.Xlsx.TextExportMode = TextExportMode.Text;
      
    2. If the output cell types matters for some reason:

      float myTextValue;
      if (float.TryParse(textToPrint,out  myValue))
              {
                  string decSeperator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                  if (text.Contains(decSeperator))
                  {
                      if (textToPrint.Length > textToPrint.IndexOf(decSeperator[0]) + 3)
                          textToPrint= textToPrint.Remove(textToPrint.IndexOf(decSeperator[0]) + 3);
                  }
              }
      

    This will trim after 2 digits if it can find seperator in text. It can be changed like round if last digit is higer than 5 etc.

    XtraReports Export Tool will already find out if the text is number or not and it will set the cell type itself

    If someone finds a better solution please feel free to communicate.