• Dev GridControl数据导出格式问题


    环境:DevExpress9.3,Vs2008

    DevExpress的GridControl提供方便的数据导出到Excel功能,导出中用户可以根据GridControl的格式进行导出(ExportToXls(Stream stream)方法)。

    1、如果表中存在一列为字符类型,但是存放的为数字,那么按照GridControl的格式导出到Excel中便会产生"将存储为文本的数字转换为数字"的问题提示,必须在Excel中把这些值转化为数字,才可进行计算等。

    对于这种方式的处理方法很简单,只要把GridControl绑定的数据源的这一列的数据类型修改为数值类型即可。

    image

    2、我手动对存储的数字进行了处理,对整数采用千分位显示(intValue.ToString("###,###")方法),对小数采用千分位两位小数显示,不足两位小数的用0补足(String.Format("{0:N}", doubleValue)方法或者String.Format("{0:N2}",doubleValue)方法)。这种方式已经将存储的数字转化为字符串了,如果该列数值类型,则会导致赋值错误,因此列只可以是字符类型,同样导致按照GridControl的格式导出到Excel中便会产生"将存储为文本的数字转换为数字"的问题。

    对于这中方法,我们可以通过GridControl自带的数据格式进行解决。下面可以通过两种方法进行实现。

    (1)通过代码进行实现

    对于整数:

          gridViewTest.Columns[i].DisplayFormat.FormatType = FormatType.Numeric;
                gridViewTest.Columns[i].DisplayFormat.FormatString = "#,###";

    对于小数:

          gridViewTest.Columns[i].DisplayFormat.FormatType = FormatType.Numeric;
                gridViewTest.Columns[i].DisplayFormat.FormatString = "#,###.00";

    (1)通过设计窗体进行实现

    对于整数:

    image

    对于小数:

    image

    下面附导出到Excel的代码实现:

       1: private void btnExport_Click(object sender, EventArgs e)
       2: {
       3:     saveFileDialog1.FileName = "";
       4:     saveFileDialog1.Filter = "Excel文件(.xls)|*.xls";
       5:     saveFileDialog1.FileName = "测试文档";
       6:     saveFileDialog1.OverwritePrompt = false; //已存在文件是否覆盖提示
       7:     if (saveFileDialog1.ShowDialog() != DialogResult.OK)
       8:         return;
       9:     //已存在文件是否覆盖提示
      10:     while (System.IO.File.Exists(saveFileDialog1.FileName) && 
      11:         DevExpress.XtraEditors.XtraMessageBox.Show("该文件名已存在,是否覆盖?", 
      12:         "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
      13:     {
      14:         if (saveFileDialog1.ShowDialog() != DialogResult.OK)
      15:             return;
      16:     }
      17:     if (saveFileDialog1.FileName != "")
      18:     {
      19:         try
      20:         {
      21:             System.IO.FileStream fs =
      22:                (System.IO.FileStream)saveFileDialog1.OpenFile();
      23:             this.gridControlTest.ExportToXls(fs);
      24:             fs.Close();
      25:             DevExpress.XtraEditors.XtraMessageBox.Show("数据导出成功!", "提示");
      26:         }
      27:         catch (Exception ex)
      28:         {
      29:             if (ex.Message.Contains("正由另一进程使用"))
      30:             {
      31:                 DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!文件正由另一个程序占用!", "提示");
      32:             }
      33:             else
      34:                 DevExpress.XtraEditors.XtraMessageBox.Show("数据导出失败!数据量过大,请分别统计再导出!", "提示");
      35:         }
      36:     }
      37: }
  • 相关阅读:
    "科林明伦杯"哈理工第九届——分布式服务(概率期望+思维)
    Nim游戏——简单博弈论
    acwing 1252搭配购买(并查集 + 01背包 )
    [LeetCode] 67. Add Binary
    [LeetCode] 415. Add Strings
    [LeetCode] 43. Multiply Strings
    [LeetCode] 412. Fizz Buzz
    [LeetCode] 201. Bitwise AND of Numbers Range
    [LeetCode] 389. Find the Difference
    [LeetCode] 326. Power of Three + 342. Power of Four
  • 原文地址:https://www.cnblogs.com/Jingkunliu/p/3723185.html
Copyright © 2020-2023  润新知