table1里面有三个字段: x   y   z(  N  型数据 )
现要将excel文件中的数据读入表中,请问如何实现?拜托了!(原表中记录为空)我用的一个opendialog1,和一个button1键组合将excel文件读入Ttable1(数据库为自带paradox)

解决方案 »

  1.   

    可以直接在SQL server 中转换
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,ComObj, StdCtrls, DB, ADODB, Grids, DBGrids;type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        ADOTable1: TADOTable;
        DataSource1: TDataSource;
        OpenDialog1: TOpenDialog;
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
    MSExcel: Variant;
    i: Integer;
    begin
    OpenDialog1.Filter:='*.xls';
    if OpenDialog1.Execute then
    begin
        ADOTable1.Close;
        ADOTable1.Open;
        MSExcel:=CreateOLEObject('Excel.Application');
        MSExcel.WorkBooks.Open(OpenDialog1.FileName);
        MSExcel.Visible:=False;
      //从有数据的行逐行读入数据
        for i:=1 to MSExcel.ActiveSheet.UsedRange.Rows.Count do
        begin
             ADOTable1.Insert;
             ADOTable1.FieldByName('a').AsString := MSExcel.Cells[i,1].Value;
             ADOTable1.FieldByName('b').AsString := MSExcel.Cells[i,2].Value;
             //数据格式控制
             //MSExcel.Cells[i,3].NumberFormat:='@';
             ADOTable1.FieldByName('c').AsString := MSExcel.Cells[i,3].Value;
             ADOTable1.FieldByName('d').AsString := MSExcel.Cells[i,4].Value;
             ADOTable1.post;
        end;
        MSExcel.ActiveWorkBook.Close;
        MSExcel.Quit;
    end;
    end;
    end.
      

  3.   

    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;
      

  4.   

    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;