VC操作Excel写入数据源码
2011-07-22 09:39
489 查看
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 写入与写出数据
- Java Poi操作Excel写入数据并设置style
- C#中NPOI操作excel之读取和写入excel数据
- 使用VSIUAL C#.NET操作Excel -把DataTable中的数据写入Excel
- jxl操作excel写入数据不覆盖原有数据示例
- Java操作Excel之理解JXL--读取Excel模板动态写入数据并生成Excel
- c#操作Excel和把Dataset数据写入Excel
- C#中NPOI操作excel之读取和写入excel数据
- VC 小工具里应用到excel做数据库时的查询与写入操作
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- 采用JXL包进行EXCEL数据写入操作
- [例题]VB操作Excel (1)[用VB创建一个xls文件][并向里面写入一个数据]
- Python对excel写入数据操作实例代码(只供参考)
- VC中操作excel表格 ListCtrl数据导出到Excel
- NPOI操作excel之写入数据到excel表
- 使用事务操作SQLite数据批量插入,提高数据批量写入速度,源码讲解
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- Java操作Excel之理解JXL--读取Excel模板动态写入数据并生成Excel
- VC++ 中操作Excel文档的方法,读取,查询,写入,修改,删除
- 使用VSIUAL C#.NET操作Excel -把DataTable中的数据写入Excel