Dev控件GridControl和TreeList的数据导出操作
导出pdf格式出现乱码时可以在main 方法中添加如下代码:
DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("宋体", 9);
1 /// <summary> 2 /// 数据导出 3 /// </summary> 4 public class DataExport 5 { 6 /// <summary> 7 /// 导出GridConrol中的数据 8 /// </summary> 9 /// <param name="gridControl"></param> 10 public static void GridControlExport(GridControl gridControl) 11 { 12 using (SaveFileDialog saveDialog = new SaveFileDialog()) 13 { 14 saveDialog.Filter = "Excel (2003)(.xls)|*.xls|Excel (2010) (.xlsx)|*.xlsx |RichText File (.rtf)|*.rtf |Pdf File (.pdf)|*.pdf |Html File (.html)|*.html"; 15 if (saveDialog.ShowDialog() != DialogResult.Cancel) 16 { 17 string exportFilePath = saveDialog.FileName; 18 string fileExtenstion = new FileInfo(exportFilePath).Extension; 19 20 switch (fileExtenstion) 21 { 22 case ".xls": 23 gridControl.ExportToXls(exportFilePath); 24 break; 25 case ".xlsx": 26 gridControl.ExportToXlsx(exportFilePath); 27 break; 28 case ".rtf": 29 gridControl.ExportToRtf(exportFilePath); 30 break; 31 case ".pdf": 32 gridControl.ExportToPdf(exportFilePath); 33 break; 34 case ".html": 35 gridControl.ExportToHtml(exportFilePath); 36 break; 37 case ".mht": 38 gridControl.ExportToMht(exportFilePath); 39 break; 40 case ".csv": 41 gridControl.ExportToCsv(exportFilePath); 42 break; 43 default: 44 break; 45 } 46 47 if (File.Exists(exportFilePath)) 48 { 49 try 50 { 51 if (DialogResult.Yes == MessageBox.Show("文件已成功导出,是否打开文件?", "提示", MessageBoxButtons.YesNo)) 52 { 53 System.Diagnostics.Process.Start(exportFilePath); 54 } 55 } 56 catch 57 { 58 String msg = "The file could not be opened." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath; 59 MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 60 } 61 } 62 else 63 { 64 String msg = "The file could not be saved." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath; 65 MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 66 } 67 } 68 } 69 } 70 /// <summary> 71 /// 导出TreeList中的数据 72 /// </summary> 73 /// <param name="treeList"></param> 74 public static void TreeListExport(TreeList treeList) 75 { 76 using (SaveFileDialog saveDialog = new SaveFileDialog()) 77 { 78 saveDialog.Filter = "Excel (2003)(.xls)|*.xls|Excel (2010) (.xlsx)|*.xlsx |RichText File (.rtf)|*.rtf |Pdf File (.pdf)|*.pdf |Html File (.html)|*.html"; 79 if (saveDialog.ShowDialog() != DialogResult.Cancel) 80 { 81 string exportFilePath = saveDialog.FileName; 82 string fileExtenstion = new FileInfo(exportFilePath).Extension; 83 84 switch (fileExtenstion) 85 { 86 case ".xls": 87 treeList.ExportToXls(exportFilePath); 88 break; 89 case ".xlsx": 90 treeList.ExportToXlsx(exportFilePath); 91 break; 92 case ".rtf": 93 treeList.ExportToRtf(exportFilePath); 94 break; 95 case ".pdf": 96 treeList.ExportToPdf(exportFilePath); 97 break; 98 case ".csv": 99 treeList.ExportToCsv(exportFilePath); 100 break; 101 102 default: 103 break; 104 } 105 106 if (File.Exists(exportFilePath)) 107 { 108 try 109 { 110 if (DialogResult.Yes == MessageBox.Show("文件已成功导出,是否打开文件?", "提示", MessageBoxButtons.YesNo)) 111 { 112 System.Diagnostics.Process.Start(exportFilePath); 113 } 114 } 115 catch 116 { 117 String msg = "The file could not be opened." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath; 118 MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 119 } 120 } 121 else 122 { 123 String msg = "The file could not be saved." + Environment.NewLine + Environment.NewLine + "Path: " + exportFilePath; 124 MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 125 } 126 } 127 } 128 } 129 }