Delphi导出Excel的设置操作 delphi打开excel文件

   2023-02-07 学习力0
核心提示:procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); {新建Excel工作簿、进行页面设置} begin {新建Excel工作簿} if ExlApp.WorkBooks.Count1 then begin ExlBook := ExlApp.Workbooks.Add; //ExlBook:=ExlApp.WorkBooks[1].WorkSheets[1];
procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); 
{新建Excel工作簿、进行页面设置} 
begin 
  {新建Excel工作簿} 
  if ExlApp.WorkBooks.Count < 1 then 
  begin 
    ExlBook := ExlApp.Workbooks.Add; //ExlBook:=ExlApp.WorkBooks[1].WorkSheets[1]; 
    ExlApp.ActiveSheet.Name:=SheetName; 
  end; {if} 

  {进行页面设置} 

  以上是怎么不用空间而调用EXCEL的, 很方便的! 
  //设置页面 
  if PageSize=1 then 
    ExlApp.ActiveSheet.PageSetup.PaperSize := xlPaperA3; //纸张大小: A3 
  if PageSize=2 then 
    ExlApp.ActiveSheet.PageSetup.PaperSize := xlPaperA4; //纸张大小: A4 
  if PageSize=3 then 
    ExlApp.ActiveSheet.PageSetup.PaperSize := xlPaperB5; //纸张大小: B5 
  if PageLay=1 then 
    ExlApp.ActiveSheet.PageSetup.Orientation := xlportrait; //页面放置方向:纵向 
  if PageLay=2 then 
    ExlApp.ActiveSheet.PageSetup.Orientation := xlLandscape; //页面放置方向:横向 

  //设置页宽自动适应 
  ExlApp.ActiveSheet.PageSetup.Zoom := False; 
  ExlApp.ActiveSheet.PageSetup.FitToPagesWide := 1; 
  ExlApp.ActiveSheet.PageSetup.FitToPagesTall := False; 

  //设置页眉、页脚(即:页标题、页号) 
  ExlApp.ActiveSheet.PageSetup.RightFooter := '打印时间: ' + '&D &T'; 
  ExlApp.ActiveSheet.PageSetup.CenterFooter := '第&''&P&''页,共&''&N&''页';

  //设置页边距: 
  ExlApp.ActiveSheet.PageSetup.TopMargin := 1.5/0.035; 
  ExlApp.ActiveSheet.PageSetup.BottomMargin := 1.5/0.035; 
  ExlApp.ActiveSheet.PageSetup.LeftMargin := 1/0.035; 
  ExlApp.ActiveSheet.PageSetup.RightMargin := 1/0.035; 
  ExlApp.ActiveSheet.PageSetup.HeaderMargin:= 0.5/0.035; 
  ExlApp.ActiveSheet.PageSetup.FooterMargin := 0.5/0.035; 

  //设置页面对齐方式 
  ExlApp.ActiveSheet.PageSetup.CenterHorizontally := True; //页面水平居中 
  //ExlApp.ActiveSheet.PageSetup.CenterVertically := True; //页面垂直居中 

  //设置整体字体格式 
  ExlApp.Cells.Font.Name := '宋体'; //字体 
  ExlApp.Cells.Font.Size := 12; //字号 
  ExlApp.Cells.RowHeight := 16; //行高 
  ExlApp.Cells.VerticalAlignment := 2; //垂直方向对齐方式:居中 
end; {CreatRepSheet} 

procedure SetAddMess(H_Mess1,H_Mess2,H_Mess3,F_Mess1,F_Mess2,F_Mess3:String);

//用户自定义页眉、页脚(即:页标题、页号)

begin

  ExlApp.ActiveSheet.PageSetup.LeftHeader:=H_Mess1;

  ExlApp.ActiveSheet.PageSetup.CenterHeader:=H_Mess2;

  ExlApp.ActiveSheet.PageSetup.RightHeader:=H_Mess3;

end;{SetAddMess}

procedure SetRepBody(x,ch:Integer;cw:Double;cf:String);

//设置整体各列数据格式

begin

  ExlApp.ActiveSheet.Columns[x].ColumnWidth:=cw; //列宽

  ExlApp.ActiveSheet.Columns[x].NumberFormat:=Cf; //单元格数据格式

  ExlApp.ActiveSheet.Columns[x].HorizontalAlignment:=ch; //水平方向对齐方式

