1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
public static void ExportToExcel(string title, bool isPageForEachLink, string sheetName, XlSheetCreatedEventHandler handler, params DevExpress.XtraPrinting.IPrintable[] printables) { SaveFileDialog saveFileDialog = new SaveFileDialog() { FileName = title, Title = "导出Excel", Filter = "Excel文件(*.xlsx)|*.xlsx|Excel文件(*.xls)|*.xls" }; DialogResult dialogResult = saveFileDialog.ShowDialog(); if (dialogResult == DialogResult.Cancel) return; string fileName = saveFileDialog.FileName; DevExpress.XtraPrintingLinks.CompositeLink link = new DevExpress.XtraPrintingLinks.CompositeLink(new DevExpress.XtraPrinting.PrintingSystem()); foreach (var item in printables) { var plink = new DevExpress.XtraPrinting.PrintableComponentLink() { Component = item }; link.Links.Add(plink); }
if (handler != null) { link.PrintingSystem.XlSheetCreated += handler; }
if (isPageForEachLink) { link.CreatePageForEachLink(); } if (string.IsNullOrEmpty(sheetName)) sheetName = "Sheet"; try { int count = 1; while (System.IO.File.Exists(fileName)) { if (fileName.Contains(").")) { int start = fileName.LastIndexOf("(", StringComparison.Ordinal); int end = fileName.LastIndexOf(").", StringComparison.Ordinal) - fileName.LastIndexOf("(", StringComparison.Ordinal) + 2; fileName = fileName.Replace(fileName.Substring(start, end), $"({count})."); } else { fileName = fileName.Replace(".", $"({count})."); } count++; }
if (fileName.LastIndexOf(".xlsx", StringComparison.Ordinal) >= fileName.Length - 5) { DevExpress.XtraPrinting.XlsxExportOptions options = new DevExpress.XtraPrinting.XlsxExportOptions() { SheetName = sheetName }; if (isPageForEachLink) options.ExportMode = DevExpress.XtraPrinting.XlsxExportMode.SingleFilePageByPage; link.ExportToXlsx(fileName, options); } else { DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions() { SheetName = sheetName }; if (isPageForEachLink) options.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFilePageByPage; 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); } }
private static void PrintingSystem_XlSheetCreated(object sender, XlSheetCreatedEventArgs e) { if (e.Index == 0) e.SheetName = "基本信息"; else if (e.Index == 1) e.SheetName = "学业信息"; else if (e.Index == 2) e.SheetName = "工作经历"; else if (e.Index == 3) e.SheetName = "家庭成员"; }
|