GridControl導出Excel樣式設置

GridControl導出Excel提供了現成的方法ExportToXls,簡單使用方法:

private void barExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.FileName = "導出Excel";
            fileDialog.Title = "導出Excel";
            fileDialog.Filter = "xls文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            { 
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();  
                gridControl1.ExportToXls(fileDialog.FileName, options); 
                if (XtraMessageBox.Show("導出成功,是否打開", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
                    System.Diagnostics.Process.Start(fileDialog.FileName); 
            }
        } 

往往我們在實際工作中使用Gridcontrol實現複雜的數據展示:多表頭,合併單元格,單元格&行自定義背景色和字體、圖片等等。在導出Excel時如果僅使用上述代碼遠不能滿足導出的樣式。下圖GridControl是我將要導出到Excel的

樣式設置:

//1、多表頭我完全使用Bands,隱藏了原Gridview中Column,因此在打印或導出時候需要將GridView的Columns隱藏
bandedGridView1.OptionsPrint.PrintHeader = false;
//2、行高、列寬設置爲GridControl的樣式,而非Excel默認高度和寬度
bandedGridView1.OptionsPrint.AutoWidth = false;
bandedGridView1.OptionsPrint.UsePrintStyles = false;
//3、勾選框,這裏我使用另一種方式展示:ImageComboBox  True-是 Flase-否(自己定義)
DevExpress.XtraPrinting.XlsExportOptions options = new  DevExpress.XtraPrinting.XlsExportOptions();
options.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;//將列表的展示值作爲導出內容,否則導出的就是實際值(這裏就是true false)
options.RawDataMode = false;  

尤爲重要一點:單元格字體樣式和背景色一定要使用gridView1_RowStyle(object sender, RowStyleEventArgs e)或者RowCellStyle這兩個事件去設置,不要使用CustomDrawCell。這樣導出的數據樣式就和GridControl展示的一樣。

導出的整個代碼:

 private void barExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.FileName = "導出Excel";
            fileDialog.Title = "導出Excel";
            fileDialog.Filter = "xls文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                this.column1.ColumnEdit = this.repositoryItemImageComboBox1;//綁定ImageComboBox控件用於導出
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                options.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
                options.RawDataMode = false; 
                bandedGridView1.OptionsPrint.PrintHeader = false;
                bandedGridView1.OptionsPrint.AutoWidth = false;
                bandedGridView1.OptionsPrint.UsePrintStyles = false; 
                gridControl1.ExportToXls(fileDialog.FileName, options);
                this.column1.ColumnEdit = this.repositoryItemCheckEdit1; //導出成功後,綁定回原控件
                if (XtraMessageBox.Show("導出成功,是否打開", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(fileDialog.FileName);
                } 
            }
        }

        private void gridView1_RowStyle(object sender, RowStyleEventArgs e)
        {
            GridView view = sender as GridView;
            T row = view.GetRow(e.RowHandle) as T;
            if (row == null) return;
            e.Appearance.BackColor = Color.Yellow;
        }
 

導出Excel效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章