Delphi中操作Excel

var
xlsFile: String;
xlsApp, Sheet: OleVariant;
begin
Result := '';
try
xlsFile := 'c:file1.xls';
xlsApp := CreateOleObject('Excel.Application'); //需要引用 ComObj 单元
try
//打开xls文件
xlsApp.WorkBooks.Open(xlsFile);
Sheet := xlsApp.Sheets[1];
try
//光标归位到Excel档第一页第一格
if xlsApp.ActiveSheet.Index <> Sheet.Index then
Sheet.Select;
Sheet.Cells.Item[1,1].Select;
//显示第3行第5列的数据,可参考此句用循环与判定取出xls中所有数据
ShowMessage(VarToStr(Sheet.Cells.Item[3,5].Value));
end;
finally
Sheet := unassigned;
xlsApp.Quit;
xlsApp := Unassigned;
end;
except
ShowMessage('无法启动Excel程式,请检查!');
end;
end;[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20037923/viewspace-1015967/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/20037923/viewspace-1015967/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用方便,功能强大。 type FileCheckResult = (fcrNotExistend,fcrNotXSLFile,fcrValidXSL); //文件不存在,不是XSL文件,合法的XSL文件 TOLEExcel = class(TComponent) private FExcelCreated: Boolean; FVisible: Boolean; FExcel: Variant; //Excel程序对象 FWorkBook: Variant; //Excel工作簿对象 FWorkSheet: Variant; //Excel工作簿 工作表对象 FCellFont: TFont; //单元格字体对象 FTitleFont: TFont; // FFontChanged: Boolean; FIgnoreFont: Boolean; FFileName: TFileName; //********************************************自己添加*****************************// FCreateFromFile:Boolean; //指示是否打开已有文件 FExcelCaption:string; //用程序打开Excel的窗体标 //*********************************来自U_Report*****************************// FRCPrePage:Integer; //每页显示的记录数 FMax:Integer; //最大的数组个数 procedure SetExcelCellFont(var Cell: Variant); procedure SetExcelTitleFont(var Cell: Variant); procedure GetTableColumnName(const Table: TTable; var Cell: Variant); procedure GetQueryColumnName(const Query: TQuery; var Cell: Variant); procedure GetFixedCols(const StringGrid: TStringGrid; var Cell: Variant); procedure GetFixedRows(const StringGrid: TStringGrid; var Cell: Variant); procedure GetStringGridBody(const StringGrid: TStringGrid; var Cell: Variant); protected procedure SetCellFont(NewFont: TFont); procedure SetTitleFont(NewFont: TFont); procedure SetVisible(DoShow: Boolean); function GetCell( ARow,ACol: Integer): string; procedure SetCell(ACol, ARow: Integer; const Value: string); function GetDateCell(ACol, ARow: Integer): TDateTime; procedure SetDateCell(ACol, ARow: Integer; const Value: TDateTime); //*********************************************自己添加************************************// procedure SetCaption(ACaption:string);//设置打开文件后,Excel主程序的窗体标题 function GetCapiton:string;//返回打开文件后,Excel主程序的窗体标题 public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure C
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值