C++Builder中使用Excel的类

使用方法,把下面的RtpExcel中的RtpExcel.h段存成文件RtpExcel.h,
RtpExcel.c存成文件RtpExcel.c并保存,加入到C++Builder的工程中即可使用。

/*
RtpExcel.h
*/
//---------------------------------------------------------------------------
#ifndef 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();
        //从sBeginRow行开始设置数据并初始化边框
        bool SetData(const RptInf& rInf,TDataSet* pSet);
        bool PrintRpt();
private:
        //初始化应用程序对象
        bool InitApp();
        //设置excel程序对象的可见性
        bool SetAppVisible(bool bVisible);

private:
        bool SetCellBorder();

        bool SetInfTable();
        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(true);
        return true;
}
bool CRptExcel::InitApp()
{
        if(!NewExcelApp())      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应用程序对象!";
        return false;
    }
    return true;
}
bool CRptExcel::NewWorkBook()
{
 Variant all_workbooks;
 //-- Get workbooks collection
 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;
}
//得到m_cEnd,m_cBegin;m_RowLast;m_RowBegin;的值
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 CRptExcel::SetCellValue()
{
        char ctemp,cEnd;
        int iRow,iRowLast;
        unsigned int index;
        Variant cell;
        String str;
        if(!m_pSet)
        {
                m_sError="没有设置数据集!";
                return false;
        }
        if(m_pSet->Eof&&m_pSet->Bof)
        {
                m_sError="数据集为空";
                return false;
        }
        if(m_ColCount<=0)
        {
                m_sError="列数读取出错!";
                return false;
        }
        ctemp='A';iRow=4;
        for(index=0;index        {
                ctemp='A'+index;
                str.sprintf("%c%d",ctemp,iRow);
                cell=m_Sheet.OlePropertyGet("Range",str);
                str=m_pSet->Fields->Fields[index]->FieldName;
                cell.OlePropertySet("Value",str);
                if(ctemp=='Z')
                {
                        m_sError="列数太多出错";
                        return false;
                }
        }

        iRow++;ctemp='A';
        m_pSet->First();
        while(!m_pSet->Eof)
        {
                for(index=0;index                {
                        ctemp='A'+index;
                        str.sprintf("%c%d",ctemp,iRow);
                        cell=m_Sheet.OlePropertyGet("Range",str);
                        str=m_pSet->Fields->Fields[index]->AsString;
                        cell.OlePropertySet("Value",str);
                }
                iRow++;
                m_pSet->Next();
        }

        return 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="设置表头信息时出错!";
                return false;
        }
        return true;
}
//设置公司的信息到页眉页脚处
bool CRptExcel::SetInfCom()
{
        try{
        Variant PageHeader=m_Sheet.OlePropertyGet("PageSetup");
        PageHeader.OlePropertySet("RightHeader","&D ");
        PageHeader.OlePropertySet("LeftHeader",m_sCompanyInf);
        }
        catch(...)
        {
                m_sError="设置页眉信息时出错!";
                return false;
        }
        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="设置表头信息时出错!";
                return false;
        }
        return true;
}
bool CRptExcel::SetTopRow()
{
        try{
        Variant vCell=m_Sheet.OlePropertyGet("Range","A3");
        vCell.OlePropertySet("Value",m_sA3Content);
        String str;
        str.sprintf("%c3",'A'+m_ColCount);
        vCell.OlePropertyGet("Range",str);
        vCell.OlePropertySet("Value",m_sLastCol3Content);
        }
        catch(...)
        {
                m_sError="设置表头信息时出错!";
                return false;
        }
        return true;
}
bool CRptExcel::SetCellBorder()
{
        String str;
        char ct='A';
        for(unsigned int index=m_RowBegin;index        {
         for(unsigned int j=0;j         {
              ct='A'+j;
              Variant vCell,vBorder;
              try{
                    str.sprintf("%c%d",ct,index);
                    vCell=m_Sheet.OlePropertyGet("Range",str);
                    vCell.OlePropertyGet("Borders").OlePropertySet("linestyle",xlContinuous);
                    if(j==0)//对第一列的单元格设置其左边界为粗
                    {
                            vBorder=vCell.OlePropertyGet("Borders",xlEdgeLeft);
                            vBorder.OlePropertySet("linestyle",xlContinuous);
                            vBorder.OlePropertySet("weight",xlThick);
                    }
                    if(j==m_ColCount-1)//the Right Edge of last col
                    {
                            vBorder=vCell.OlePropertyGet("Borders",xlEdgeRight);
                            vBorder.OlePropertySet("linestyle",xlContinuous);
                            vBorder.OlePropertySet("weight",xlThick);
                    }
                    if(index==m_RowBegin)//the first row having data
                    {
                            vBorder=vCell.OlePropertyGet("Borders",xlEdgeTop);
                            vBorder.OlePropertySet("linestyle",xlContinuous);
                            vBorder.OlePropertySet("weight",xlThick);
                    }
                    if(index==m_RowLast)
                    {
                            vBorder=vCell.OlePropertyGet("Borders",xlEdgeBottom);
                            vBorder.OlePropertySet("linestyle",xlContinuous);
                            vBorder.OlePropertySet("weight",xlThick);
                    }
                }
                catch(...)
                {
                        m_sError="设置边框时出错!";
                        return false;
                }
         }
        }
        return true;
}
//---------------------------------------------------------------------------
#pragma package(smart_init)

你可能感兴趣的:(C++Builder中使用Excel的类)