Delphi控制Excel的行列宽,字体,单元格合并问题
大师们,高人们,小弟遇到Delphi操作Excel的问题,我是从网上搜到的Delphi操作Excel的步骤,但是运行总是出错(Undeclared identifier:'Columns'),一直没有找到为什么,请给指点。下面是代码,Form1中添加了分别添加一个ExcelApplication1,ExcelWorkbook1,ExcelWorksheet1和一个按钮unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExcelXP, OleServer;
type
TForm1 = class(TForm)
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
Temp_Worksheet : _WorkSheet ;
begin
//显示当前窗口
ExcelApplication1.Connect ;
//显示当前窗口:
ExcelApplication1.Visible [ 0 ] := True ;
//更改 Excel 标题栏:
ExcelApplication1.Caption := '应用程序调用 Microsoft Excel';
// 添加新工作簿:
ExcelWorkbook1.ConnectTo ( ExcelApplication1.Workbooks.Add ( EmptyParam , 0 ) ) ;
//添加新工作表:
Temp_Worksheet := ExcelWorkbook1.
WorkSheets.Add ( EmptyParam , EmptyParam , EmptyParam , EmptyParam , 0 ) As _WorkSheet ;
ExcelWorkSheet1.ConnectTo ( Temp_WorkSheet ) ;
//设置指定列的宽度(单位:字符个数),以第一列为例:
ExcelApplication1.ActiveSheet.Columns [ 1 ].ColumnsWidth := 5 ;
//设置第一行字体属性:
ExcelApplication1.ActiveSheet.Rows [ 1 ].Font.Name := '' 隶书 '' ;
ExcelApplication1.ActiveSheet.Rows [ 1 ].Font.Color := clBlue ;
ExcelApplication1.ActiveSheet.Rows [ 1 ].Font.Bold := True ;
ExcelApplication1.ActiveSheet.Rows [ 1 ].Font.UnderLine := True ;
//指定边框线宽度:
ExcelApplication1.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;//1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
//合并单元格
ExcelWorksheet1.Range['A3','A6'].MergeCells := True;//合并单元格
ExcelWorksheet1.Cells.Item[1,1].Value := 'dddd';//赋值
end;
end.