VC操作Excel写入数据源码

void ControlExcel() { // TODO: Add your command handler code here COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR), vtTrue((short)TRUE), vtFalse((short)FALSE); _Application *ExcelApp = new _Application; ExcelApp->CreateDispatch("excel.application"); //设置为显示 ExcelApp->SetVisible(TRUE); //得到WorkBooks Workbooks ExcelBook=ExcelApp->GetWorkbooks(); Sheets ExcelSheet=ExcelBook.Add(vtOptional); _Workbook workBook; _Worksheet workSheet; Range range; workBook.AttachDispatch(ExcelApp->GetApplication()); ExcelSheet=workBook.GetSheets(); workSheet=ExcelSheet.GetItem(COleVariant((short)1)); workSheet.Activate(); Range myallrange=workSheet.GetRange(COleVariant("A1"),COleVariant("D1")); //myallrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myallrange.SetHorizontalAlignment(COleVariant((short)3)); myallrange.Merge(COleVariant((short)1)); myallrange.SetValue(COleVariant("个人信息表")); Range myrange=workSheet.GetRange(COleVariant("A2"),COleVariant("A2")); myrange.SetValue(COleVariant("姓名")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); myrange.SetColumnWidth(COleVariant((short)15)); myrange=workSheet.GetRange(COleVariant("B2"),COleVariant("B2")); myrange.SetValue(COleVariant("性别")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); myrange.SetColumnWidth(COleVariant((short)15)); myrange=workSheet.GetRange(COleVariant("C2"),COleVariant("C2")); myrange.SetValue(COleVariant("QQ")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); myrange.SetColumnWidth(COleVariant((short)15)); myrange=workSheet.GetRange(COleVariant("D2"),COleVariant("D2")); myrange.SetValue(COleVariant("Email")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); myrange.SetColumnWidth(COleVariant((short)17)); CString str; for(int i=3;i<=5;i++) { str.Format("A%d",i); Range myrange=workSheet.GetRange(COleVariant(str),COleVariant(str)); myrange.SetValue(COleVariant("相生昌")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); str.Format("B%d",i); myrange=workSheet.GetRange(COleVariant(str),COleVariant(str)); myrange.SetValue(COleVariant("男")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); str.Format("C%d",i); myrange=workSheet.GetRange(COleVariant(str),COleVariant(str)); myrange.SetValue(COleVariant("16432884")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); str.Format("D%d",i); myrange=workSheet.GetRange(COleVariant(str),COleVariant(str)); myrange.SetValue(COleVariant("superxsc@126.com")); myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional); myrange.SetHorizontalAlignment(COleVariant((short)3)); myrange.SetVerticalAlignment(COleVariant((short)2)); } ExcelApp->SetVisible(TRUE); delete ExcelApp; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值