看到好多存储数据到EXCEL中的文件,却没有从EXCEL中读取数据的办法。
s:string;
v,w:variant;    v:=createoleobject('Excel.Application');
    v.visible := true;
    w:=v.workbooks.open('G:\test.xls');    s:=v.cells(1,1).value;//这一步出错
请问,这时候如何读取第一个格的数据到一个字符串变量中?非常感谢

解决方案 »

  1.   

    select *  from OPENROWSET('Microsoft.Jet.OLEDB.4.0','Extended properties=Excel 8.0;HDR=Yes;DATABASE=c:\BackUp\uexcel\Toll_fym.xls',sheet1$)
      

  2.   

    楼上朋友的方法不错,最好在ADOConnection的连符字符串中写上去,只需要比连ACCESS额外加两项内容就行了,这样操作数据就会像ACCESS和其它数据库一样了,每个Sheet表就相当于我们平时用的数据表。
    但它并不能完全读取Excel的文件。如一些格式信息等。这是一大不足。
    用自动化对象也是不错的,不过要编定大量的代码才能够实现,相当于普通桌面数据库的功能。还需要懂得VBA这门语言才行。
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var ExcelApp,MyWorkBook: OLEVariant;
        i,j: Integer;
    begin
        try
          ExcelApp:=CreateOleObject('Excel.Application');
          MyWorkBook:=CreateOleobject('Excel.Sheet');
        except
          application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',
               mb_OK+mb_IconStop);
          Exit;
        end;
        //ExcelApp.Visible := true;
        MyworkBook:= ExcelApp.workBooks.Open(ExtractFileDir(Application.ExeName)
          + '\' + Edit1.Text + '.xls');
        for i := 3 to 9 do begin
            //Read a row into table
            table1.Append;
            table1.FieldByName('Name').AsString := MyWorkBook.WorkSheets[1].Cells[i,1].Value;
            table1.FieldByName('Size').AsString := MyWorkBook.WorkSheets[1].Cells[i,2].Value;
            table1.FieldByName('Weight').AsString := MyWorkBook.WorkSheets[1].Cells[i,3].Value;
            table1.FieldByName('Area').AsString := MyWorkBook.WorkSheets[1].Cells[i,4].Value;
        end;
        //showmessage(MyWorkBook.WorkSheets[1].Columns[1].numberformat);
        //showmessage(MyWorkBook.WorkSheets[1].Columns[2].numberformat);    ExcelApp:=Unassigned; //释放VARIANT变量
    end;
    **************************打开Excel:
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      i:integer;
      aa:string;
    begin
      ExcelApplication1.FindFile;
      Try
        ExcelApplication1.Connect;
      Except
        MessageDlg('Excel 没有安装,请安装后运行。', mtError, [mbOk], 0);
        Abort;
      End;
      ExcelApplication1.Visible[0]:=false;
      ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[ExcelApplication1.Workbooks.count]);
      ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
      ListBox2.Items.Clear;
      SpeedButton1.Enabled:=true;  for i:=1 to  ExcelWorksheet1.Cells.Columns.Count do
      begin
        aa:=ExcelWorksheet1.Cells.item[1,i];
        if aa<>'' then
          ListBox2.Items.Add(ExcelWorksheet1.Cells.item[1,i]);
      end;执行导入:
    procedure TForm1.SpeedButton1Click(Sender: TObject);
    var
      i,j:integer;
      aa,bb:string;
      Myarray:array[1..100,1..2]of integer;begin
      if ListBox1.ItemIndex>0 then
        ListBox1.ItemIndex:=ListBox1.ItemIndex-1;
        label3.Caption:=inttostr(ListBox1.ItemIndex);
        //建立数据导入的联结关系,得到字段对应数组。
        //array〔i,1〕 记录目的字段;array〔i,2〕记录EXCEl中的列数。
        for i:=0 to listbox1.Items.Count-1 do begin
          myarray[i+1,1]:=i+1;
          for j:=1 to  ExcelWorksheet1.Cells.Columns.Count do begin
            aa:=ExcelWorksheet1.Cells.item[1,j];
            if trim(aa)='' then break;
            if listbox2.Items[i]=aa then
              myarray[i+1,2]:=j;
          end;
        end;    //数据导入
        i:=1;
        query1.Active:=true;
        query1.Edit;
        while trim(ExcelWorksheet1.Cells.item[i+1,1])<>'' do begin
          aa:=ExcelWorksheet1.Cells.item[i+1,1];
          if query1.Locate('ID',aa,[loCaseInsensitive])then begin
             query1.edit;
             for j:=1 to listbox1.Items.Count do begin
               if myarray[j,2]<>0 then
                 Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
             end;
           // showmessage('有重复记录。')
          end else begin
             query1.edit;
             query1.insert;
             for j:=1 to listbox1.Items.Count do begin
               if myarray[j,2]<>0 then
                 Query1.Fields[j-1].Value:=ExcelWorksheet1.Cells.item[i+1,myarray[j,2]];
             end;
          end;
          Label3.caption:=inttostr(i);
          i:=i+1;    end;
        query1.post;
        showmessage('共导入'+inttostr(i-1)+'条员工信息。');
    end;
      

  4.   

    直接在ACCESS中是可以导的,步骤如下:
    1,新建一个ACCESS数据库,点右建,选导入项。
    2,选择.XML驱动;选中相应的EXCEL文件,按照提示导入即可了。简单方便。效率还高,不要写代码的。:)
      

  5.   

    ADOConnection1.Execute('select * into abcd from [excel 8.0;database=F:\temp\Book1.xls].[Sheet1$]'); 这样看看
      

  6.   

    怎么会不能读呢,用OLE方法,如下(只给出读语句):
    a := ExcelApp.Activesheet.Cells.Item[i,j];