/// <summary> /// 导出DataGrid中的数据到Excel文件并打开该文件(带文件保存对话框) /// </summary> /// <param name="dgv"></param> public static void exportToExcelWithDevExpress(DevExpress.XtraGrid.Views.Grid.GridView dgv) { SaveFileDialog sfd = new SaveFileDialog(); sfd.FileName = string.Empty; sfd.Filter = "*.xls|*.xls"; if (sfd.ShowDialog() == DialogResult.OK) { try { //写入Excel文件 dgv.ExportToXls(sfd.FileName); //弹出提示 MessageBox.Show("Excel导出完成!" + sfd.FileName); //打开文件 if (File.Exists(sfd.FileName)) { Process.Start(sfd.FileName); } } catch (Exception ex) { MessageBox.Show("对不起,Excel导出失败!Ex:" + ex.ToString()); } } }
/// <summary> /// 数据导出到Excel /// </summary> /// <param name="paramGridView">要操作的GridView控件</param> /// <param name="paramFilePath">导出文件路径</param> private static void ExportToExcel(DevExpress.XtraGrid.Views.Grid.GridView paramGridView, string paramFilePath) { DevExpress.XtraPrinting.XlsExportOptions myXlsExportOptions = new DevExpress.XtraPrinting.XlsExportOptions(); myXlsExportOptions.ExportHyperlinks = true; myXlsExportOptions.SheetName = "餐饮管理系统"; myXlsExportOptions.ShowGridLines = true; myXlsExportOptions.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value; paramGridView.ExportToXls(paramFilePath, myXlsExportOptions); }
/// <summary> /// hàm này cho phép xuất excel và lưu dữ liệu trong Release /// </summary> /// <param name="v_grv"> tên của gridview</param> /// <param name="name"> tên file mà bạn muốn xuất ra</param> public static void xuat_excel(DevExpress.XtraGrid.Views.Grid.GridView v_grv, string name) { name = name + ".xls"; v_grv.SaveLayoutToXml("tempLayout.xml"); foreach (DevExpress.XtraGrid.Columns.GridColumn col in v_grv.Columns) { col.Visible = true; } v_grv.ExportToXls(name); v_grv.RestoreLayoutFromXml("tempLayout.xml"); }
// // XLS 화일변환 // //------------------------------------------------------------------------------------------------------------- private void simpleXLS_Click(object sender, EventArgs e) { String fileName = ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls"); if (fileName != "") { // gridView1.BestFitColumns(); //XlsExportOptions xo = new XlsExportOptions(); //xo.TextExportMode = TextExportMode.Text; //xo.ShowGridLines = true; //xo.SheetName = "test"; gridView1.OptionsPrint.AutoWidth = false; //gridView1.ExportToXls(fileName, xo); gridView1.ExportToXls(fileName); OpenFile(fileName); } }
/// <summary> /// Hàm xuất excel. /// </summary> /// <param name="grvData"></param> public static void ExportExcel(DevExpress.XtraGrid.Views.Grid.GridView grvData) { string filepath = System.IO.Path.GetTempFileName(); filepath = filepath.Remove(filepath.LastIndexOf('.') + 1); filepath = String.Concat(filepath, "xls"); grvData.OptionsPrint.AutoWidth = false; grvData.OptionsPrint.ExpandAllDetails = false; grvData.OptionsPrint.PrintDetails = true; grvData.OptionsPrint.UsePrintStyles = true; try { grvData.ExportToExcelOld(filepath); } catch { grvData.ExportToXls(filepath); } System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("Excel.exe", String.Format("/r \"{0}\"", filepath)); System.Diagnostics.Process.Start(startInfo); }