比如我要把一个数放在已经做好的EXCEL表格中,比如把100放在D5这个单元格...然后把这张EXCEL报表打印出来...要怎么实现呢???能否说详细一些...在此谢了..解决了立码送分..

解决方案 »

  1.   

    var xls:OleVariant;
    begin try
          xls:=CreateOleObject('Excel.Application');  //调用 Excel      需要在单元加上ComObj;
      except
       // Msgbox(sMsgNotInstallExcel,sTitleError,1);
        exit;
      end;if not (FileExists(extractfil+'\pring\报价单.xls')) then
      xls.WorkBooks.add   //增加一个表
        else xls.WorkBooks.open(extractfil+'\pring\报价单.xls');//打开一个设计好的表
         xls.cells(4,2):='100';//写入数据
        xls.ActiveSheet.PrintPreview;//打印一览end;
      

  2.   

    修改2樓的.
    uses comobj;var 
      xls:Variant;
    begin  try
       xls:=CreateOleObject('Excel.Application');  //调用 Excel      需要在单元加上ComObj;
      except
       // Msgbox(sMsgNotInstallExcel,sTitleError,1);
       xls:=unAssigned;
       exit;
      end; if not (FileExists(extractfil+'\pring\报价单.xls')) then
        xls.WorkBooks.add   //增加一个表
     else
        xls.WorkBooks.open(extractfil+'\pring\报价单.xls');//打开一个设计好的表
     xls.cells(4,2):='100';//写入数据
     xls.Visible:=True;
     xls.ActiveSheet.PrintPreview;//打印一览end;
      

  3.   

    谢谢二楼的回复,如果我打印后.要不保存退出应该怎么做呢??
    如果用xls.quit 会提示是否保存.
      

  4.   

    建议用DBGRIDEH,导出为EXCEL太方便了
      

  5.   

    如果你只想打印的话 建议你使用FastReport 来做报表 我一般做两个模式  一个把数据保存成一个xls文件 为了可以发email的
    如果是打印的 的话 我就用 FastReport 来做报表 
      

  6.   

    自由界面和报表的完美解决方案!
    http://www.anylib.com
      

  7.   

    谢谢二楼的回复,如果我打印后.要不保存退出应该怎么做呢??
    如果用xls.quit 会提示是否保存.//不出現提示
    xls.DisplayAlerts := False;
      

  8.   

    我觉得像控制Excel, Word用VB简单得多.
     konhon(优华)
    就OK
      

  9.   

    各位高手,按你们的方法,为何我用'xls.' 时,不会像控件那样自动出现属性和方法呢?
    你们又是如何知道“xls.DisplayAlerts ”之类的方法的呢?
      

  10.   

    (二) 使用Delphi 控件方法
    在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。 
    1) 打开Excel 
    ExcelApplication1.Connect;
    2) 显示当前窗口:
    ExcelApplication1.Visible[0]:=True;
    3) 更改 Excel 标题栏:
    ExcelApplication1.Caption := '应用程序调用 Microsoft Excel';
    4) 添加新工作簿:
    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
       5) 添加新工作表:
    var Temp_Worksheet: _WorkSheet;
    begin
    Temp_Worksheet:=ExcelWorkbook1.
    WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;
    ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);
    End;
       6) 打开已存在的工作簿:
    ExcelApplication1.Workbooks.Open (c:\a.xls
    EmptyParam,EmptyParam,EmptyParam,EmptyParam,
    EmptyParam,EmptyParam,EmptyParam,EmptyParam,
        EmptyParam,EmptyParam,EmptyParam,EmptyParam,0)
    7) 设置第2个工作表为活动工作表:
    ExcelApplication1.WorkSheets[2].Activate; 或
    ExcelApplication1.WorksSheets[ 'Sheet2' ].Activate;
    8) 给单元格赋值:
    ExcelApplication1.Cells[1,4].Value := '第一行第四列';
    9) 设置指定列的宽度(单位:字符个数),以第一列为例:
    ExcelApplication1.ActiveSheet.Columns[1].ColumnsWidth := 5;
    10) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
    ExcelApplication1.ActiveSheet.Rows[2].RowHeight := 1/0.035; // 1厘米
    11) 在第8行之前插入分页符:
    ExcelApplication1.WorkSheets[1].Rows[8].PageBreak := 1;
    12) 在第8列之前删除分页符:
    ExcelApplication1.ActiveSheet.Columns[4].PageBreak := 0;
    13) 指定边框线宽度:
    ExcelApplication1.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;
    1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )
    14) 清除第一行第四列单元格公式:
    ExcelApplication1.ActiveSheet.Cells[1,4].ClearContents;
    15) 设置第一行字体属性:
    ExcelApplication1.ActiveSheet.Rows[1].Font.Name := '隶书';
    ExcelApplication1.ActiveSheet.Rows[1].Font.Color := clBlue;
    ExcelApplication1.ActiveSheet.Rows[1].Font.Bold := True;
    ExcelApplication1.ActiveSheet.Rows[1].Font.UnderLine := True;
    16) 进行页面设置:
     a.页眉:
        ExcelApplication1.ActiveSheet.PageSetup.CenterHeader := '报表演示';
    b.页脚:
        ExcelApplication1.ActiveSheet.PageSetup.CenterFooter := '第&P页';
    c.页眉到顶端边距2cm:
        ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 2/0.035;
    d.页脚到底端边距3cm:
        ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 3/0.035;
    e.顶边距2cm:
        ExcelApplication1.ActiveSheet.PageSetup.TopMargin := 2/0.035;
    f.底边距2cm:
        ExcelApplication1.ActiveSheet.PageSetup.BottomMargin := 2/0.035;
    g.左边距2cm:
        ExcelApplication1.ActiveSheet.PageSetup.LeftMargin := 2/0.035;
    h.右边距2cm:
        ExcelApplication1.ActiveSheet.PageSetup.RightMargin := 2/0.035;
    i.页面水平居中:
        ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;
    j.页面垂直居中:
        ExcelApplication1.ActiveSheet.PageSetup.CenterVertically := 2/0.035;
    k.打印单元格网线:
        ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines := True;
    17) 拷贝操作:
    a.拷贝整个工作表:
        ExcelApplication1.ActiveSheet.Used.Range.Copy;
    b.拷贝指定区域:
        ExcelApplication1.ActiveSheet.Range[ 'A1:E2' ].Copy;
    c.从A1位置开始粘贴:
        ExcelApplication1.ActiveSheet.
      

  11.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, {if delphi6.0 add ...}Variants{d6};type
      TForm1 = class(TForm)
        Button1: TButton;
        CheckBox1: TCheckBox;
        Button2: TButton;
        OpenDialog1: TOpenDialog;
        Button3: TButton;
        Button4: TButton;
        Button5: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
      private
        v:Variant;
        Sheet: Variant;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}uses Comobj;//打开EXCEL新建文件
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      try
        v:= CreateOleObject('Excel.Application');
        v.Visible := CheckBox1.Checked;//是否显示
        v.Workbooks.Add;//新建EXCEL文件
        v.Workbooks[1].WorkSheets[1].Name := 'DELPHI演示';
    //    Sheet:= v.Workbooks[1].WorkSheets['DELPHI演示'];//等效下面的语句
        Sheet:= v.Workbooks[1].WorkSheets[1];
        Sheet.Cells[1,1] :='DELPHI盒子';
        Sheet.Cells[2,1] :='http://www.delphibox.com';
      except
        Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
        v.DisplayAlerts := false;//是否提示存盘
        v.Quit;//如果出错则退出
        exit;
      end;
      Application.Restore;
      Application.BringToFront;
    end;//打开存在的EXCEL文档
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
      begin
        try
          v:= CreateOleObject('Excel.Application');
          v.Visible := CheckBox1.Checked;
          v.Workbooks.Open(OpenDialog1.FileName);
      //    Sheet := v.Workbooks[1].WorkSheets[1];
        except
          Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
          v.DisplayAlerts := false;
          v.Quit;
          exit;
        end;
        Application.Restore;
        Application.BringToFront;
      end;
    end;//关闭EXCEL并退出
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      try
        If not varIsEmpty(v) then
        begin
      //    如果需要在关闭前确定是否存盘,加入:
      //    v.DiaplayAlert:=true; //确定存盘
      //    v.DiaplayAlert:=false;//不存盘,直接退出
          v.WorkBooks[1].Close(True, 'C:\untitled.xls'); //取文件名退出
          v.quit;
        end;
      finally
        Close;
      end;
    end;//EXCEL打印页面设置
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
      begin
        try
          v:= CreateOleObject('Excel.Application');
          v.Visible := CheckBox1.Checked;
          v.Workbooks.Open(OpenDialog1.FileName);
          Sheet:=v.Workbooks[1].WorkSheets[1];
          Sheet.PageSetup.PrintTitleRows :='$1:$3'; //页眉
          Sheet.PageSetup.PrintTitleColumns := '';
          Sheet.PageSetup.LeftFooter := ' 注:页脚'+' 总共&N页'+'--第&P页'; //页脚
          Sheet.PageSetup.LeftMargin := 30; //设置边距
          Sheet.PageSetup.RightMargin := 30;
          Sheet.PageSetup.TopMargin := 30;
          Sheet.PageSetup.BottomMargin := 50;
        //  Sheet.PageSetup.PrintQuality := 400; //分辨率(根据打印机确定)
          Sheet.PageSetup.CenterHorizontally := True;//是否水平居中
          Sheet.PageSetup.CenterVertically := True; //是否垂直居中
          Sheet.PageSetup.Orientation := 2; //横向打印
          Sheet.PageSetup.Draft := False; //非草稿模式
        //  Sheet.PageSetup.FirstPageNumber := xlAutomatic;
          Sheet.PageSetup.BlackAndWhite := True; //黑白稿
          Sheet.PageSetup.Zoom := 100; //缩放
          sheet.PrintPreView; //打印预览
        except
          Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
          v.DisplayAlerts := false;
          v.Quit;
          exit;
        end;
      end;
    end;//EXCEL文档格式设置
    procedure TForm1.Button5Click(Sender: TObject);
    var
      Range: Variant;
    begin
      if OpenDialog1.Execute then
      begin
        try
          v:= CreateOleObject('Excel.Application');
          v.Visible := CheckBox1.Checked;
          v.Workbooks.Open(OpenDialog1.FileName);
          Range := v.Workbooks[1].WorkSheets[1].Range['A2:G2'];//单元格从A2到M2
          Range.Merge; //合并单元格
          Range.Rows.RowHeight := 50; //设置行高
          Range.Borders.LineStyle := 1; //加边框
          Range.Columns[2].ColumnWidth := 12; // 设置列宽
          Range.FormulaR1C1 := '合并区';
          Range.HorizontalAlignment := 3;//xlCenter(水平对齐方式)
          Range.VerticalAlignment := 2;//xlCenter(垂直对齐方式)
          Range.Characters.Font.Name := '宋体'; //字体
          Range.Characters.Font.FontStyle := '加粗';
          Range.Characters.Font.Size := 15;
          Range.Characters.Font.OutlineFont := False; //是否有下划线
          Range.Characters.Font.ColorIndex := 0;//xlAutomatic; //颜色
        except
          Showmessage('初始化Excel失败,可能没装Excel,或者其他错误;请重起再试。');
          v.DisplayAlerts := false;
          v.Quit;
          exit;
        end;
      end;
    end;end.