private void xuatExcel() { string ten_cautruyvan = ""; if (this.lookUpEditCauTruyVan.EditValue.ToString() != "Tên Câu Truy Vấn") { ten_cautruyvan = this.lookUpEditCauTruyVan.Text.ToString(); } SaveFileDialog f = new SaveFileDialog(); f.Filter = "Excel file (*.xlsx)|*.xlsx"; f.FileName = "PhanTichMau14a_" + ten_cautruyvan + "_" + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString(); if (f.ShowDialog() == DialogResult.OK) { CompositeLink complink = new CompositeLink(new PrintingSystem()); PrintableComponentLink link = new PrintableComponentLink(new PrintingSystem()); link.Component = this.gridControlKetQua; link.CreateMarginalHeaderArea += new CreateAreaEventHandler(Link_CreateMarginalHeaderArea); complink.Links.Add(link); //Rename Sheet complink.PrintingSystem.XlSheetCreated += PrintingSystem_XlSheetCreated; complink.CreatePageForEachLink(); XlsxExportOptions options = new XlsxExportOptions(); options.ExportMode = XlsxExportMode.SingleFilePageByPage; complink.ExportToXlsx(f.FileName, options); MessageBox.Show("Xuất Excel Thành Công!"); } }
/// <summary> /// DevExpress通用导出Excel,支持多个控件同时导出在同一个Sheet表 /// eg:ExportToXlsx("",gridControl1,gridControl2); /// 将gridControl1和gridControl2的数据一同导出到同一张工作表 /// </summary> /// <param name="title">文件名</param> /// <param name="panels">控件集</param> public static void ExportToExcel(string title, params IPrintable [] panels) { SaveFileDialog saveFileDialog = new SaveFileDialog( ); saveFileDialog.FileName = title; saveFileDialog.Title = "导出Excel"; saveFileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls"; DialogResult dialogResult = saveFileDialog.ShowDialog( ); if (dialogResult == DialogResult.Cancel) { return; } string FileName = saveFileDialog.FileName; 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(); //建立文档 try { 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 { FileName = FileName.Replace(".", string.Format("({0}).", count)); } count++; } if (FileName.LastIndexOf(".xlsx") >= FileName.Length - 5) { XlsxExportOptions options = new XlsxExportOptions( ); link.ExportToXlsx(FileName, options); } else { XlsExportOptions options = new XlsExportOptions( ); link.ExportToXls(FileName, options); } if (DevExpress.XtraEditors.XtraMessageBox.Show("保存成功,是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { System.Diagnostics.Process.Start(FileName); //打开指定路径下的文件 } } catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message); } }
public static void ExportToExcel(string title, params IPrintable[] panels) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.FileName = title; saveFileDialog.Title = string.Format("{0} Excel", UnitField.Export); saveFileDialog.Filter = "Excel (*.xlsx)|*.xlsx|Excel(*.xls)|*.xls"; if (saveFileDialog.ShowDialog() == DialogResult.Cancel) { return; } string text = saveFileDialog.FileName; PrintingSystem expr_43 = new PrintingSystem(); CompositeLink compositeLink = new CompositeLink(expr_43); expr_43.Links.Add(compositeLink); for (int i = 0; i < panels.Length; i++) { IPrintable printable = panels[i]; compositeLink.Links.Add(ExportToExcelHelper.CreatePrintableLink(printable)); } compositeLink.Landscape = true; try { int num = 1; while (File.Exists(text)) { if (text.Contains(").")) { int startIndex = text.LastIndexOf("(", StringComparison.Ordinal); int length = text.LastIndexOf(").", StringComparison.Ordinal) - text.LastIndexOf("(", StringComparison.Ordinal) + 2; text = text.Replace(text.Substring(startIndex, length), string.Format("({0}).", num)); } else { text = text.Replace(".", string.Format("({0}).", num)); } num++; } if (text.LastIndexOf(".xlsx", StringComparison.Ordinal) >= text.Length - 5) { XlsxExportOptions options = new XlsxExportOptions(); compositeLink.ExportToXlsx(text, options); } else { XlsExportOptions options2 = new XlsExportOptions(); compositeLink.ExportToXls(text, options2); } //if (XtraMessageBox.Show(Face.resources.UnitField.ExportOK, UnitField.SystemMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) if (XtraMessageBox.Show("µ¼³öOK", UnitField.SystemMessage, MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes) { Process.Start(text); } } catch (Exception arg_16F_0) { XtraMessageBox.Show(arg_16F_0.Message); } }