/// <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 基本样式设置