http://blog.youkuaiyun.com/miderph84/archive/2009/03/17/3998235.aspx
要在应用程序中控制Excel的运行,首先必须在编制自动化客户程序时包含 Comobj.hpp
#include "Comobj.hpp"
C++ Builder把Excel自动化对象的功能包装在下面的四个Ole Object Class函数中,应用人员可以很方便地进行调用。
设置对象属性:void OlePropertySet(属性名,参数……);
获得对象属性:Variant OlePropertyGet(属性名,参数……);
调用对象方法:1) Variant OleFunction(函数名,参数……);
2) void OleProcedure(过程名,参数……);
在程序中可以用宏定义来节省时间:
#define PG OlePropertyGet
#define PS OlePropertySet
#define FN OleFunction
#define PR OleProcedure
举例:
ExcelApp.OlePropertyGet("workbooks").OleFunction("Add");
可写为
ExcelApp.PG("workbooks").FN("Add");
◎ 设置列宽
ExcelApp.PG("Columns", 1).PS("ColumnWidth", 22);
或者
Range = ExcelApp.PG("Cells", 1, 3);
Range.PS("ColumnWidth", 22);
◎ 设置行高
ExcelApp.PG("Rows", 2).PS("RowHeight", 25);
或者
Range = ExcelApp.PG("Cells", 2, 1);
Range.PS("RowHeight", 25);
或者另一个方法 (没试过)
eworkbook.worksheets[1].columns.autofit;//如不自定义宽度,可以使各列自动对齐