/// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (useExcel == true)
            {
                try
                {
                    excelApp.Application.Workbooks.Close();
                    excelApp.Application.Quit();
                    excelApp.Quit();
                    excelBook  = null;
                    excelSheet = null;
                    excelApp   = null;
                    GC.Collect();
                }
                catch
                {
                    throw new Exception("Excel 关闭错误!");
                }
            }

            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
Exemplo n.º 2
0
 public void Dispose()
 {
     try
     {
         if (_mExcelApplication != null)
         {
             Close(false);
             _mExcelApplication.Quit();
             _mExcelApplication = null;
         }
     }
     catch (Exception ex)
     {
         Debug.Write(ex.ToString());
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Replace single scripts depend value sheet
        /// </summary>
        public void DependentValReplace(string source, string file)
        {
            string _sPath_Source  = source;
            string _sPath_Destine = file;
            object oMissing       = System.Reflection.Missing.Value;

            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

            objExl_Source.EnableEvents  = false;
            objExl_Source.DisplayAlerts = false;
            objExl_Source.Visible       = false;

            double[] widthOfScripts = new double[15] {
                84.75, 134.25, 54, 36.75, 127.5, 220.5, 80.25, 99, 103.5, 132.75, 54, 54, 54, 54, 54
            };

            //  objExl_Source.ScreenUpdating = true;

            Excel.Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            //ApplicationClass objExl_Destine = new ApplicationClass();
            //objExl_Destine.EnableEvents = false;
            //objExl_Destine.DisplayAlerts = false;
            //objExl_Destine.Visible = false;
            //  objExl_Destine.ScreenUpdating = true;

            Excel.Workbook objWBook_Destine = objExl_Source.Workbooks.Open(_sPath_Destine, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

            //Workbook objWBook_Source = objExl_Source.Workbooks.Open(_sPath_Source,
            //  0,false,5,oMissing,oMissing,false,XlPlatform.xlWindows,
            //  oMissing,true,false,0,true,false,false);

            //Workbook objWBook_Destine = objExl_Destine.Workbooks.Open(_sPath_Destine,
            //  0, false, 5, oMissing, oMissing, false, XlPlatform.xlWindows,
            //  oMissing, true, false, 0, true, false, false);
            string strSheetName = "Scripts";

            Excel.Worksheet objWSheet_Source = (Excel.Worksheet)objWBook_Source.Worksheets[strSheetName];

            //objWSheet_Source.Copy((Excel.Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count], oMissing);


            //strSheetName = "Action DataValDepend";
            //objWSheet_Source = (Excel.Worksheet)objWBook_Source.Worksheets[strSheetName];

            //objWSheet_Source.Copy((Excel.Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count], oMissing);

            Excel.Range range = objWSheet_Source.get_Range(objWSheet_Source.Cells[1, 1], objWSheet_Source.Cells[900, 20]);
            // range.Value = "123";
            Excel.Worksheet sheet1 = (Excel.Worksheet)objWBook_Destine.Sheets[1];
            Excel.Range     range1 = sheet1.get_Range(sheet1.Cells[1, 1], sheet1.Cells[900, 20]);
            range.Copy(range1);

            for (int i = 1; i <= 15; i++)
            {
                Excel.Range rmgSource  = (Excel.Range)objWSheet_Source.Cells[1, i];
                Excel.Range rmgDestine = (Excel.Range)sheet1.Cells[1, i];
                rmgDestine.EntireColumn.ColumnWidth = rmgSource.EntireColumn.ColumnWidth;
                //rmgDestine.EntireColumn.ColumnWidth = widthOfScripts[i - 1];
                //rmg.EntireColumn.AutoFit();
                //rmg.Width = widthOfScripts[i-1];
                //MessageBox.Show(rmg.Width.ToString());
            }

            foreach (Excel.Worksheet xSheet in objWBook_Source.Worksheets)
            {
                if (xSheet.Name.Equals("Notes") || xSheet.Name.Equals("Note"))
                {
                    strSheetName     = "Notes";
                    objWSheet_Source = (Excel.Worksheet)objWBook_Source.Worksheets[strSheetName];
                    objWSheet_Source.Copy(oMissing, (Excel.Worksheet)objWBook_Destine.Sheets[objWBook_Destine.Sheets.Count]);
                }
            }

            //objWBook_Source.Save();
            objWBook_Destine.Save();
            objWBook_Destine = null;
            objExl_Source.Quit();
            // objExl_Destine.Quit();
        }
Exemplo n.º 4
0
 //**********************************************************************
 /// <summary>
 /// Close the excel
 /// </summary>
 public void close()
 {
     xBook.Save();
     excelMain.Quit();
     excelMain = null;
 }