Use Excel class in C ++ Builder

xiaoxiao2021-03-06  67

Using the method, store the RTPEXCEL.H segment in the following RTPEXCEL to file RTPEXCEL.H, RTPEXCEL.C to save the file RTPEXCEL.C and save it, add it to the work of C Builder. /*//--------------------------------------------------------------------- ---------------------------------- # i indef rptexcelh # define rptexcelh # include struct rptpagesetup { String sLeftHeader; String sLeftFooter; String sCenterheader; String sRightHeader; String sRightFooter; String sCenterFooter;}; struct RptInf {RptPageSetup RptPage; String tTitle; String tFirstRowL; String tFirstRowR;}; class CRptExcel {public: CRptExcel (); ~ CRptExcel () / / Start setting data from the SBEGINROW row and initialize the Border Bool SetData (Const RPTINF & RINF, TDATASET * PSET); BOOL Printrpt (); private: // Initialize the application object BOOL INITAPP (); // Set the visible Excel program object Sex Bool SetAppvisible (Bool Bvisible);

PRIVATE: BOOL setcellborder ();

Bool setInfcom (); bool settitle (); bool settoprow (); bool setCellvalue (); BOOL newWorkbook (); Bool newExcelapp ();

private: TDataSet * m_pSet; Variant m_ExcelApp; Variant m_Sheet; Variant m_WorkBook; Variant m_Range; unsigned int m_RowLast; unsigned int m_RowBegin; char m_cBegin; char m_cEnd; unsigned int m_RowCount; unsigned int m_ColCount; String m_sTitle; String m_sCompanyInf; String m_sA3Content; String M_slastcol3content; bool m_bapprun; private: string m_serror;}; // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------------- # Endif // ------- -------------------------------------------------- ------------------ / * rptexcel.c * / # include #pragma hdrstop # include "excel_2k_srvr.h"

#include "RptExcel.h" CRptExcel :: CRptExcel () {m_pSet = NULL; m_bAppRun = false;} CRptExcel :: ~ CRptExcel () {if (m_bAppRun) {m_ExcelApp.OleFunction ( "Quit");}} bool CRptExcel: : Printrpt () {if (! InitApp ()) Return false; if (! Setcellvalue ()) Return False; if (! Setcellborder ()) Return False; if (! SetTitle ()) Return False; if (! Setinfcom )) Return False; if (! setinftable ()) Return False; if (! settoprow ()) Return False; setAppvisible;} Bool crpTexcel :: initApp () {ix (!.com) Return False; if (! newworkbook ()) Return False; Return true;} Bool CRPTEXCEL :: NewExcelapp () {Try {m_excelapp = variant :: createObject ("excel.application"); m_bapprun = true;} catch (... ) {M_Serror = "Excel application object!"; Return false;} rebootExcel :: newWorkbook () {variant all_workbooks; // - Get Workbooks Collecti on all_workbooks = m_ExcelApp.OlePropertyGet ( "Workbooks"); // - Set number of worksheets to 1 m_ExcelApp.OlePropertySet ( "SheetsInNewWorkbook", (Variant) 1); // - Create a new workbook m_WorkBook = all_workbooks.OleFunction ( "Add"); m_Sheet = m_WorkBook.OlePropertyGet ( "ActiveSheet"); return true;} bool CRptExcel :: SetAppVisible (bool bVisible) {m_ExcelApp.OlePropertySet ( "Visible", (Variant) bVisible); return true;} // Get m_cend, m_cbegin; m_rowlast; m_rowbegin; value BOOL CRPTEXCEL :: setData (const rptinf & rinf, tdataset * pset) {m_colcount = pset-> fieldcount; m_cbegin = 'a'; m_cend = '

A ' m_ColCount; m_RowBegin = 4; m_RowCount = pSet-> RecordCount; m_RowLast = m_RowBegin m_RowCount; m_pSet = pSet; m_sTitle = rInf.tTitle; m_sA3Content = rInf.tFirstRowL; m_sLastCol3Content = rInf.tFirstRowR; m_sCompanyInf = rInf.RptPage. Sleftheader; Return True;} Bool CREMP, CEND; IROW, IROWLAST; Unsigned Int Index; Variant Cell; String Str; if (! m_pset) {m_serror = "No data set!"; Return false;} if (m_pset-> eof && m_pset-> bof) {m_SERROR = "Dataset is empty"; return false;} if (m_colcount <= 0) {m_SERROR = "column number read error!"; RETURN FALSE; } Ctemp = 'a'; irow = 4; for (index = 0; index fields-> fields [index] -> fieldName; cell.olepropertySet ("value", str); if (ctemp == 'z') {m_serror = "The number of columns"; RETURN FALSE; }

Irow ; ctemp = 'a'; m_pset-> first (); while (! m_pset-> eof) {for (index = 0; index fields-> fields [index] -> asstring; cell.olepropertyset (" Value ", STR) }} Irow ; m_pset-> next ();} reburn true;} Bool crpTexcel :: setTitle () {string str; char ct; str.sprintf ("% C% D:% C% D", 'A', 1, ('a' m_colcount), 1); Variant vcell; try {vCell = m_sheet.olePropertyget ("Range", Str); vcell.olepropertySet ("Value", m_stitle);} catch (...) { M_SError = "Set the wrong message when the header information is set!"; return true;} // set the company's information to the header foot Bool CRPTEXCEL :: setInfcom () {Try {variant PageHeader = m _Sheet.olePropertyGet ("Pagesetup"); PageHeader.olePropertySet ("RightHeader", "& D"); PageHeader.olePropertySet ("LeftHeader", m_scompanyinf);} Catch (...) {m_Serror = "Setup header information error ! "; Return true;} Bool crptExcel :: setInftable () {

try {Variant PageHeader = m_Sheet.OlePropertyGet ( "PageSetup"); PageHeader.OlePropertySet ( "RightFoot", "& P / & N"); PageHeader.OlePropertySet ( "LeftFoot", m_sTitle);} catch (...) {m_sError = "Error when setting the header information!"; Return false;} reburn true;} bool crptExcel :: settoprow () {Try {variant vCell = m_sheet.olePropertyget ("Range", "A3"); vcell.olepropertySet ("Value ", m_sa3content); string str ;Sprintf ("% C3 ", 'a' m_colcount); vcell.olepropertyget (" Range ", str); vcell.olepropertyset (" Value ", m_slastcol3content);} catch. ..) {m_Serror = "Set the error when setting the header information!"; Return true;} Bool crpTexcel :: setcellborder () {string str; char ct = 'a'; for (unsigned int index = m_rowbegin; INDEX

转载请注明原文地址:https://www.9cbs.com/read-111122.html

New Post(0)