说具体点行么?

解决方案 »

  1.   

    var
      i,j:integer;
      excel_sheet:variant;
    begin
      try
        excel_sheet:=CreateOleObject('Excel.Application');
      except
        showmessage('无法启动 MS Excel');
        exit;
      end;
      Excel_sheet.WorkBooks.Add;
      for i:=0 to StringGrid1.Cols.Count-1 do
        for j:=0 to StringGrid1.Rows.Count-1 do
        excel_sheet.cells.item[i+1,j+1]:=StringGrid1.Cells[i,j];
      end;
      excel_sheet.ActiveWorkBook.saveas(Filepath);
      excel_sheet.application.quit;                //關閉 MS Excel
    end;
      

  2.   

    跟你一个我做系统中写的一个公共过程,直接用了。
    procedure Tpublic.SaveToExcel(title, tip_msg: string; dbgrid1: TDbgrid);
    var
      XL, Xarr,sumstr: Variant;
      I,clums: Integer;  //列数
      j,rows : Integer;  //行数
      fieldcunt:integer;//dbgrid1的字段个数
    begin
    {note the ComObj (example OleAuto not correct) in the uses}
    // Create an array of query element size
        try
          screen.Cursor:=crhourglass;
          fieldcunt:=dbgrid1.FieldCount;
          Xarr:=VarArrayCreate([1,fieldcunt],varVariant);
          XL:=CreateOLEObject('Excel.Application'); // Ole object creation
          XL.WorkBooks.add;    for i := 0 to dbgrid1.Columns.Count - 1 do
          begin  //添加标题信息
             XL.range[chr(65+i)+'2',chr(65+i)+'2'] := dbgrid1.Columns.Items[i].Title.Caption;
          end;
          j := 3; //行数
          dbgrid1.DataSource.DataSet.First;
          while not dbgrid1.DataSource.DataSet.Eof do
          begin  //添加内容信息
             for i:= 0 to fieldcunt - 1 do //列数
             begin
               XL.range[chr(65+i)+inttostr(j),chr(65+i)+inttostr(j)]  := dbgrid1.Columns.Items[i].Field.AsString;
               clums:=i;
             end;
             Inc(j);
             rows:=j;
             dbgrid1.DataSource.DataSet.Next;
          end;
    //---------------进行求和操作---------------------
        //sumstr:='=SUM(R[-'+inttostr(rows-3)+']C:R[-1]C)';
        //XL.Cells[j,2].Formula:='=SUM(b3:b'+inttostr(j-1)+')';
       //xl.Cells.Item[j,2]:='=SUM(b3:b'+inttostr(j-1)+')';
        //for i:=2 to clums do
         // begin
            XL.Cells[i,rows].Formula:='=SUM('+chr(64+i)+'3:'+chr(64+i)+inttostr(rows-1)+')';
            //XL.Range[chr(64+i)+'3',chr(64+i)+inttostr(rows-1)].Select;
            //XL.Range[chr(64+i)+inttostr(rows)].Activate;
            //XL.ActiveCell.FormulaR1C1 :='=SUM(R[-11]C:R[-1]C)';
            //XL.ActiveCell.FormulaR1C1 :=sumstr;
         // end;
          //XL.Range['B4','B15'].Select;
    //-----------------选择所有的信息设置内容字体及大小----------
          XL.cells.select; // Select everything
          XL.Selection.Font.Name:='宋体';
          XL.Selection.Font.Size:=12;
          XL.selection.Columns.AutoFit;
    //----------------再次选择设置标题------------------------------------
          XL.RANGE['A1',chr(65+dbgrid1.Columns.Count-1)+'1'].SELECT;
          XL.SELECTION.Merge; //对选中的单元格进行合并操作
          XL.SELECTION.value:=title;
          XL.Selection.Font.Name:='宋体';
          XL.Selection.Font.Size:=16;
          XL.Selection.Font.ColorIndex := 3 ;
          XL.Selection.Font.Bold := True;//-------------------------------------------------------------------
          screen.Cursor:=crdefault;
          XL.visible:=true;
          except
              messagebox(0,pchar(tip_msg),'系统提示',mb_ok+mb_iconwarning+mb_taskmodal);
        end;
    end;
      

  3.   

    建议用TMS的ADVSTRINGGRID控件,功能强大,不用自己再做重复的工作了
    WWW。51ELPHI。COM有下载
      

  4.   

    procedure outputExcel(Fdbgrid:Tdbgrid;title:string);
    var
    icount2,iCount,icount3, jCount: Integer;
    XLApp: Variant;
    Sheet: Variant;
    begin
      Screen.Cursor := crHourGlass;
      if not VarIsEmpty(XLApp) then
      begin
        XLApp.DisplayAlerts := False;
        XLApp.Quit;
        VarClear(XLApp);
      end;
        //通过ole创建Excel对象
    try
    XLApp := CreateOleObject('Excel.Application');
    except
    Screen.Cursor := crDefault;
    Exit;
    end;
    XLApp.WorkBooks.Add[XLWBatWorksheet];
    XLApp.WorkBooks[1].WorkSheets[1].Name := title;
    Sheet := XLApp.Workbooks[1].WorkSheets[title];
    if not Fdbgrid.DataSource.DataSet.Active then
    begin
    Screen.Cursor := crDefault;
    Exit;
    end;
    Fdbgrid.DataSource.DataSet.first;
    Sheet.cells[ 1, 1] :=title;
    for iCount := 0 to Fdbgrid.Columns.Count - 1 do
    begin
    if Fdbgrid.Columns[icount].Visible=true then
    begin
      Sheet.cells[2, iCount2 + 1] := Fdbgrid.Columns.Items[iCount].Title.Caption;
      iCount2:=iCount2+1;
    end
    end;
    jCount := 2;
    while not Fdbgrid.DataSource.DataSet.Eof do
    begin
    for iCount := 0 to Fdbgrid.Columns.Count - 1 do
    begin
    if Fdbgrid.Columns[icount].Visible=true then
    begin
    Sheet.cells[jCount + 1, iCount3 + 1] := Fdbgrid.Columns.Items[iCount].Field.AsString;
    iCount3:=iCount3+1;
    end
    end;
    iCount3:=0;
    Inc(jCount);
    Fdbgrid.DataSource.DataSet.Next;
    end;
    XlApp.Visible := True;
    Screen.Cursor := crDefault;
    end;
    end.