BandGridview默认导出的是Columns的列头信息,而不是Bands的列头信息,为了实现导出多层行头。代码如下
1 public static void ExportExcel2(DevExpress.XtraGrid.Views.BandedGrid.BandedGridView bandedGridView) 2 { 3 SaveFileDialog saveFileDialog = new SaveFileDialog(); 4 saveFileDialog.Title = "导出Excel"; 5 saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx"; 6 DialogResult dialogResult = saveFileDialog.ShowDialog(); 7 if (dialogResult == DialogResult.OK) 8 { 9 DevExpress.XtraPrinting.XlsxExportOptionsEx op = new DevExpress.XtraPrinting.XlsxExportOptionsEx(); 10 11 op.ExportType = DevExpress.Export.ExportType.WYSIWYG; 12 bandedGridView.OptionsPrint.PrintHeader = false; 13 bandedGridView.OptionsPrint.AutoWidth = false; 14 bandedGridView.ExportToXlsx(saveFileDialog.FileName, op); 15 DevExpress.XtraEditors.XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 16 } 17 }
其中需注意几点:
1.有两种Options:DevExpress.XtraPrinting.XlsxExportOptions和DevExpress.XtraPrinting.XlsxExportOptionsEx,需使用:DevExpress.XtraPrinting.XlsxExportOptionsEx; 2.DevExpress.Export.ExportType的方式有两种:DataAware和WYSIWYG;默认为DataAware,需设置为WYSIWYG;
3.OptionsPrint下的各属性默认值都为true,需设置PrintHeader = false(不显示列头)和AutoWidth = false(不自适应宽度)
时间: 2024-10-25 06:45:36