BCB操作EXCEL

最近经常用EXCEL导出报表,上网搜索了一下,整理了一下CB操作EXCEL的操作,以作留念

1:启动Excel

 Variant vExcelApp, vSheet, Range;
Variant vExcelApp = Variant::CreateObject("Excel.Application"); //
vExcelApp.OlePropertySet("Visible", false);//隐藏Excel界面
 vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 新增工作区
  vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1);//操作这个工作表

2:退出Excel

 vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs", "c://2.xls");//保存
 vExcelApp.OleFunction("Quit");//退出
 vSheet = Unassigned;
 vExcelApp = Unassigned;

//不保存直接退出

vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertySet("Saved", true);
vExcelApp.OleFunction("Quit");

3:对齐

Range.OlePropertySet("VerticalAlignment", 3);

 Range.OlePropertySet("HorizontalAlignment", 3); //2=居左 3=居中

4:赋值

vSheet.OlePropertyGet("Cells", 3, i + 2).OlePropertySet("Value", gdbill[i].c_str());

5:操作字体

vSheet.OlePropertyGet("Cells",1, j + 1).OlePropertyGet("Font").OlePropertySet("Bold", true);

font-bold, Italic,size, name,color(rgb)

6:行高,列宽

vSheet.OlePropertyGet("Columns", 2).OlePropertySet("ColumnWidth", 25);

vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 30);

8:合并单元格

             char b1 = 'b' + i * 2; //b
            String ra;
            ra.sprintf("%c%d:%c%d", b1, 2, b1 + 1, 2);
            Range = vSheet.OlePropertyGet("range", ra.c_str());
            Range.OleFunction("Merge", false);

9:设置单元格背景

vInter = vSheet.OlePropertyGet( "Cells", 1, j + 1).OlePropertyGet("Interior");
vInter.OlePropertySet("ColorIndex", 15); // 灰色
vInter.OlePropertySet("Pattern", 1); // xlSolid
vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic

 10:操纵多工作表

//生成缺省的三个工作表

 vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add"); // 工作表

//选择工作表1

 vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1).OleProcedure("Select");
vSheet1 = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("ActiveSheet");

//选择工作表
vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 2).OleProcedure("Select");
vSheet2 = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("ActiveSheet");

参考资料:

http://www.builder.com.cn/2007/1027/585220.shtml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值