• 将GridControl和ChartControl导出到Excel的同个Sheet页中


    DevExpress 10 版本

    #region 导出 private void btnExport_Click(object sender, EventArgs e) { ExportToExcel(gc, chartControl1); } /// <summary> /// 创建打印Componet /// </summary> /// <param name="printable"></param> /// <returns></returns> PrintableComponentLink CreatePrintableLink(IPrintable printable) { ChartControl chart = printable as ChartControl; if (chart != null) chart.OptionsPrint.SizeMode = DevExpress.XtraCharts.Printing.PrintSizeMode.Stretch; PrintableComponentLink printableLink = new PrintableComponentLink() { Component = printable }; return printableLink; } /// <summary> /// 导出Excel,支持多个控件同时导出在同一个Sheet表 /// </summary> /// <param name="panels">控件集</param> public void ExportToExcel(params IPrintable[] panels) { string fileName = string.Empty; if (gv.RowCount > 0) { try { this.saveFileDialog.FileName = "XXX -- " + DateTime.Now.Date.ToString("yyyy-MM-dd"); // 默认文件名 this.saveFileDialog.Title = "Export Excel"; this.saveFileDialog.DefaultExt = "xlsx"; // 扩展名 this.saveFileDialog.Filter = "Excel file(*.xlsx)|*.xlsx|Excel file(*.xls)|*.xls"; this.saveFileDialog.OverwritePrompt = false; // 文件存在是是否提示覆盖 if (this.saveFileDialog.ShowDialog() == DialogResult.OK) { fileName = saveFileDialog.FileName; this.gv.RowStyle -= gv_RowStyle; //去除选中行样式 PrintingSystem ps = new PrintingSystem(); CompositeLink link = new CompositeLink(ps); ps.Links.Add(link); foreach (IPrintable panel in panels) { link.Links.Add(CreatePrintableLink(panel)); } link.Landscape = true;//横向 //判断是否有标题,有则设置 link.CreateDocument(); //建立文档 //ps.PreviewFormEx.Show();//进行预览 //ps.Print(); //打印 try { if (string.IsNullOrEmpty(fileName)) { return; } int count = 1; //在重复名称后加(序号) while (File.Exists(fileName)) { if (fileName.Contains(").")) { int start = fileName.LastIndexOf("("); int end = fileName.LastIndexOf(").") - fileName.LastIndexOf("(") + 2; fileName = fileName.Replace(fileName.Substring(start, end), string.Format("({0}).", count)); } else { int find = fileName.LastIndexOf("."); fileName = fileName.Insert(find, string.Format("({0})", count)); } count++; } if (fileName.LastIndexOf(".xlsx") >= fileName.Length - 5) { XlsxExportOptions options = new XlsxExportOptions(); options.SheetName = "XXX"; //修改sheet页命名 link.PrintingSystem.ExportToXlsx(fileName, options); } else { XlsExportOptions options = new XlsExportOptions(); options.SheetName = "XXX"; //修改sheet页命名 link.PrintingSystem.ExportToXls(fileName, options); } if (DevExpress.XtraEditors.XtraMessageBox.Show("保存成功,是否打开文件?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { System.Diagnostics.Process.Start(fileName);//打开指定路径下的文件 } } catch (Exception) { //提示... } this.gv.RowStyle += gv_RowStyle; } } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message); } } else { //提示... return; } } #endregion

     相关链接:https://q.cnblogs.com/q/88667/#a_203370

  • 相关阅读:
    The 4 Most Important Skills for a Software Developer
    Youth is not a time of life, it is a state of mind——青春不是一段年华,而是一种心境
    英雄所见略同——每个人都有的一套价值体系观念
    28法则————10分钟休息胜过半小时努力
    离职员工心声
    员工必备素质、能力——职场精英
    安卓sqlite数据库的使用
    安卓adb命令的使用
    windows使用命令行,提高效率
    命令行编译java文件(含第三方jar包)
  • 原文地址:https://www.cnblogs.com/719468186-QAQ/p/6961279.html
Copyright © 2020-2023  润新知