ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Delphi与Excel (转)

Delphi与Excel (转)

原创 Linux操作系统 作者:amyz 时间:2007-08-15 15:47:06 0 删除 编辑
Delphi与Excel (转)[@more@]

uses comobj,excel97,Excel2000;

//从Excel写数据到Access库
prodedure ExcelToMdb(EXLfile:string;);
var
  sheet,XLApp,workbook : variant;
  iRow,MaxRow:integer;
begin
  screen.Cursor:=crHourGlass; 
  try
  //创建对象
  XLApp:=createOleobject('Excel.Application');
  XLApp.displayAlerts:=false;
  XLApp.ScreenUpdating:=false;
  XLApp.WorkBooks.Add(EXLfile);
  workbook := XLApp.workbooks[1];
  sheet:=workbook.worksheets[1];
  //sheet:=XLApp.WorkBooks[1].worksheets[1];
 
 
  //取得最大行数  maxRow
  XLApp.ActiveCell.SpecialCells(xlLastCell).select;
  maxRow:=XLApp.ActiveCell.Row;  //最大行数

  //写数据到Access库
  ADOTable1.open;
  for iRow:=2 to MaxRow do
  if sheet.cells[iRow,1]<>'' then  //关键字不为空
  begin
  ADOTable1.Append ;
  ADOTable1.fieldByName('ID').asInteger:=
   strToInt(sheet.cells[iRow,1]);
  ADOTable1.fieldByName('code').asString:=sheet.cells[iRow,2]; //编码
  ADOTable1.fieldByName('name').asString:=sheet.cells[iRow,3]; //名称
  ADOTable1.post;
  end;

  finally
  if not VarIsEmpty(XLApp) then begin  //释放对象
  XLApp.displayAlerts:=false;
  XLApp.ScreenUpdating:=true;
  XLApp.quit;
  end;
  screen.Cursor:=crDefault;
  end;
end;

//=================== 其他一些属性方法 ===============//

  curRow:=XLApp.ActiveCell.Row; //当前行数
  XLApp.displayAlerts:=false; //运行宏时是否显示警告和消息
  XLApp.ScreenUpdating:=false;  //屏幕更新功能,提高速度;
  //结束运行后,ScreenUpdating 设回到 True

  XLApp.run('macroName',params...)//运行宏

  workbook.save ;
  workBook.SaveAs(AFileName,xlNormal,'','',False,False);


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

请登录后发表评论 登录
全部评论
  • 博文量
    3982
  • 访问量
    7895803