1 /// <summary> 2 /// 导出到Excel文件 3 /// </summary> 4 /// <param name="gridView">数据网格</param> 5 public static void ExportToExcel(GridView gridView) /////////////////////////////////////// 6 { 7 ExportToExcel_Plus(gridView, ""); 8 } 9 10 /// <summary> 11 /// 导出到Excel文件 12 /// </summary> 13 /// <param name="gridView">数据网格</param> 14 /// <param name="filename"></param> 15 public static void ExportToExcel(GridView gridView, string fileName)///////////////////////////////////////// 16 { 17 ExportToExcel_Plus(gridView, fileName); 18 } 19 20 private static void ExportToExcel_Plus(GridView gridView, string fileName)//////////////////////////////////////////// 21 { 22 if (gridView.RowCount == 0) 23 { 24 MsgBox.ShowOK("没有数据需要导出。请先检索出数据,然后导出。"); 25 return; 26 } 27 //userright ur = new userright(); 28 //if (!ur.GetUserRightDetail("f4")) 29 //{ 30 // MsgBox.ShowOK("没有数据导出权限。如果确实需要,请联系公司相关负责人员给您授权!"); 31 // return; 32 //} 33 Cursor currentCursor = Cursor.Current; 34 Cursor.Current = Cursors.WaitCursor; 35 36 SaveFileDialog sfd = new SaveFileDialog(); 37 sfd.Title = "导出到Microsoft Excel文件【注意:不能覆盖已存在的文件】"; 38 sfd.Filter = "Microsoft Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx"; 39 sfd.DefaultExt = "xls"; 40 sfd.FileName = fileName.Trim() == "" ? "" : fileName; 41 GridColumn gc = GetGridViewColumn(gridView, "billstate");//获取状态列 42 43 if (sfd.ShowDialog() == DialogResult.OK) 44 { 45 try 46 { 47 GridViewExportLink gvLink = gridView.CreateExportLink(new ExportXlsProvider(sfd.FileName)) as GridViewExportLink; 48 gvLink.ExportAll = true; //true 导出所有 false 导出选中行 49 if (gc != null && gc.Visible) gvLink.ExportCellsAsDisplayText = true; 50 else gvLink.ExportCellsAsDisplayText = false; 51 //if (commonclass.reptitle.Contains("圣安") || commonclass.reptitle.Contains("华泉龙")) 52 // gvLink.ExportCellsAsDisplayText = true; 53 54 //gvLink.ExportAppearance.Row.BackColor = Color.White; 55 //gvLink.ExportAppearance.EvenRow.BackColor = Color.White; 56 57 gvLink.ExportTo(true); 58 Cursor.Current = currentCursor; 59 60 if (MsgBox.ShowYesNo("导出成功!是否打开文件?\r\n\r\n" + sfd.FileName) == DialogResult.Yes) 61 { 62 try 63 { 64 System.Diagnostics.Process process = new System.Diagnostics.Process(); 65 process.StartInfo.FileName = sfd.FileName; 66 process.StartInfo.Verb = "Open"; 67 process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; 68 process.Start(); 69 } 70 catch 71 { 72 MsgBox.ShowOK("打开失败。您的系统中没有合适的程序打开该文件!"); 73 } 74 } 75 } 76 catch (IOException) 77 { 78 MsgBox.ShowOK("导出失败!\r\n文件已打开,无法覆盖!"); 79 } 80 catch (Exception ex) 81 { 82 MsgBox.ShowOK("导出失败:\r\n" + ex.Message); 83 } 84 } 85 }
导出GridView到Exel
猜你喜欢
转载自www.cnblogs.com/JvYouQing/p/10290779.html
今日推荐
周排行