XtraReports Format Cells for Excel 以编程方式导入

XtraReports Format Cells for Excel import programmatically

使用 Designer 时,我可以将单元格的 XlsxFormatString 属性 编辑为“#,##0.00”,结果符合预期。当我尝试按以下方式以编程方式执行时,没有任何改变:

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;
        }

有什么建议可以使它正确吗?

经过对Devexpress Documents的一些研究和数十次尝试,我找到了这个解决方案;

  1. 如果输出不会用于某些计算:

    XtraReport myReport = new XtraReport();
    myReport.ExportOptions.Xls.TextExportMode = TextExportMode.Text;
    myReport.ExportOptions.Xlsx.TextExportMode = TextExportMode.Text;
    
  2. 如果输出单元格类型由于某种原因很重要:

    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);
                }
            }
    

如果它能在文本中找到分隔符,这将 trim 在 2 位数字之后。如果最后一位数字大于 5 等,它可以像圆形一样更改。

XtraReports 导出工具已经确定文本是否为数字,并且会自行设置单元格类型

如果有人找到更好的解决方案欢迎交流