分享

通过Excel控件进行数据的打印或者预览

 quasiceo 2013-02-25
如何把Excel预览窗口嵌入我的程序做报表? 浏览:157
加入我的收藏
我用Excel做报表,在预览的时候希望窗口和我的Form集成。如下图,请教如何实现:
按此在新窗口浏览图片

别说用其它报表工具,因为Excel的通用性极好,函数丰富。生成的图表能二次编辑,随着数据列的变化而变化。外发也不用培训,过滤,排序,筛选到其它位置等都很容易实现了,这是其它工具做不到的。
----------------------------------------------
-


男 feverkim2k3 (无赖飞猪) ▲▲▲▲△ -
盒子活跃会员
2013-2-24 16:02:43
报表区域用一个OLEContainer来代替就是了。然后打开报表调用
OleContainer1.CreateObjectFromFile('d:\Format.xls',False); 
这样就会自动打开EXCEL文件。
此帖子包含附件:
JPEG 图像
大小:85.5K
----------------------------------------------
-
男 sbzldlb (边缘人) ▲▲▲▲△ -
盒子活跃会员
2013-2-24 19:16:41
ole是可以的
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
  ExcelApp: OleVariant;
begin
  try
    ExcelApp := CreateOleObject('EXCEL.application');
  except
    Application.MessageBox('请安装EXCEL再打印!', '提示',MB_ok + MB_iconinformation + mb_applmodal);
    Exit;
  end;
  try
    try
      ExcelApp.Visible := True;
      ExcelApp.Workbooks.open(FileName);
      ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
      ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2;  //页面水平居中
//      ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2;   //页面垂直居中
      //ExcelApp.Cells.Select;
      if Range = '' then
        ExcelApp.Cells.Select
      else
        ExcelApp.Range[ Range ].Select;
 
      
      ExcelApp.Selection.Font.Size := 11;
      ExcelApp.Selection.borders.LineStyle := 7;
      ExcelApp.Selection.RowHeight := 1/0.035;
      ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
      ExcelApp.Selection.Rows[3].WrapText := True;
 
      if AutoFit then
      begin
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
 
        ExcelApp.Selection.WrapText := True;
        ExcelApp.Selection.Rows.AutoFit; //设为自动列宽,
        //FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ Range ].Select;
      end;
 
 
 
      
      ExcelApp.ActiveSheet.PrintPreview;
      //ExcelApp.ActiveSheet.PrintOut;
    except
    end;
  finally
    ExcelApp:=Null;
  end;
end;

通过Excel控件进行数据的打印或者预览

 下面的单元是利用Excel,进行数据的打印功能。
函数包括了PrintView、PrintOut
参数含义:
FileName:需要打印的Excel文件路径
AUtoFile:是否列宽字段适应
Range:指定某一个格子选择 默认是空
 
具体是否页面垂直或者水平居中,看实际的情况
--------------------------------------------------------------------------------------------------
 
unit uExcelPrint;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,ComObj;
 
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
procedure PrintOut(FileName:String;const Range:String = '';const AutoFit:boolean = False);
 
implementation
 
procedure PrintView(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
  ExcelApp: OleVariant;
begin
  try
    ExcelApp := CreateOleObject('EXCEL.application');
  except
    Application.MessageBox('请安装EXCEL再打印!', '提示',MB_ok + MB_iconinformation + mb_applmodal);
    Exit;
  end;
  try
    try
      ExcelApp.Visible := True;
      ExcelApp.Workbooks.open(FileName);
      ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
      ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2;  //页面水平居中
//      ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2;   //页面垂直居中
      //ExcelApp.Cells.Select;
      if Range = '' then
        ExcelApp.Cells.Select
      else
        ExcelApp.Range[ Range ].Select;
 
      
      ExcelApp.Selection.Font.Size := 11;
      ExcelApp.Selection.borders.LineStyle := 7;
      ExcelApp.Selection.RowHeight := 1/0.035;
      ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
      ExcelApp.Selection.Rows[3].WrapText := True;
 
      if AutoFit then
      begin
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
 
        ExcelApp.Selection.WrapText := True;
        ExcelApp.Selection.Rows.AutoFit; //设为自动列宽,
        //FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ Range ].Select;
      end;
 
 
 
      
      ExcelApp.ActiveSheet.PrintPreview;
      //ExcelApp.ActiveSheet.PrintOut;
    except
    end;
  finally
    ExcelApp:=Null;
  end;
end;
 
procedure PrintOut(FileName:String;const Range:String = '';const AutoFit:boolean = False);
var
  ExcelApp: OleVariant;
begin
  try
    ExcelApp := CreateOleObject('EXCEL.application');
  except
    Application.MessageBox('请安装EXCEL再打印!', '提示',MB_ok + MB_iconinformation + mb_applmodal);
    Exit;
  end;
  try
    try
      ExcelApp.Visible := True;
      ExcelApp.Workbooks.open(FileName);
      //ExcelApp.ActiveSheet.PrintPreview;
      ExcelApp.ActiveSheet.PageSetup.Orientation := 2;
      if Range = '' then
        ExcelApp.Cells.Select
      else
        ExcelApp.Range[ Range ].Select;
 
      ExcelApp.Selection.Font.Size := 11;
      ExcelApp.Selection.borders.LineStyle := 7;
      ExcelApp.Selection.RowHeight := 1/0.035;
      ExcelApp.Selection.Rows[3].RowHeight := 1.5/0.035; // 1厘米
      ExcelApp.Selection.Rows[3].WrapText := True;
 
      if AutoFit then
      begin
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ StringReplace(Range,'A1','A3',[]) ].Select;
 
        ExcelApp.Selection.WrapText := True;
        ExcelApp.Selection.Rows.AutoFit; //设为自动列宽,
        //FXLSRange:='A1:H'+IntToStr(cdsSTAT.RecordCount+2);
 
        if Range = '' then
          ExcelApp.Cells.Select
        else
          ExcelApp.Range[ Range ].Select;
      end;
 
      ExcelApp.ActiveSheet.PrintOut;
    except
    end;
  finally
    ExcelApp:=Null;
  end;
 
end;
 
end.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多