Excel operation-delete a worksheet

Source: Internet
Author: User

After studying Excel for a while, I also know that the worksheet can be deleted by calling the delete method of the _ worksheet object. However, I tried it many times but it failed. After repeated tests, you must clear the data in the worksheet before deleting the worksheet. Some people say no. I think it may be the difference between Office versions. I must use office2003, and 2007 may be unnecessary. Paste the code out, and the method is correct. Int cioexcel: deleteworksheet (cstring strfilepath, cstring strsheetname) {int nretval = 0; // open the file and obtain the activated sheet lpdispatch lpdisp = NULL; colevariant covoptional, vt_error); // open the file lpdisp = m_oworkbooks.open (strfilepath, _ variant_t (vtmissing ), _ variant_t (vtmissing), _ variant_t (vtmissing), _ variant_t (vt Missing), _ variant_t (vtmissing), _ variant_t (vtmissing ), _ variant_t (vtmissing); // obtain the active workbook (workbook) m_oworkbook.attachdispatch (lpdisp, true); // obtain the worksheet set values (m_oworkbook.getsheets (), true ); // obtain the specified worksheet m_oworksheet.attachdispatch (m_oworksheets.getitem (colevariant (strsheetna Me), true); values (m_oworksheet.getusedrange (), true); // clear the data m_ocurrrange.clear (); // save m_oworkbook.saveas (colevariant (strfilepath) first ), _ variant_t (vtmissing), 0, _ variant_t (vtmissing ), _ variant_t (vtmissing), _ variant_t (vtmissing )); // Delete the worksheet m_oworksheet.delete (); // Save the cursor (colevariant (strfilepath), _ variant_t (vtmissing ), _ variant_t (vtmissing), _ variant_t (vtmissing), 0, _ variant_t (vtmissing), _ variant_t (vtmissing ), _ variant_t (vtmissing); m_oworkbook.close (covoptional, colevariant (strfilepath), covoptional); M _ Oworkbooks. Close (); Return 0;} the two parameters of the function are respectively the path of the Excel file and the name of the worksheet.

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.