C++builder利用控件操作excel导出数据

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C++builder利用控件操作excel导出数据
写这个完全是因为前几日自己在做导出的时候遇到了麻烦,ole冲突了,不断报错,软件版本又必须上,无奈之下才想到使用excel2000控件,多方查阅,基本需要使用到的功能都列出来了。

详见代码。

1.首先拖下来三个控件Texcelapplication Texcelworkbook Texcelworksheet
后面就直接贴代码了,有注释:
ExcelApplication1->Connect();
ExcelApplication1->set_Visible(1,true);
ExcelApplication1->set_Caption(WideString(filename));
ExcelWorkbook1->ConnectTo(ExcelApplication1->Workboo ks->Add(TNoParam(),0));
ExcelWorksheet1->ConnectTo(ExcelWorkbook1->Workshee ts->get_Item((Variant)1));
ExcelWorksheet1->set_Name(WideString("*******"));
//设置边框
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 1, (Variant)
1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)15,(Variant )6)))->Borders->set_LineStyle( (Va riant)1 );
//设置字体、大小
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 1, (Variant)
1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)20,(Variant )7)))->Font->set_Name( (Variant)F ont1 );// Font1 定义为一个widestring类型如Widestring Font1 = “宋体”;
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 1, (Variant)
1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)1,(Variant) 1)))->Font->set_Size( (Variant)20 );
//设置标题框大小(行高)
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 1, (Variant)
1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)1,(Variant) 1)))->Rows->set_RowHeight( (Vari ant)40 );
//合并单元格
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 1, (Variant)
1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)1,(Variant) 6)))->Columns->Cells->Merge((Var iant)true);
//黑体
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 1, (Variant)
1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)1,(Variant) 1)))->Font->set_Bold( (Variant)true
);
//指定坐标位置字体颜色
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 14,(Variant)
1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)14,(Variant )6)))->Font->set_Color( (Variant)cl Blue );
//指定位置填入值
ExcelWorksheet1->get_Range( ( ExcelWorksheet1->Cells-> get_Item ( (Variant) 1, (Variant)1 ) ),(ExcelWorksheet1->Cells->get_Item((Variant)1,(Vari ant)6)))->set_Value((Variant)tit le);
//保留小数点后两位数
ExcelWorksheet1->get_Range((ExcelWorksheet1->Cells->g
et_Item((Variant)5,(Variant)3) ),(Exce lWorksheet1->Cells->get_Item((Variant)5,(Variant)6)))->set_Nu mberFormatLocal((Variant)length ); //length : WideString length = “0.00”; 如需加千分符号length = “#,##0.00”;美元length = “$#,##0.00”;其他货币自己找符号。

//水平居中
ExcelWorksheet1->get_Range((ExcelWorksheet1->Cells->g et_Item((Variant)1,(Variant)1) ),(Exce lWorksheet1->Cells->get_Item((Variant)20,(Variant)7)))->set_Ho rizontalAlignment((Variant)3); //1 常规,2 靠左(缩进),3 居中,4 靠右(缩进)
//标题垂直居中
ExcelWorksheet1->get_Range((ExcelWorksheet1->Cells->g et_Item((Variant)1,(Variant)1) ),(Exce lWorksheet1->Cells->get_Item((Variant)1,(Variant)7)))->set_Vert icalAlignment((Variant)2); // // 1=靠上2=居中3=靠下对齐4=两端对齐5=分散对齐
//日期
ExcelWorksheet1->get_Range((ExcelWorksheet1->Cells->g et_Item((Variant)2,(Variant)2) ),(Exce lWorksheet1->Cells->get_Item((Variant)2,(Variant)3)))->set_Valu e((Variant)(Date()));
最后就是干掉他,
ExcelWorkbook1->Close((V ariant)true);
ExcelApplication1->Disconnect();
ExcelApplication1->Quit();。

相关文档
最新文档