BOOL CTest_App::InitInstance() { ....... // 初始化Con支持库 if(ConInitialize(NULL) != 0) { AfxMessageBox("初始化COn支持库失败!"); exit(1); } ......... }
class CTest_Dlg { // 定义相关全局变量 _Application app; Workbooks books; _Workbook book; Worksheets sheets; _Worksheet sheet; Range range; }
void CTest_Dlg::OnTest() { // TODO: Add extra validation here LPDISPATCH lpDisp; COleVariant vResult; CString str = ""; COleVariant covTrue((short)TRUE); COleVariant covFalse((short)FALSE); COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); // 创建excel 2003 服务器(启动Excel) if(!app.CreateDispatch("Excel.Application", NULL)) { AfxMessageBox("Create Excel Server Failed !"); return; } app.SetVisible(TRUE); books.AttachDispatch(app.GetWorkbooks(), true); // 打开一个工作薄 lpDisp = books.Open("E:\\test.xls", covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional); ASSERT(lpDisp); book.AttachDispatch(lpDisp); // 获取WorkSheets sheets.AttachDispatch(book.GetWorksheets(), true); // 获取WorkSheet sheet.AttachDispatch(sheets.GetItem(_variant_t((short)(1)))); // 得到全部Cells range.AttachDispatch(sheet.GetCells(), true); // 往单元格里写入字符串输出 //range.SetItem(_variant_t((LONG)1), _variant_t((LONG)1), _variant_t("hello, world !")); /* range.AttachDispatch(sheet.GetRange(_variant_t("A2"), _variant_t("A2")), true); range.SetValue2(_variant_t("2011/02/15")); range.AttachDispatch(sheet.GetRange(_variant_t("A3"), _variant_t("B6")), true); range.SetValue2(_variant_t((double)3.14)); // 设置单元格的列宽为12 range.AttachDispatch(sheet.GetRange(_variant_t("A1"), _variant_t("A1")), true); range.SetColumnWidth(_variant_t((long)12)); range.AttachDispatch(sheet.GetCells(), true); range.SetHorizontalAlignment(_variant_t((long)-4108)); // 设置单元格 range.SetVerticalAlignment(_variant_t((long)-4108)); */ for(int j = 2; j < 300; j++) { range.AttachDispatch(range.GetItem(_variant_t((long)j), _variant_t((long)6)).pdispVal); vResult = range.GetValue(covOptional); VARIANT varItem = range.GetText(); str = varItem.bstrVal; CString str_t = str; char* p = (LPSTR)(LPCTSTR)str; int a = 0, sum = 0; for(int i = 2; i < 5; i++) { a = p[i]-'0'; sum+=a; sum*=16; } sum+=p[i]-'0'; range.AttachDispatch(sheet.GetCells()); range.SetItem(_variant_t((long)j), _variant_t((long)5), _variant_t((long)sum)); } book.Save(); range.ReleaseDispatch(); sheet.ReleaseDispatch(); sheets.ReleaseDispatch(); book.ReleaseDispatch(); books.ReleaseDispatch(); app.ReleaseDispatch(); //range.AttachDispatch CDialog::OnOK(); }
// 本内容摘自 http://blog.csdn.net/shuilan0066/article/details/7936376
【VC控制EXCEL的基本操作之选择范围篇】
1 选择单元格
//选择一个单元格 range.AttachDispatch(sheet.GetRange(COleVariant(_T("A1")),COleVariant(_T("A1"))),TRUE); //选择多个单元格 range.AttachDispatch(sheet.GetRange(COleVariant(_T("A1")),COleVariant(_T("D1"))),TRUE); //使用变量表示单元格 CString CellName; Int i=1;j=1; CellName.Format(_T(“%c%d”),j+64,i); //A–Z 65-90 //加载单元格 rnge.AttachDispatch(range.GetRange(COleVariant(CellName),COleVariant(CellName)));
2 选择某行
// 1) //选择第一行 range.AttachDispatch(sheet.GetRange(COleVariant(_T("A1")),COleVariant(_T("IV1"))),TRUE); //选择前5行 range.AttachDispatch(sheet.GetRange(COleVariant(_T("A1")),COleVariant(_T("IV5"))),TRUE); // 2)先选择某行中的某个单元格,然后再选择整行 Range rows; rows.AttachDispatch(sheet.GetRange(COleVariant(_T("A1")),COleVariant(_T("A1"))),TRUE); rows=rows.GetEntireRow(); // 3)获得所有的行,然后再选择指定行 Range rows; range.AttachDispatch(sheet.GetRows(),TRUE); //选择第一行 rows.AttachDispatch(range.GetItem(COleVariant((long)1),vtMissing).pdispVal); rows.SetRowHeight(COleVariant((long)60));
3 选择某一列
// 1) range.AttachDispatch(sheet.GetRange(COleVariant(L"A1"),COleVariant(L"A65536"))); //第一列 // 2)先选择某列中的某个单元格,然后再选择整列 range.AttachDispatch(sheet.GetRange(COleVariant(L"A1"),COleVariant(L"A1"))); //第一列 range=range.GetEntireColumn(); // 3)先获得所有列,然后再选择某一列 //获得所有列 range.AttachDispatch(sheet.GetColumns(),true); //选择第一列 range.AttachDispatch(range.GetItem(COleVariant((long)1),vtMissing).pdispVal,TRUE);
4 选择全部CELLS
range.AttachDispatch(sheet.GetCells(),TRUE);//加载所有单元格
5 选择已使用的单元格
range.AttachDispatch(sheet.GetUsedRange());//加载已使用的单元格
6 获取单元格的值
// 1) range=sheet.GetRange(COleVariant("A1"),COleVariant("A1")); COleVariant rValue; rValue=COleVariant(range.GetValue()); rValue.ChangeType(VT_BSTR); this->MessageBox(CString(rValue.bstrVal)); // 2) //读取第一个单元格的值 range.AttachDispatch(sheet.GetCells()); range.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal ); vResult =range.GetValue(); CString str; if(vResult.vt == VT_BSTR) //字符串 { str=vResult.bstrVal; } else if (vResult.vt==VT_R8) //8字节的数字 { str.Format(L"%f",vResult.dblVal); } else if(vResult.vt==VT_DATE) //时间格式 { SYSTEMTIME st; VariantTimeToSystemTime((long)&vResult.date, &st); } else if(vResult.vt==VT_EMPTY) //单元格空的 { str=""; }
// 本内容摘自 http://blog.csdn.net/shuilan0066/article/details/7936383
【VC控制EXCEL的基本操作之设置篇】
1 设置单元格的值
// 1) / 选中指定单元格,使用SetValue设置值 CellName.Format(_T("A%d"),i);//单元格的名称 range.AttachDispatch(sheet.GetRange(COleVariant(CellName),COleVariant(CellName)));//加载单元格range.SetValue(COleVariant(subFileName)); CellName.Format(_T("C%d"),i);//单元格的名称 range.AttachDispatch(sheet.GetRange(COleVariant(CellName),COleVariant(CellName)));//加载单元格 range.SetValue(COleVariant((long)i)); // 2) // 选中所有的单元格, 使用SetItem 设置指定单元格的值 range.AttachDispatch(sheet.GetCells(),TRUE);//加载所有单元格 //设置第I行 第1、2、3列的值 range.SetItem(COleVariant((long)(i)),COleVariant((long)1),COleVariant(subFileName)); //第一列 range.SetItem(COleVariant((long)(i)),COleVariant((long)2),COleVariant(unicodeStr)); //第二列 range.SetItem(COleVariant((long)(i)),COleVariant((long)3),COleVariant((long)i)); //第三列
2 设置行高列宽
// 选中某列 设置列宽 //获得所有列 range.AttachDispatch(sheet.GetColumns(),true); //设置第一列的列宽 Range cols cols.AttachDispatch(range.GetItem(COleVariant((long)1),vtMissing).pdispVal,TRUE); cols.SetColumnWidth(COleVariant((long)10)); //设置列宽 //设置第4列的列宽 cols.AttachDispatch(range.GetItem(COleVariant((long)4),vtMissing).pdispVal,TRUE); cols.SetColumnWidth(COleVariant((long)10)); //设置列宽 //设置列宽为自动适应 cols.AutoFit(); // 设置行高 Range rows; range.AttachDispatch(sheet.GetRows(),TRUE); //选择第一行 rows.AttachDispatch(range.GetItem(COleVariant((long)1),vtMissing).pdispVal); //设置行高 rows.SetRowHeight(COleVariant((long)60)); // 注意: 行高列宽使用的单位不一样
3 设置单元格类型
range.AttachDispatch(sheet.GetCells(),TRUE);//加载所有单元格 range.SetNumberFormat(COleVariant(L"@")); //将单元格设置为文本类型
4 设置单元格字体
Font ft; //设置第一列的字体 range.AttachDispatch(sheet.GetColumns(),true); range.AttachDispatch(range.GetItem(COleVariant((long)1),vtMissing).pdispVal,TRUE); //字体作用范围 ft.AttachDispatch(range.GetFont()); ft.SetName(COleVariant(_T("宋体"))); ft.SetSize(COleVariant((long)48)); ft.SetBold(COleVariant((long)1));//粗体 ft.SetColorIndex(COleVariant((long)2));//设置字体颜色
5 设置单元格背景色
range.AttachDispatch(sheet.GetRange(COleVariant(_T("A1")),COleVariant(_T("D1")))); //设置底色///// Interior it; it.AttachDispatch(range.GetInterior()); it.SetColorIndex(COleVariant((long)11));//标题底色 ////表格内容的底色//// range.AttachDispatch(sheet.GetRange(COleVariant(_T("A2")),COleVariant(_T("D5")))); it.AttachDispatch(range.GetInterior()); it.SetColorIndex(COleVariant((long)15));
6 设置表格边框
1) range.AttachDispatch(sheet.GetRange(COleVariant(_T("A2")),COleVariant(_T("D5")))); //LineStyle=线型Weight=线宽ColorIndex=线的颜色(-4105为自动) range.BorderAround(COleVariant((long)1),(long)2,((long)-4105),vtMissing);//设置边框 2) range.AttachDispatch(sheet.GetRange(COleVariant(_T("A2")),COleVariant(_T("D5")))); _variant_t v1; //线型 _variant_t v2; //宽度 _variant_t v3; //颜色 v1.vt=VT_I2; v1.lVal=2; // 线的样式:- no line; 1-solid; 2-big dot;3-small dot;4-dash dot; 5-dash dot dot; v2.vt=v1.vt; v2.lVal=3; // 线的粗细程度 v3.vt=v1.vt; v3.lVal=1; // 1-black;2-white;3-red;4-green;5-blue; 6-yellow; 7-pink;8-dark blue; UnitRge.BorderAround(v1,v2,v3,vtMissing);//设置边框
7 合并单元格
1) //合并单元格 //加载要合并的单元格 range.AttachDispatch(sheet.GetRange(COleVariant(_T("A1")),COleVariant(_T("D1"))),TRUE); range.Merge(COleVariant((long)0)); 2) //将第一个单元格合并成行,列 range.AttachDispatch(sheet.GetCells()); unionRange.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal ); //第一个单元格 unionRange.AttachDispatch(unionRange.GetResize(COleVariant((long)3),COleVariant((long)2))); //GetResize(3,2) 从第一个单元格开始合并行列的所有单元格 unionRange.Merge(COleVariant((long)0)); //合并单元格
8 设置单元格文本为自动换行 及排列方式
range.AttachDispatch(sheet.GetUsedRange());//加载已使用的单元格 range.SetWrapText(COleVariant((long)1));//设置单元格内的文本为自动换行 //设置齐方式为水平垂直居中 //水平对齐:默认=1,居中=-4108,左=-4131,右=-4152 //垂直对齐:默认=2,居中=-4108,左=-4160,右=-4107 range.SetHorizontalAlignment(COleVariant((long)-4108)); range.SetVerticalAlignment(COleVariant((long)-4108));
9 在单元格中插入公式
//选择A2单元格,插入一个公式"=RAND()*100000",并设置A2数字格式为货币形式 range=sheet.GetRange(COleVariant(L"A2"),COleVariant(L"A2")); range.SetFormula(COleVariant(L"=RAND()*100000")); range.SetNumberFormat(COleVariant(L"$0.00"));
10 在单元格中插入图片
Shapes shapes = sheet.GetShapes();// 从Sheet对象上获得一个Shapes range.AttachDispatch(sheet.GetRange(COleVariant(L"E5"),COleVariant(L"E5"))); // 获得Range对象,用来插入图片 shapes.AddPicture( L"d:\\pic.bmp " , false , true , (float)range.GetLeft().dblVal ,(float)range.GetTop().dblVal,(float)range.GetWidth().dblVal,(float)range.GetHeight().dblVal); ShapeRange sRange = shapes.GetRange(_variant_t(long(1))); sRange.SetHeight(float(30)); //在RANGE范围内 设置图片宽高 sRange.SetWidth(float(30));
11 对选取的区域进行排序
#define xlAscending (long) 1 #define xlDescending (long) 2 #define vOpt COleVariant((long) DISP_E_PARAMNOTFOUND, VT_ERROR) #define xlHeader (long) 1 // 选取的区域有标题 #define xlNoHeader (long) 2 // 选取的区域无标题 一定要正确确定是否有标题,否则排序可能不成功 #define xlMatchCase COleVariant((long) 1) #define xlIgnoreCase COleVariant((long) 0) #define xlTopToBottom (long) 1 // 垂直方向进行排序 #define xlLeftToRight (long) 2 // 水平方向进行排序 #define xlPinYin (long) 1 // this is the default // 按字符的中文拼音进行排序 #define xlStroke (long) 2 // 按每个字符中的笔画数进行排序 VARIANT key1; // these lines set up first arg (key1) to sort V_VT(&key1) = VT_DISPATCH; // 排序时,关键字的vt设置为VT_DISPATCH V_DISPATCH(&key1) = sheet.GetRange(COleVariant(L"A1"),COleVariant(L"A1")); // 设置按哪个关键字进行排序 选择单元意为:按此列(或行)为主关键字进行排序 range=sheet.GetRange(COleVariant(L"A1"),COleVariant(CellName)); // 选择对哪些区域内的单元格进行排序 range.Sort(key1, xlAscending, vOpt, vOpt, xlAscending, vOpt,xlAscending,xlNoHeader,vOpt,xlIgnoreCase,xlTopToBottom,xlPinYin); //设置排序 无标题时一定要设置为xlNoHeader 否则不起作用