Example #1
0
        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);
        }