Export excel in VC ++-> Save automatically

Source: Internet
Author: User

Directly Add code

// Query the database and export exceladoconn v_cadoconn; _ recordsetptr v_precordset; v_cadoconn.oninitadoconn (theapp. a_idatabase_type, theapp. a_sdatabase_ip, theapp. a_sdatabase_name, theapp. a_sdatabase_user, theapp. a_sdatabase_password); int v_iindex = 2; cstring v_sbsc, v_slevel, v_sinfo, v_sstart, v_sdel, v_sdetail; // cstring v_ssql = "select zbsc, zlevel, zinfo, zstart, zdel, zdetail from historical alarm table where zstart between '"+ v_sstarttime + "' And '"+ v_sdeltime +" 'order by zstart DESC "; cstring v_ssql =" select zbsc, zlevel, zinfo, zstart, zdel, zdetail from historical alarm table order by zstart DESC "; // use the m_exlapp object to create the excel2003 process /******************************* **************************************** */:: oleinitialize (null); colevariant vopt (long) disp_e_paramnotfound, vt_error); range m_exlrge; _ worksheet m_exlsheet; worksheets m_exlsheets; _ workbook m_exlbook; Workbooks m_exlbooks; _ application m_exlapp; If (! M_exlapp.createdispatch ("Excel. Application", null) {afxmessagebox ("An error occurred while creating the Excel service! "); Return;} m_exlbooks.attachdispatch (m_exlapp.getworkbooks (), true); m_exlbook.attachdispatch (m_exlbooks.add (_ variant_t)" "), true ); // load an Excel template. For example, if it is null, load a new sheet (m_exlbook.getsheets (), true); // load the sheet page m_exlsheets.attachdispatch (m_exlbook.getworksheets (), true ); // get sheet1 m_exlsheet.attachdispatch (m_exlsheets.getitem (_ variant_t ("sheet1"), true ); ///// merge the first row of cells A1 to G1 //// // Load Cells to be merged (m_exlsheet.getrange (_ variant_t ("A1"), _ variant_t ("f1"), true); m_exlrge.merge (_ variant_t (long) 0 )); /*************************************** *********************************/////// // set the table content ////// m_exlrge.attachdispatch (m_exlsheet.getcells (), true); // load all cells m_exlrge.setitem (_ variant_t (long) 1), _ variant_t (long) 1), _ variant_t ("timed export of alarm information, export time: "+ v_ttime.format (" % Y-% m-% d % H: % m: % s "); m_exlrge.setitem (_ variant_t (long) 2), _ variant_t (long) 1 ), _ variant_t (""); m_exlrge.setitem (_ variant_t (long) 2), _ variant_t (long) 2), _ variant_t ("alarm level ")); m_exlrge.setitem (_ variant_t (long) 2), _ variant_t (long) 3), _ variant_t ("alarm information"); m_exlrge.setitem (_ variant_t (long) 2), _ variant_t (long) 4), _ variant_t ("occurrence time"); m_exlrge.setitem (_ variant_t (long) 2), _ variant_t (long) 5), _ variant_t ("restoration time ")); M_exlrge.setitem (_ variant_t (long) 2), _ variant_t (long) 6), _ variant_t ("details"); v_precordset = substring (_ bstr_t) v_ssql); While (! V_precordset-> adoeof & this-> m_bstopshow) {If (v_precordset-> fields-> getitem ("zbsc")-> actualsize> 0) v_sbsc = v_precordset-> getcollect ("zbsc "). bstrval; elsev_sbsc = ""; if (v_precordset-> fields-> getitem ("zlevel")-> actualsize> 0) v_slevel = v_precordset-> getcollect ("zlevel "). bstrval; elsev_slevel = ""; if (v_precordset-> fields-> getitem ("zinfo")-> actualsize> 0) v_sinfo = v_precordset-> getcollect ("zinfo "). BS Trval; elsev_sinfo = ""; if (v_precordset-> fields-> getitem ("zstart")-> actualsize> 0) v_sstart = v_precordset-> getcollect ("zstart "). bstrval; elsev_sstart = ""; if (v_precordset-> fields-> getitem ("zdel")-> actualsize> 0) v_sdel = v_precordset-> getcollect ("zdel "). bstrval; elsev_sdel = ""; if (v_precordset-> fields-> getitem ("zdetail")-> actualsize> 0) v_sdetail = v_precordset-> getcollect ("zdetail "). bstrval; elsev _ Sdetail = ""; // write excelv_iindex ++; m_exlrge.setitem (_ variant_t (long) v_iindex), _ variant_t (long) 1), _ variant_t (v_sbsc )); m_exlrge.setitem (_ variant_t (long) v_iindex), _ variant_t (long) 2), _ variant_t (v_slevel); m_exlrge.setitem (_ variant_t (long) v_iindex ), _ variant_t (long) 3), _ variant_t (v_sinfo); m_exlrge.setitem (_ variant_t (long) v_iindex), _ variant_t (long) 4 ), _ variant_t (v_sstart); m_exlrge.setitem (_ Varian T_t (long) v_iindex), _ variant_t (long) 5), _ variant_t (v_sdel); m_exlrge.setitem (_ variant_t (long) v_iindex ), _ variant_t (long) 6), _ variant_t (v_sdetail); v_precordset-> movenext (); // if the export is successful, set truethis-> m_bexcel = true ;} /*************************************** ******************************** // generate an Excel name/ * cstring v_ssavepath; cstring v_scurrentdate = v_ttime.format ("% Y _ % m _ % d"); cstring v_sdirectoty = v_spath + "\ exce Rochelle export "; if (! Createdirectory (lpctstr) v_sdirectoty, null) v_ssavepath = v_sdirectoty + "\" + v_scurrentdate + ". XLSX "; m_exlbook.savecopyas (colevariant (lpctstr) v_ssavepath); */round (" C: \ x.xlsx ", vopt, vopt ); /*************************************** ******************************** // release the object (equivalent important !) M_exlrge.releasedispatch (); release (); m_exlsheets.releasedispatch (); m_exlbook.releasedispatch (); m_exlbooks.releasedispatch (); // m_exlapp must be released, otherwise, an Excel process will reside in the memory after the program ends, and an error will occur when the program repeats running m_exlapp.quit (); m_exlapp.releasedispatch ();: oleuninitialize (); // shut down the database v_cadoconn.exitconnect ();}

 

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.