rt

解决方案 »

  1.   

    这是我找到的,看对你是否有帮助
    1) 显示当前窗口:
    ExcelApp.Visible := True;2) 更改 Excel 标题栏:
    ExcelApp.Caption := '应用程序调用 Microsoft Excel';3) 添加新工作簿:
    ExcelApp.WorkBooks.Add;4) 打开已存在的工作簿:
    ExcelApp.WorkBooks.Open( 'C:\Excel\Demo.xls' );5) 工作表保存:
    if not ExcelApp.ActiveWorkBook.Saved then
       ExcelApp.ActiveSheet.PrintPreview;6) 关闭工作簿:
    ExcelApp.WorkBooks.Close;
      

  2.   

    http://www.google.com/search?q=delphi+%E6%8E%A7%E5%88%B6excel&ie=UTF-8&oe=UTF-8&hl=zh-CN&lr=
      

  3.   

    {创建OLE对象}function My_CreateOleObject(const ClassName: string; out Ole_Handle: IDispatch): Boolean;varClassID: TCLSID;l_Result: HResult;beginResult := False;l_Result := CLSIDFromProgID(PWideChar(WideString(ClassName)), ClassID);if (l_Result and $80000000) = 0 thenbeginl_Result := CoCreateInstance(ClassID, nil, CLSCTX_INPROC_SERVER orCLSCTX_LOCAL_SERVER, IDispatch, Ole_Handle);if (l_Result and $80000000) = 0 thenResult := True;end;end;varl_Excel_Handle: IDispatch;beginif FShow_Progress = True thenbeginCreate_Run_Excel_Form(nil);FRun_Excel_Form.Show;end;if My_GetActiveOleObject('Excel.Application', l_Excel_Handle) = False thenif My_CreateOleObject('Excel.Application', l_Excel_Handle) = False thenbeginFRun_Excel_Form.Free;FRun_Excel_Form := nil;raise exception.Create('启动Excel失败,可能没有安装Excel!');Result := False;Exit;end;FExcel_Handle := l_Excel_Handle;if FShow_Progress = True thenbeginFRun_Excel_Form.Free;FRun_Excel_Form := nil;end;Result := True;end;{插入新的工作博}function TDBGridExport.New_Workbook: Boolean;vari: Integer;beginResult := True;tryFWorkbook_Handle := FExcel_Handle.Workbooks.Add;exceptraise exception.Create('新建Excel工作表出错!');Result := False;Exit;end;if FTitle <> '' thenFWorkbook_Handle.Application.ActiveWindow.Caption := FTitle;if FSheetName <> '' thenbeginfor i := 2 to FWorkbook_Handle.Sheets.Count doif FSheetName = FWorkbook_Handle.Sheets[i].Name thenbeginraise exception.Create('工作表命名重复!');Result := False;exit;end;tryFWorkbook_Handle.Sheets[1].Name := FSheetName;exceptraise exception.Create('工作表命名错误!');Result := False;exit;end;end;end;
      

  4.   

    constructor TReport.Create(reportName: String);
    begin
        excel := UnAssigned;
        sheet := UnAssigned;
        workBook := UnAssigned;    curRow := 1;    if reportName <> '' then
           strReportName := reportName   //strReportName 为全局变量
        else
            strReportName := '未命名';
    end;
      

  5.   

    这是俺写的一个程序,你把无用的去掉即可;(AdsData:连接的数据库,SName: 要保存的名称Title:生成Excel的路径procedure TDataInForm.WriteExcel(AdsData: TDataSet; sName, Title: string);
    var
      ExcelApplication2: TExcelApplication;
      ExcelWorksheet2: TExcelWorksheet;
      ExcelWorkbook2: TExcelWorkbook;
      i, j: integer;
      filename: string;
    begin
      filename := sName;// 要保存的文件名称
    try
        ExcelApplication2 := TExcelApplication.Create(Application);
        ExcelWorksheet2 := TExcelWorksheet.Create(Application);
        ExcelWorkbook2 := TExcelWorkbook.Create(Application);
        ExcelApplication2.Connect;
      except
        Application.Messagebox('Excel 没有安装!', '警告', MB_ICONERROR + mb_Ok);
        Abort;
      end;
      try
        ExcelApplication2.Workbooks.Add(EmptyParam, 0);
        ExcelWorkbook2.ConnectTo(ExcelApplication2.Workbooks[1]);
        ExcelWorksheet2.ConnectTo(ExcelWorkbook2.Worksheets[1] as _worksheet);
        AdsData.Close;
        AdsData.Open;//
        AdsData.First;
        for j := 0 to AdsData.Fields.Count - 1 do
          begin
            ExcelWorksheet2.Cells.item[1,j+1]:=AdsData.Fields[j].DisplayLabel;
            ExcelWorksheet2.Cells.item[1,j+1].font.size := '10';
          end;
        i:=0;
        while not AdsData.Eof do
          begin
            for j := 0 to AdsData.Fields.Count- 1 do
              begin
                ExcelWorksheet2.Cells.item[i+2,j+1]:=AdsData.Fields[j].Asstring;
                ExcelWorksheet2.Cells.item[i+2, j + 1].font.size :=10;
              end;
            AdsData.Next;
            i:=i+1;
          end;
        ExcelWorksheet2.Columns.AutoFit;
        //ExcelWorksheet2.Cells.item[1, 2] := Title;
        //ExcelWorksheet2.Cells.Item[1, 2].font.size := 14;
        ExcelWorksheet2.SaveAs(filename);
        Application.Messagebox(pchar('数据成功导出!'), '提示', mb_Ok);
      finally
        ExcelApplication2.Disconnect;
        ExcelApplication2.Quit;
        ExcelApplication2.Free;
        ExcelWorksheet2.Free;
        ExcelWorkbook2.Free;
      end;
      flag:=1;
    end;