using System; using System.Windows.Forms; using System.Diagnostics; namespace Xc_db_rygl ... { /**//// <summary> /// ExportDataGrid 的摘要说明。 /// </summary> public class ExportDataGrid ...{ public DataGrid ExportGrid; public Form ParentWindow=null; /**//// <summary> /// made by 江边孤鸟 who's email is jbgh608@163.com /// </summary> /// <param name="parentWindow">父亲窗口</param> /// <param name="grid">要导出的DataGrid</param> public ExportDataGrid(Form parentWindow,DataGrid grid) ...{ ExportGrid=grid; ParentWindow= parentWindow; SaveFileDialog(); } 保存对话框#region 保存对话框 private void SaveFileDialog() ...{ string localFilePath,fileNameExt,newFileName,FilePath; SaveFileDialog saveFileDialog1 = new SaveFileDialog ( ) ; saveFileDialog1.Filter= " txt files(*.txt)|*.txt|All files(*.*)|*.*" ; saveFileDialog1.FilterIndex = 2 ; saveFileDialog1.RestoreDirectory = true ; if ( saveFileDialog1.ShowDialog ( ) == DialogResult.OK ) ...{ localFilePath=saveFileDialog1.FileName.ToString(); fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("/") + 1); FilePath= localFilePath.Substring(0,localFilePath.LastIndexOf("/") ); newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt; this.ParentWindow.Cursor=new Cursor(Constant.ApplicationDirectory+"/wait.cur"); //this.ParentWindow.Cursor=PublicStaticFun.GetCursor("Xc_db_rygl.wait.cur",GetType()); ExportExcel(FilePath+"/"+newFileName,fileNameExt); } } #endregion 导出Excel#region 导出Excel public bool ExportExcel(string FilePath,string p_ReportName) ...{ if ( this.ExportGrid.TableStyles.Count == 0 ) return false; DataGridTableStyle ts = this.ExportGrid.TableStyles[0]; // 创建Excel对象 Excel.Application xlApp = new Excel.ApplicationClass(); if ( xlApp == null ) ...{ MessageBox.Show("Excel无法启动"); return false; } // 创建Excel工作薄 Excel.Workbook xlBook = xlApp.Workbooks.Add(true); Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1]; // 设置标题 Excel.Range range = xlSheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,ts.GridColumnStyles.Count]); range.MergeCells = true; xlApp.ActiveCell.FormulaR1C1 = p_ReportName; xlApp.ActiveCell.Font.Size = 20; xlApp.ActiveCell.Font.Bold = true; xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter; // 列索引,行索引,总列数,总行数 int colIndex = 0; int RowIndex = 0; int colCount = ts.GridColumnStyles.Count; int RowCount = this.ParentWindow.BindingContext[this.ExportGrid.DataSource,this.ExportGrid.DataMember].Count; // 创建缓存数据 object[,] objData = new object[RowCount + 1, colCount ]; // 获取列标题 foreach(DataGridColumnStyle cs in ts.GridColumnStyles) ...{ objData[RowIndex,colIndex++] = cs.HeaderText; } // 获取数据 for(RowIndex =1;RowIndex<=RowCount;RowIndex++) ...{ for(colIndex=0;colIndex < colCount;colIndex++) ...{ objData[RowIndex,colIndex] = this.ExportGrid[RowIndex-1,colIndex].ToString(); } Application.DoEvents(); } // 写入Excel xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true; range = xlSheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount+2,colCount]); range.Value2 = objData; // 保存 try ...{ xlApp.Cells.EntireColumn.AutoFit(); xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter; xlApp.Cells.HorizontalAlignment = Excel.Constants.xlCenter; //xlApp.Visible = true; xlBook.Saved = true; xlBook.SaveCopyAs(FilePath + ".xls"); MessageBox.Show("导出成功1!"); this.ParentWindow.Cursor=Cursors.Default; } catch ...{ MessageBox.Show("保存出错,请检查!"); return false; } finally ...{ xlApp.Quit(); GC.Collect(); KillProcess("excel") ; } return true; } #endregion 杀死进程#region 杀死进程 private void KillProcess(string processName) ...{ System.Diagnostics.Process myproc= new System.Diagnostics.Process(); //得到所有打开的进程 try ...{ foreach (Process thisproc in Process.GetProcessesByName(processName)) ...{ thisproc.Kill(); } } catch(Exception Exc) ...{ throw new Exception("",Exc); } } #endregion }}