/// <summary> /// 匯出到Excel /// </summary> /// <param name="gridControl">GridControl</param> /// <param name="fileNameTitle">匯出到Excel的Sheet檔名稱</param> public static void ExportToExcel(this GridControl gridControl, string fileNameTitle) { var fileName = SaveAs(fileNameTitle); XlsExportOptions options = new XlsExportOptions(); options.Suppress256ColumnsWarning = true; options.Suppress65536RowsWarning = true; options.TextExportMode = TextExportMode.Text; options.SheetName = fileNameTitle; var gridView = (GridView)gridControl.Views[0]; gridView.AppearancePrint.Row.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap; gridView.OptionsPrint.AutoWidth = false; gridView.AppearancePrint.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; gridControl.ExportToXls(fileName, options); if (MessageCommon.ShowQueInf("儲存成功,是否開啟檔案?") == DialogResult.Yes) System.Diagnostics.Process.Start(fileName);//開啟指定路徑下的檔案 } /// <summary> /// 獲取使用者檔案儲存對話方塊選擇的完整檔案路徑 /// </summary> /// <param name="fileNameTitle"></param> /// <returns></returns> private static string SaveAs(string fileNameTitle) { string filename = fileNameTitle + DateTime.Now.ToString("yyMMddhhmmss") + new Random().Next(100, 999); return FileDialogHelper.Save("匯出到 Microsoft Excel Document", "Excel檔案(*.xls)|*.xls", filename, ""); }
DevExpress GridView 匯出到Excel 自動調整列寬 設定 GridView 的 OptionPrint.AutoWidth = false
DevExpress.XtraGrid.Views.Grid.GridView gdv #region GridView屬性設定 //行號所在列的寬度 gdv.IndicatorWidth = 40; //頂部皮膚 可用於分組 gdv.OptionsView.ShowGroupPanel = false; //顯示底部皮膚 可用於展示統計 gdv.OptionsView.ShowFooter = true; //奇數行的效果設定是否可用 gdv.OptionsView.EnableAppearanceEvenRow = true; //失去焦點時 是否保留行選中效果 gdv.OptionsSelection.EnableAppearanceHideSelection = false; //是否顯示焦點單元格樣式 gdv.OptionsSelection.EnableAppearanceFocusedCell = false; //只讀 gdv.OptionsBehavior.ReadOnly = true; //不可編輯 若設定不可編輯 會導致表格中增加的按鈕的單擊事件不可用 gdv.OptionsBehavior.Editable = false; //行選中 gdv.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus; //邊框 //gdv.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder; //關閉列右鍵選單 gdv.OptionsMenu.EnableColumnMenu = false; //列字型對齊方式 gdv.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //列字型設定 gdv.Appearance.HeaderPanel.Font = new System.Drawing.Font("微軟雅黑", 14F, FontStyle.Bold, GraphicsUnit.Pixel); //行字型對齊方式 gdv.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //奇數行背景色 gdv.Appearance.EvenRow.BackColor = Color.FromArgb(228, 243, 255); //焦點行背景色 gdv.Appearance.FocusedRow.BackColor = Color.FromArgb(0, 153, 255); //焦點行字型顏色 gdv.Appearance.FocusedRow.ForeColor = Color.White; //FooterPanel字型對齊方式 gdv.Appearance.FooterPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; //行字型 gdv.Appearance.Row.Font = new System.Drawing.Font("微軟雅黑", 14F, FontStyle.Regular, GraphicsUnit.Pixel); //匯出相關設定 gdv.AppearancePrint.Row.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap; gdv.OptionsPrint.AutoWidth = false; gdv.AppearancePrint.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; #endregion
參考:http://www.cnblogs.com/LikeHeart/p/6729387.html Dev中GridControl的GridView 基本樣式設定