end;{SetRepBody}

procedure CreatTitle(TitleName:String;y:Integer);

{设置标题}

Var

  RepSpace:String;

begin

  CellMerge(1,1,1,y);

  ExlApp.cells(1,1):=TitleName;

  RepSpace:='A1'+':'+GetRepRange(1,y);

  ExlApp.Range[RepSpace].Select;

  ExlApp.Selection.NumberFormat :='G/通用格式';

  ExlApp.Selection.Font.Size:=22;

  ExlApp.Selection.Font.Name:='黑体';

  ExlApp.Selection.Font.Bold:=True;

  ExlApp.Selection.HorizontalAlignment:=3; //水平方向对齐方式:居中

  ExlApp.Rows[1].RowHeight:=28;

end;{RepHead}

procedure CreatSubHead(SubTitle: Array of String);

{设置常规子表头}

Var

  i,j:Integer;

begin

  j:=0;

  for i:=Low(SubTitle) to High(SubTitle) do

  begin

    Inc(j);

    ExlApp.cells(2,j):=SubTitle[i];

  end;

end;{CreatRepHead}

procedure SubHeadFormat(y,r:Integer);

{设置子表头格式}

Var

  RepSpace:String;

  n:Integer;

begin

  RepSpace:='A2'+':'+GetRepRange(1+r,y);

  ExlApp.Range[RepSpace].Select;

  ExlApp.Selection.NumberFormat :='G/通用格式';

  ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中

  ExlApp.Selection.Font.Bold:=True;

  for n:=1 to r do

  begin

    ExlApp.Rows[1+n].RowHeight:=18;

    SetRepLine(1+n,y);

  end;{for}

end;{SubHeadFormat}

procedure DTSubHeadGS(x,y,r:Integer);

{设置动态子表头格式}

Var

  RepSpace:String;

  n:Integer;

begin

  RepSpace:=GetRepRange(x,1)+':'+GetRepRange(x+r-1,y);

  ExlApp.Range[RepSpace].Select;

  ExlApp.Selection.NumberFormat :='G/通用格式';

  ExlApp.Selection.HorizontalAlignment:=3; //表头水平对齐方式:居中

  ExlApp.Selection.Font.Bold:=True;

  for n:=0 to r-1 do

  begin

    ExlApp.Rows[x+n].RowHeight:=18;

    SetRepLine(x+n,y);

  end;{for}

end;{DTSubHeadGS}

procedure WriteData(RepData: String; x,y,flag:Integer); //写入数据

{写数据}

begin

  if flag=1 then

    ExlApp.cells(x,y):=StrToDate(RepData)

  else

   ExlApp.cells(x,y):=RepData;

end;{WriteDate}

procedure RepPageBreak(x,y,r:Integer); //X:分页处行数,Y:列数,R:子表头总共的行数

//分页、复制表头

Var

  RepSpace:String;

  n:Integer;

begin

  ExlApp.ActiveSheet.Rows[x].PageBreak := 1;

  RepSpace:='A1'+':'+GetRepRange(r+1,y);

  ExlApp.ActiveSheet.Range[RepSpace].Copy;

  RepSpace:='A'+IntToStr(x);

  ExlApp.ActiveSheet.Range[RepSpace].PasteSpecial;

  ExlApp.Rows[x].RowHeight:=28;

  for n:=2 to r do

    ExlApp.Rows[x+n].RowHeight:=18;

end;{RepPageBreak}

procedure RepSaveAs(FileName:String);

{保存为*.xls文件}

begin

  try

    ExlBook.saveas(FileName);

  except

    MessageDlg('不能访问文件,请关闭Microsoft Excel后再运行本程序!', mtError, [mbOk], 0);

  end;

end;{RepSaveAs}

procedure RepPrivew(FileName:String);

{预览}

begin

  RepCreat;

  ExlApp.Visible :=True;

  try

    ExlApp.workBooks.Open(FileName);

    ExlApp.Workbooks[1].WorkSheets[1].PrintPreview;

  finally

    ExlApp.Quit;

    ExlApp:=Unassigned;

    //ExlApp:='';

  end;{try}

