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; }
VC操作Excel写入数据源码
最新推荐文章于 2012-02-09 13:25:41 发布