Пример #1
0
 /// <summary> 获取全局的 Excel 程序 </summary>
 /// <param name="visible"></param>
 /// <returns>获取失败则返回 null</returns>
 public static Excel.Application GetExcelApp(bool visible = false)
 {
     if (_workingApp != null)
     {
         var processId = 0;
         var threadId  = eZstd.API.Windows.GetWindowThreadProcessId(_workingApp.Hwnd, ref processId);
         var pr        = Process.GetProcessById(processId);
         if (pr == null || pr.HasExited)
         {
             _workingApp = null;
         }
         else
         {
             _workingApp.Visible = visible;
             return(_workingApp);
         }
     }
     if (_workingApp == null)
     {
         _workingApp = new Excel.Application {
             Visible = visible
         };
     }
     if (_workingApp == null)
     {
         throw new NullReferenceException($"无法打开 Excel 程序!");
     }
     return(_workingApp);
 }
Пример #2
0
        public static void ExportarExcel(string[,] valores)
        {
            _Excel.Application oExcel;
            _Excel._Workbook   oWB;
            _Excel._Worksheet  oWS;
            _Excel.Range       oRng;


            //Start excel
            oExcel         = new _Excel.Application();
            oExcel.Visible = true;

            //Get a new workbook.
            oWB = (_Excel._Workbook)(oExcel.Workbooks.Add(Missing.Value));
            oWS = (_Excel._Worksheet)oWB.ActiveSheet;

            //Add table headers going cell by cell.
            oWS.Cells[1, 1] = "Objeto";
            oWS.Cells[1, 2] = "Área";
            oWS.Cells[1, 3] = "Perímetro";

            //Format A1:C1 as bold, vertical alignment = center.
            oWS.get_Range("A1", "C1").Font.Bold         = true;
            oWS.get_Range("A1", "C1").VerticalAlignment =
                _Excel.XlVAlign.xlVAlignCenter;


            //Fill A2:C6 with an array of values.
            oWS.get_Range("A2", "C3").Value2 = valores;

            //AutoFit columns A:D.
            oRng = oWS.get_Range("A1", "C1");
            oRng.EntireColumn.AutoFit();

            //Manipulate a variable number of columns for Quarterly Sales Data.
            //DisplayQuarterlySales(oWS);

            //Make sure Excel is visible and give the user control
            //of Microsoft Excel's lifetime.
            oExcel.Visible     = true;
            oExcel.UserControl = true;
        }
Пример #3
0
 /// <returns>成功则返回 true</returns>
 public static bool KillActiveExcelApp(Excel.Application appToKill)
 {
     if (appToKill != null)
     {
         try
         {
             // excelApp.Quit();
             var processId = 0;
             var threadId  = eZstd.API.Windows.GetWindowThreadProcessId(appToKill.Hwnd, ref processId);
             var pr        = Process.GetProcessById(processId);
             pr.Kill();
             //
             if (appToKill.Equals(_workingApp))
             {
                 _workingApp = null;
             }
         }
         catch (Exception)
         {
             return(false);
         }
     }
     return(true);
 }