学习c++一点一滴----c++builder 导出excel

ContractedBlock.gifExpandedBlockStart.gifCode
    if(SaveDialog1->Execute()==false)
    {
        MessageBox(NULL,
"请选择文件!","提示",MB_OK|MB_ICONEXCLAMATION);
        
return;
    }
    
int nFileHandle=FileCreate(SaveDialog1->FileName);
    

    AnsiString m_FileName;
    
if(FileExists(SaveDialog1->FileName)&&(MessageBox(NULL,"文件重复,是否替换?","提示",MB_YESNO|MB_ICONEXCLAMATION)==IDNO))
    {
        
return;//m_FileName=SaveDialog1->FileName;
    }
    
else
    {
        m_FileName
=SaveDialog1->FileName;
        
//MessageBox(NULL,"文件不存在!","捍卫者",MB_OK|MB_ICONERROR);
        
//return;
    }
    FileClose(nFileHandle);
    Variant Ex,Wb,Sh1;

    
//String m_FileName="d:\test.xls";
    try
    {
        Ex
=Variant::CreateObject("Excel.Application");
    }
    
catch()
    {
        MessageBox(NULL,
"启动Excel出错,可能没有安装Excel","捍卫者",MB_OK|MB_ICONERROR);
        Ex
=Unassigned;
        
return;
    }

    Ex.OlePropertySet(
"Visible",false);      //设置不可见
    Ex.OlePropertySet("DisplayAlerts",false);   //设置不可弹出对话框
    
//Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",SaveDialog1->FileName.c_str());
    Ex.OlePropertyGet("WorkBooks").OleFunction("Add",1); //添加一个sheet
    Sh1=Ex.OlePropertyGet("ActiveWorkBook").OlePropertyGet("ActiveSheet"); //获取默认的sheet
    Sh1.OleProcedure("Select");    //选择sheet
    Sh1.OlePropertyGet("Cells").OleProcedure("Select"); //设置celles,列选择
    Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size",ListView1->Font->Size);    //设置字体大小
    Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name",ListView1->Font->Name.c_str());       //设置第一行,标题
    Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("FontStyle","常规"); //设置字体样式
    Sh1.OlePropertyGet("Cells",1,1).OleProcedure("Select"); //选择表1,1
    int nRowCount(ListView1->Items->Count+1);
    nRowCount
=nRowCount<2?2:nRowCount;
    
int nColCount(ListView1->Columns->Count);
    nColCount
=nColCount<1?1:nColCount;
    
for(int i=0;i<nColCount;i++)
    {
        
int nColWidth;
        
if(ListView1->Columns->Count>i)
        {
            nColWidth
=ListView1->Columns->Items[i]->Width;
        }
        
else
        {
            nColWidth
=100;
        }
        Ex.OlePropertyGet(
"Columns",i+1).OlePropertySet("ColumnWidth",nColWidth);    //设置列宽
    }
    
for(int j=0;j<ListView1->Columns->Count;j++)
    {
        Ex.OlePropertyGet(
"Rows",1).OlePropertySet("RowHeight",20);    //设置行高
        Sh1.OlePropertyGet("Cells",1,j+1).OlePropertySet("Value",ListView1->Columns->Items[j]->Caption.c_str());    //添加内容
        Variant vInter=Sh1.OlePropertyGet("Cells",1,j+1).OlePropertyGet("Interior");    //设置
        vInter.OlePropertySet("ColorIndex",15);      //设置颜色
        vInter.OlePropertySet("Pattern",1);             //设置样式
        vInter.OlePropertySet("PatternColorIndex",-4105);
    }

    
for(int i=0;i<nRowCount-1;i++)
    {
        Ex.OlePropertyGet(
"Rows",i+2).OlePropertySet("RowHeight",16);
        Sh1.OlePropertyGet(
"Cells",i+2,1).OlePropertySet("Value",ListView1->Items->Item[i]->Caption.c_str());
        
for(int j=0;j<ListView1->Items->Item[i]->SubItems->Count;j++)
        {
            Sh1.OlePropertyGet(
"Cells",i+2,j+2).OlePropertySet("Value",ListView1->Items->Item[i]->SubItems->Strings[j].c_str());

        }
    }
    Ex.OlePropertyGet(
"ActiveWorkbook").OleProcedure("SaveAs",m_FileName.c_str());     //另存
    
//Ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Save");
    Ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Close");  //关闭sheet
    Ex.OleFunction("Quit"); //退出excel
    Sh1=Unassigned;      //释放
    MessageBox(NULL,"导出成功","捍卫者",MB_OK|MB_ICONINFORMATION);
网上找了些资料,才搞成这个玩意,感觉很麻烦,但是很有成就感。大家自己看看,无非4个函数,来回调用excel的属性,方法,过程。
OlePropertyGet      //获取
OlePropertySet      //设置
OleProcedure      //调用一个过程
OleFunction      //函数

转载于:https://www.cnblogs.com/baiye/archive/2009/07/20/1527240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值