【问题标题】:XtraReports Format Cells for Excel import programmatically以编程方式导入 Excel 的 XtraReports 格式单元格
【发布时间】:2015-09-22 12:59:16
【问题描述】:

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

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

【问题讨论】:

    标签: excel import formatting xtrareport


    【解决方案1】:

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

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

    XtraReports 导出工具会判断文本是否为数字,并自行设置单元格类型

    如果有人找到更好的解决方案,请随时交流。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2011-11-22
      • 2014-11-11
      • 2010-11-22
      • 1970-01-01
      相关资源
      最近更新 更多