对于Excel单元格的属性操作各种各样,进行概括总结如下:
range.NumberFormatLocal = "@";
range = (Range)worksheet.get_Range("A1", "E1");
range.Merge(0);
worksheet.Cells[1, 1] = "Excel单元格赋值";
range.Font.Size = 15;
range.Font.Underline=true;
range.Font.Name="黑体";
range.HorizontalAlignment=XlHAlign.xlHAlignCenter;
range.ColumnWidth=15;
range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();
range.Borders.LineStyle=1;
range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());
range.EntireColumn.AutoFit();
Range.HorizontalAlignment= xlCenter;
Range.VerticalAlignment= xlCenter
Range.WrapText=true;
Range.Interior.ColorIndex=39;
Range.Font.Color=clBlue;
xlsApp.DisplayAlerts=false;
workbook.SaveCopyAs(temp);///填入完信息之后另存到路径及文件名字
其中对于单元格边框的操作一般使用Borders、BorderAround,但是这样有一个弊端只能够对于整个边框进行操作。有时我们需要对于单边或者获取和操作单元格内斜线,这个时候我们在VB中是比较容易实现的,那么在VC中一方面我们可以使用VBA来完成,另一个是深入使用Borders。
在Excel*.h中LPDISPATCH Borders::GetItem(long Index)方法即为VB中的Borders()属性,但是参数与VB中Borders参数值也相同,不过VC中不是常量了,需要自己定义。
定义如下:
Long index
Const xlDiagonalDown = 5
Const xlDiagonalUp = 6
Const xlEdgeBottom = 9
Const xlEdgeLeft = 7
Const xlEdgeRight = 10
Const xlEdgeTop = 8
Const xlInsideHorizontal = 12
Const xlInsideVertical = 11
使用实例:
Borders borders = myrange.GetBorders(); //获取borders
Border border = borders.GetItem(7); //获取border 参数根据long index定义
//根据参数不同还可获取斜线
border.SetWeight(COleVariant((short)3));