The preceding chapter describes how to use automation to Operate Excel. Each time new excel. Application () is used. In this way, each time you access excel, you need to start an Excel instance, which takes a lot of time to start Excel (in my environment, it usually takes 15 seconds to start Excel ). This can be optimized by declaring the Excel. application type variable as static. In this way, in addition to starting Excel for the first time, other Excel instances will not be started in subsequent operations. In this way, there will be only one Excel instance, saving space, it also saves "cold" time to start excel. In my development environment, you can reduce the original 22 seconds to 7.3-7.5 seconds, which is equivalent to saving about 2/3 of the time!
Implementation exampleCodeAs follows:
Static excel. application _ excelapp = new excel. application ();... public void finalize () // The {_ excelapp to be destroyed at the end. quit (); releasecomobject (_ excelapp); _ excelapp = NULL; GC. collect (0 );}
Do not manually kill excel. EXE in the memory (for example, use the task manager to kill it). Otherwise, the next time you call the Excel Code, an error will occur because the object has crashed.