博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
导出信息到Excel,如何将DevExpress的Gridcontrol导出到Excel 源码。
阅读量:5959 次
发布时间:2019-06-19

本文共 2706 字,大约阅读时间需要 9 分钟。

需要添加引用:System.Windows.Forms

 如何将DevExpress的Gridcontrol导出到Excel 源码。

 #region 导出已支付信息

  1.   private void btnDCYZF_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl3.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

2、 public override void ImportExcel()

        {
            if (XtraMessageBox.Show("确定要将本页面数据导入到Excel内?") == DialogResult.OK)
            {
                try
                {
                    if (this.DataSourceTable == null || this.DataSourceTable.Rows.Count == 0)
                    {
                        XtraMessageBox.Show("没有数据要导出!");
                        return;
                    }
                    this.SaveFileDialog.Filter = "Excel文件(*.xls)|*.xls";
                    if (this.SaveFileDialog.ShowDialog(this) == DialogResult.OK)
                    {
                        BaseFormFactory.ProcessFactory.Show(this, "数据导出开始");
                        ExportTo(new DevExpress.XtraExport.ExportXlsProvider(this.SaveFileDialog.FileName));
                    }

                                  }

                catch (Exception vErr)
                {
                    XtraMessageBox.Show("导出数据失败!错误源:" + vErr.Message);
                }
                finally
                {
                    BaseFormFactory.ProcessFactory.Close();
                    XtraMessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        private void ExportTo(DevExpress.XtraExport.IExportProvider provider)
        {
            DevExpress.XtraGrid.Export.BaseExportLink link = this.gridData.gridView2.CreateExportLink(provider);
            (link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
            link.ExportTo(true);
            provider.Dispose();
        }

 

导出信息到Excel详解:

  1. SaveFileDialog saveFileDialog = new SaveFileDialog();  
  2. //打开的文件选择对话框上的标题  
  3. saveFileDialog.Title = "请选择文件";  
  4. //设置文件类型  
  5. saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";  
  6. //设置默认文件类型显示顺序  
  7. saveFileDialog.FilterIndex = 1;  
  8. //保存对话框是否记忆上次打开的目录  
  9. saveFileDialog.RestoreDirectory = true;  
  10. //设置是否允许多选  
  11. saveFileDialog.Multiselect = false;  
  12. //按下确定选择的按钮  
  13. if (saveFileDialog.ShowDialog() == DialogResult.OK)  
  14. {  
  15.     //获得文件路径  
  16.     string localFilePath = saveFileDialog.FileName.ToString();  
  17.     //获取文件路径,不带文件名  
  18.     //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));  
  19.     //获取文件名,带后缀名,不带路径  
  20.     string fileNameWithSuffix = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);  
  21.     //去除文件后缀名  
  22.     string fileNameWithoutSuffix = fileNameWithSuffix.Substring(0, fileNameWithSuffix.LastIndexOf("."));  
  23.     //在文件名前加上时间  
  24.     string fileNameWithTime = DateTime.Now.ToString("yyyy-MM-dd ") + fileNameExt;  
  25.     //在文件名里加字符  
  26.     string newFileName = localFilePath.Insert(1, "Tets");  

 

fileDialog.Filter = "Excel 文件(*.xls)|*.xls|Excel 文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";

转载于:https://www.cnblogs.com/liwenty/p/8708249.html

你可能感兴趣的文章