欢迎光临
我们一直在努力

delphi与excel_delphi教程

建站超值云服务器,限时71元/月

 

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);

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » delphi与excel_delphi教程
分享到: 更多 (0)