DEV GridControl导出到Excel或打印
Posted helloEveryBody0821
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DEV GridControl导出到Excel或打印相关的知识,希望对你有一定的参考价值。
//方法1
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(); gridControl1.ExportToXls(fileDialog.FileName); DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
//方法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(); }
打印
PrintingSystem print = new DevExpress.XtraPrinting.PrintingSystem(); PrintableComponentLink link = new PrintableComponentLink(print); print.Links.Add(link); link.Component = (currentControl as GridControl);//这里可以是可打印的部件 PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter; phf.Header.Content.Clear(); phf.Header.Content.AddRange(new string[] { "", "", "" }); phf.Header.Font = new System.Drawing.Font("宋体", 14, System.Drawing.FontStyle.Bold); phf.Header.LineAlignment = BrickAlignment.Center; link.CreateDocument(); //建立文档 print.PreviewFormEx.Show();//进行预览
以上是关于DEV GridControl导出到Excel或打印的主要内容,如果未能解决你的问题,请参考以下文章
[DevExpress]DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表
[DevExpress]DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表
Dev控件学习-GridControl中的BandGridview导出多层行头操作
WPF DEV gridcontrol当前项的数据导出为mdb文件
C# winform DevExpress GridControl 大批量数据(9万行+4百列)导出Excel报错,内存溢出,怎么办?