end;{RepPrivew}

procedure RepQuit;

{退出Excel}

begin

  ExlBook.Close;

  ExlApp.Quit; //退出Excel Application

  ExlApp:=Unassigned; //释放VARIANT变量

end;{RepQuit}

procedure RepDestroy;

{非正常退出Excel}

begin

  if Not VarIsEmpty(ExlApp) then

  RepQuit;

end;{RepDestroy}

end.

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Delphi中的消息处理机制 delphi 方法
    每一个VCL都有一内在的消息处理机制,其基本点就是构件类接收到某些消息并把它们发送给适当的处理方法,如果没有特定的处理方法,则调用缺省的消息处理句柄。    其中mainwndproc是定义在Twincontrol类中的一个静态方法,不能被重载(Override)。它不直接处
    02-09
  • Delphi XE6 通过JavaScript API调用百度地图
    Delphi XE6 通过JavaScript API调用百度地图
    参考昨天的内容,有朋友还是问如何调用百度地图,也是,谁让咱都在国内呢,没办法,你懂的。 首先去申请个Key,然后看一下百度JavaScript的第一个例子:http://developer.baidu.com/map/jsdemo.htm下一步,就是把例子中的代码,移动TWebBrower中。 unit Unit
    02-09
  • Delphi编译/链接过程 delphi编程案例
    Delphi编译/链接过程 delphi编程案例
    下面展示了Delphi是怎样编译源文件,并且把它们链接起来,最终形成可执行文件。当Delphi编译项目(Project)时,将编译项目源文件、窗体单元和其他相关单元,在这个过程中将会发生好几件事情:首先,Object Pascal编译器把项目单元编译为二进制对象文件,然后
    02-09
  • Delphi CompilerVersion Constant / Compiler C
    http://delphi.wikia.com/wiki/CompilerVersion_Constant The CompilerVersion constant identifies the internal version number of the Delphi compiler.It is defined in the System unit and may be referenced either in code just as any other consta
    02-09
  • Delphi revelations #1 – kbmMW Smart client
    Delphi 启示 #1 – kbmMW Smart client on NextGen (Android) – 作用域问题以更高级的方式使用kbmMW smart client,在Android设备上,我遇到了问题。通过继承TInvokeableVariantType,kbmMW smart client可以使用Delphi支持的特殊类型的自定义Variant,从而可
    02-09
  • Delphi 调用DLL外部函数时的指针参数
    某项目需要调用设备厂家提供的DLL的函数,厂家给了一个VB的例子,有个参数是ByRef pBuffer As Single。于是在Delphi中用buffer:array of single代替:function func(buffer:array of single;count:integer):integer;far;stdcall;external 'func.dll';调用后bu
    02-09
  • 《zw版·Halcon-delphi系列原创教程》 Halcon分
    《zw版·Halcon-delphi系列原创教程》 Halcon分类函数012,polygon,多边形为方便阅读,在不影响说明的前提下,笔者对函数进行了简化::: 用符号“**”,替换:“procedure”:: 用大写字母“X”,替换:“IHUntypedObjectX”:: 省略了字符:“const”、“OleVa
    02-09
  • 最简单的delphi启动画面(转)
    首先做一窗体,然后将BorderStyle的属性设为bsnone,放image控件,align设为alclient 然后将主程序的修改为 uses Windows, Forms, Unit1 in 'Unit1.pas' {Form1}, Unit2 in 'Unit2.pas' {Form2}; {$ R *.res} begin Application.Initialize; Form2:=TForm2.Cre
    02-09
  • Delphi备忘三:TCollection的使用,用Stream保
     代码unit ufrmGetFunctionDefine;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,TypInfo,  Dialogs,ufrmStockBaseCalc, StdCtrls, ComCtrls,uQEFuncManager,uWnDataSet,uDataService;type  T
    02-09
  • Delphi Dcp 和BPL的解释
    dcp = delphi compiled package,是 package 编译时跟 bpl 一起产生出来的,记录着 package 中公开的 class、procedure、function、variable、const.... 等等的名称和相对位址。package英文翻译过来就是“包”。如果 某个控件包 A 引用了 控件包 B,当 控件包
    02-09
点击排行