如何用Delphi在Excel文件中的某个单元格加个下拉框

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleServer, ExcelXP;

type
  TForm1 = class(TForm)
    ExcelApplication1: TExcelApplication;
    ExcelWorkbook1: TExcelWorkbook;
    ExcelWorksheet1: TExcelWorksheet;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ExcelApplication1.Connect;
  ExcelApplication1.Visible[0]:=True;
  ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(null,0));
  ExcelWorksheet1.ConnectTo(ExcelApplication1.ActiveCell.Worksheet);

 ExcelApplication1.Cells.Item[3,3].Validation.Add(xlValidateList,xlValidAlertStop,
  xlBetween,'新疆,北京,上海');
end;

end.

图1

图2

 

 

如果是CreateObject方式,连接EXCEL。则代码如下:

var ExcelApp: Variant;
  i, col: integer;
  excelhong: string;
begin
  ExcelApp := CreateOleObject('Excel.Application');
//更改 Excel 标题栏
  ExcelApp.Caption := '应用程序调用 Microsoft Excel'; ;
//添加新工作簿:
  ExcelApp.Visible := True;
  ExcelApp.WorkBooks.Add;

  ExcelApp.Cells.Item[3, 3].Validation.Add(xlValidateList, xlValidAlertStop, xlBetween, '新疆,北京,上海');
end;


posted @ 2008-01-24 21:20  懒是一种生活智慧,但是懒惰不是!  阅读(1066)  评论(0编辑  收藏  举报