C # use Com to parse the Excel file, delete the Excel file, and close the Excel process completely (company practical project experience)

Source: Internet
Author: User

Yesterday, the customer asked to import data from the Excel file to the program. I was depressed and never touched the Excel file to import the data. So Google gave me a try, after about half an hour, I found two methods, one of which is Excel.. Net Com component to access. net ADO to access, consider it, ready to use Com to parse it. First, add a reference in the program, Microsoft Office InterOp Excel. After adding the reference, you can introduce the Excel space.
Using Excel = Microsoft. Office. Interop. Excel, but after the call is completed, the Excel process cannot be completely closed. The ghost program cannot be released immediately, so an exception is reported and the file is occupied. I checked it on the Internet and found the method for terminating the process. I used the method of garbage collection, System. GC, which can be forcibly recycled, but cannot be killed, because they may have no harm. If two users import data at the same time, one user first enters the process and kills the process, another user fails to import the file, but deleting the file is depressing. When I debug the file, it is okay to delete the file. When I delete the breakpoint, the file will be deleted, I found that the file may be released at a time. So, when I deleted the file, I blocked the current thread for 0.5 seconds. OK. No error is returned for deletion. Haha, really nice ~~~

This is part of the code

  1. Excel. Application m_xlsApp = null;
  2. Excel. Workbook m_Workbook = null;
  3. Excel. Worksheet m_Worksheet = null;
  4. Try
  5. {
  6. Object objOpt = System. Reflection. Missing. Value;
  7. M_xlsApp = new Excel. Application ();
  8. M_Workbook = require (s_FileName, objOpt, objOpt );
  9. M_Worksheet = (Excel. Worksheet) m_Workbook.Worksheets.get_Item (sheetIndex );
  10. DataRow newRow;
  11. For (int j = 2; j <= m_Worksheet.UsedRange.Rows.Count; j ++)
  12. {
  13. NewRow = dtTemp. NewRow ();
  14. For (int I = 1; I <= m_Worksheet.UsedRange.Columns.Count; I ++)
  15. {
  16. If (Excel. Range) (m_Worksheet.Cells [j, I])! = Null & (Excel. Range) (m_Worksheet.Cells [j, I]). Text. ToString ()! = "")
  17. {
  18. NewRow [I-1] = (Excel. Range) (m_Worksheet.Cells [j, I]). Value2.ToString ();
  19. }
  20. }
  21. DtTemp. Rows. Add (newRow );
  22. }
  23. }
  24. Catch (Exception exc)
  25. {
  26. Alert ("Import failed ~! ");
  27. }
  28. Finally
  29. {
  30. M_Worksheet = null;
  31. M_Workbook = null;
  32. M_xlsapp.quit ();
  33. Int Generation = system. gc. getgeneration (m_xlsapp );
  34. M_xlsapp = NULL;
  35. System. gc. Collect (generation );
  36. }
  37. Return dttemp;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.