设置篇 |
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范围内 设置图片宽高 |
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 否则不起作用 |
参考资料: