//添加记录菜单响应 public override void OnClick() { if (m_Hook.GridCtrl == null) { return; } FaceControl pFaceControl = (FaceControl)m_Hook.MainUserControl; DataGridView pGridControl = m_Hook.GridCtrl; if (pFaceControl.getEditable() == false) { return; } string Tablename = ""; //获取数据库连接串和表名 Tablename = pFaceControl.m_TableName; SaveFileDialog pOpenFileDlg = new SaveFileDialog(); pOpenFileDlg.Title = "设置导出Excel文件的名称"; pOpenFileDlg.Filter = "Excel文件(*.xls)|*.xls"; if (pOpenFileDlg.ShowDialog() != DialogResult.OK) { return; } string strFileName = pOpenFileDlg.FileName; //加进度条 chulili 2013-01-11 SysCommon.CProgress vProgress = new SysCommon.CProgress("进度条"); vProgress.EnableCancel = true; vProgress.EnableUserCancel(true); vProgress.ShowDescription = true; vProgress.FakeProgress = false; vProgress.TopMost = true; bool bRes = ModExcel.ExportTableToExcel(Plugin.ModuleCommon.TmpWorkSpace, Tablename, strFileName, vProgress); vProgress.Close(); if (bRes) { MessageBox.Show("导出成功!"); } //ModDBOperate.ExportTableToExcel(Tablename, strFileName); if (this.WriteLog) { Plugin.LogTable.Writelog(Caption);//xisheng 2011.07.09 增加日志 } }
public static bool ExportTableToExcel(IWorkspace pWorkSpace, string strTableName, string ExcelFileName, SysCommon.CProgress vProgress) { if (pWorkSpace == null) { return(false); } if (strTableName == "") { return(false); } if (ExcelFileName == "") { return(false); } IFeatureWorkspace pFeaWorkSpace = pWorkSpace as IFeatureWorkspace; Microsoft.Office.Interop.Excel.Application excel = null; Microsoft.Office.Interop.Excel.Workbook wb = null; //建立Excel对象 excel = new Microsoft.Office.Interop.Excel.Application(); wb = excel.Application.Workbooks.Add(true); excel.Visible = false; wb.Application.ActiveWindow.Caption = strTableName; int iRow = 1; ITable pTable = null; try { pTable = pFeaWorkSpace.OpenTable(strTableName); } catch (System.Exception ex) { } if (pTable == null) { return(false); } WriteTableStruToExcel(excel, pTable, iRow); iRow = iRow + 1; ICursor pCursor = null; pCursor = pTable.Search(null, false); int RowCnt = pTable.RowCount(null) + 1; if (vProgress != null) { vProgress.MaxValue = RowCnt; vProgress.ShowProgress(); vProgress.SetProgress("正在导出记录..."); } if (pCursor == null) { return(false); } IRow pRow = pCursor.NextRow(); while (pRow != null) { if (vProgress != null) { if (vProgress.UserAskCancel) { wb.Saved = false; excel.Workbooks.Close(); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); ModExcel.Kill(excel); GC.Collect(); return(false); } vProgress.PerformStep(); } WriteRowToExcel(excel, pRow, iRow); pRow = pCursor.NextRow(); iRow = iRow + 1; } try { wb.SaveAs(ExcelFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } catch (System.Exception ex) { System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); ModExcel.Kill(excel); GC.Collect(); return(false); } excel.Workbooks.Close(); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(wb); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); ModExcel.Kill(excel); GC.Collect(); return(true); }