论坛找了好久,没找到....提供一点支持

解决方案 »

  1.   

    把EXCEL作为库,读出记录,循环写入就好了
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
     i,j:integer;
      
    begin
      Opendialog1.Execute;
      ExcelApplication1.Connect;
      ExcelApplication1.Visible[0]:=false;
      excelapplication1.Workbooks.Open(Opendialog1.FileName ,
      emptyParam,emptyParam,emptyParam,emptyParam,emptyParam,
      emptyParam,emptyParam,emptyParam,emptyParam,emptyParam,
      emptyParam,emptyParam,emptyParam,emptyParam,0);
      ExcelApplication1.Disconnect;//出现异常情况时关闭
      ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
      ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
       for   i:=1   to  ExcelWorkbook1.cells.rows do
       for   j:=1   to   6   do
      begin   
      if trim(excelworksheet1.cells.item[i+1,1])<> ''  then
      begin
      stringgrid1.rowCount:=i+1;
      stringgrid1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i+1,j];
      end
      else
      begin
      ExcelApplication1.Disconnect;   
      ExcelApplication1.Quit;
    end;
    end;
    end;以上是我找来的代码,运行后取出的全是些数值,,,不是我单元格的内容....
    请老师.........以上代码取出数据不是我要的中数据,咋全是些数字呢?????请老师指正.
      

  3.   

    可以使用ADO的方法操作excel..参考下面的文章,不过是VC写的,要改一下:
    http://blog.csdn.net/tabby/archive/2007/11/16/1889217.aspx
      

  4.   


    procedure Tfrmmain.exceltorec(fname: string);
    var
      ss:string;
      i:integer;
      ea:variant;
    begin
      try
        EA:=CreateOleObject('Excel.Application');
      except
        application.MessageBox('NO EXCEL','', MB_OK);
        EXIT;
      end;
      EA.Visible := true;
      EA.WorkBooks.Open(fname);
      sgrid.Cells[1,1]:=EA.Cells[1,1];
      sgrid.Cells[1,2]:=EA.Cells[1,2];
      ea.quit;
      ea:=unassigned;
    end;
      

  5.   

    下面是我用的代碼,有一edit控件用于xianshi,用一button操作
    var
     OpenDialog1:TOpenDialog;
     ExcelApp:Variant;
     expath:String;
     ExCountRow,ExCountCol:integer;
     i,j:integer;
    begin
      stringgrid1.Clear;
      stringgrid1.RowCount:=2;
      stringgrid1.ColCount:=2;
      OpenDialog1:=TOpenDialog.Create(Application);
      with OpenDialog1 do
      begin
        Title:='xls選擇';
        Filter:='Excel File(*.xls)|*.xls';
        DefaultExt:='*.xls';
      end;  if OpenDialog1.Execute then
      begin
        Edit1.Text:=opendialog1.FileName; 
      end;
      opendialog1.Free;
      opendialog1:=nil;  application.ProcessMessages;
      try
        Screen.Cursor:=crhourglass;
        ExcelApp:=CreateOleObject('excel.application');
        ExcelApp.visible:=false;
        ExcelApp.workbooks.open(edit1.Text);    ExCountRow:=0;
        while not(length(ExcelApp.cells[ExCountRow+1,1])=0) do
        begin
          Inc(ExCountRow);
        end;    ExCountCol:=0;
        while not(length(ExcelApp.cells[1,ExCountCol+1])=0) do
        begin
          Inc(ExCountCol);
        end;
        stringgrid1.RowCount:=ExCountRow;
        stringgrid1.ColCount:=ExCountCol+1;
        if (excountrow>0) and (excountcol>0) then
        begin
          for j:=1 to advstringgrid1.ColCount do
            stringgrid1.Cells[j,0]:=ExcelApp.cells[1,j].value;      for i:=1 to stringgrid1.RowCount do
            for j:=1 to stringgrid1.ColCount do
              stringgrid1.Cells[j,i]:=ExcelApp.cells[i+1,j].value;
        end;    ExcelApp.workbooks.close;
        Excelapp.quit;
        stringgrid1.AutoSize:=true;
        screen.Cursor:=crdefault;
      finally
        ExcelApp.workbooks.close;
        Excelapp.quit;
        screen.Cursor:=crdefault;
      end;
    end;
      

  6.   

    不好意思,我用的是advstringgrid控件,上面有一行沒改,你把我的advstringgrid1改成你的stringgrid1就可以了
      

  7.   

    我要知道有效的行及列的数值...不如何写OLE不像控件有方法提示的...
      

  8.   

    是从EA.Cells[1,1]开始的,有效的行只能自己判断了,EA.Cells[1,1]的值为字符串型的,你可以判断是否为空 
      

  9.   

     sgrid.Cells[0,0]:='序号';
      i:=0;
      for j:= 1 to 6 do
      if EA.Cells[i+1,j]<>'' then
       begin
        sgrid.Cells[j,i]:=EA.Cells[j,i+1];
        //sgrid.Cells[2,0]:=EA.Cells[1,2];
        ea.quit;
        ea:=unassigned;以上是我加的判断...运行出错...
      

  10.   

    关于用程序读取Excel的你参照一下这个
    http://blog.csdn.net/zxf_feng/archive/2008/03/03/2142264.aspx
    对于向stringgrid写数据的参照一下这个
    procedure SetGridInfo(StrGrid:TStringGrid;sOraQuery:TOraQuery);
    var
      i,j:integer;
    begin
      StrGrid.RowCount:=2;
      i:=1;
      while not sOraQuery.eof do
      begin
        StrGrid.Cells[0,i]:=inttostr(i);
        for j:=1 to sOraQuery.FieldCount do
        begin
          StrGrid.Cells[j,i]:=sOraQuery.Fields[j-1].AsString;
        end;
        i:=i+1;
        StrGrid.RowCount:=StrGrid.RowCount+1;
        sOraQuery.Next;
      end;
    end;
      

  11.   

     if EA.Cells[i+1,j] <>'' then ->if EA.Cells[i+1,j] <>' '(有个空格) then 
      

  12.   

    结贴了...用OLE还是没有整成............
      

  13.   

    8楼的,你是用advstringgrid 这个空间改成StringGrid 的,那么你认为advstringgrid能有的属性在StringGrid控件一样可以用么?