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
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
#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
Irow ; ctemp = 'a'; m_pset-> first (); while (! m_pset-> eof) {for (index = 0; index
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