• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

delphi 导出 excel

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
procedure Tfrm_main.XLGW_Excel(p_int_Row_Begin, p_int_Col_Begin,
  p_int_Row_End, p_int_Col_End: Integer; p_str_Value, p_str: String);
var
  m_Query : TADOQuery;
  m_StringList_FieldNames : TStringList;
  m_int,rowid : Integer;
  m_int_Row : Integer;
  ExcelApp,ExcelObj:TExcel;  //引用excel单元
  m_str,Astr : String;
  HJXLCD:string;
  i:Integer;  //用于控制列宽变量
begin
  m_Query := TADOQuery.Create(nil);
    ExcelObj:=TExcel.Create;
    try
      {填写表头}
      m_Query.Connection:= sqlclass.conn;
      m_str := p_str;
      m_Query.Close;
      m_Query.SQL.Clear;
      m_Query.SQL.Add(m_str);
      try
        m_Query.Open;
      except
        ShowMessage('报表输出失败,请重新计算!');
        Exit;
      end;
      if m_Query.IsEmpty then
      begin
        ShowMessage('没有要导出的数据!');
        Exit;
      end;
      if not ExcelObj.New_WorkBook then    //显示工作薄
      begin
        ShowMessage('新建工作簿失败');
        Exit;
      end;
      try
      //页面设置                       //纵向xlPortrait       //横向xlLandscape
      ExcelObj.SetSheetPage(xlPaperA4,xlLandscape,0.6,0.6,0.6,0.6,0.3,0.3);
      except
      end;
      ExcelObj.IsVisible(True);  //控制是否显示Excel
      m_StringList_FieldNames := TStringList.Create;
      m_Query.GetFieldNames(m_StringList_FieldNames);
      ExcelObj .SetCell('序号',2,1,0);
      for m_int := 1 to m_StringList_FieldNames.Count do
      begin
        //设置指定单元格的值
        ExcelObj.SetCell(m_StringList_FieldNames.Strings[m_int - 1],2, m_int+1,0);
      end;

    {写入数据}
      rowid :=0;
      m_int_Row := 3;  //行
      while not  m_Query.Eof do
      begin
        rowid:=rowid+1;
        ExcelObj.SetCell(IntToStr(rowid),m_int_Row, 1,1); //加序号
        for m_int := 1 to m_StringList_FieldNames.Count do
        begin
          //判断输出字段类型
          //  (0:字符型;1:整形;2,3:数值型)  增加清零操作
          Astr:= m_Query.FieldByName(m_StringList_FieldNames.Strings[m_int - 1]).AsString;
         { if m_Query.Fields[m_int - 1].DataType in [ftSmallint, ftInteger,ftBCD ] then
          begin
            if (Astr = '') or (StrtoInt(Astr)=0) then
            begin
              Continue;
            end;
           //ExcelObj.SetCell(Astr,m_int_Row, m_int+1,1);    //weixing 20100707
          end
          else }
            if  m_Query.Fields[m_int - 1].DataType in [ftWord,ftFloat] then
            begin
                if (Astr = '') or (StrtoFloat(Astr)=0) then
                begin
                  Continue;
                end;
               ExcelObj.SetCell(Astr,m_int_Row, m_int+1,2);
            end
            else if m_Query.Fields[m_int - 1].DataType in [ftInteger] then  //weixing 20100707
            begin
                if (Astr = '') or (StrtoFloat(Astr)=0) then
                begin
                  Continue;
                end;
                ExcelObj.SetCell(Astr,m_int_Row, m_int+1,1);
            end
            else
            begin
              ExcelObj.SetCell(Astr,m_int_Row, m_int+1,0);
            end;//if
         end; //if
       // end;//for
        m_int_Row := m_int_Row + 1;
        m_Query.Next;
      end;//While
        for i:=1 to 13 do
        begin
          if (i=1) then
            ExcelObj.SetColumnWidth(i,24)
           else
            ExcelObj.SetColumnWidth(i,7);
         end;
      for m_int:=1 to m_int_Row-1 do
      begin
        ExcelObj.SetRowHeight(m_int,18); //设置指定行的行高
        //单元格居中
        ExcelObj.SetRowHorizontalAlignment(m_int,xlHAlignCenter);
        ExcelObj.SetRowVerticalAlignment(m_int,xlHAlignCenter);
      end;
      p_int_Col_End:=m_StringList_FieldNames.Count;
      {设置表格函数}
      ExcelObj.MergeCells(p_int_Row_Begin, p_int_Col_Begin,p_int_Row_End, p_int_Col_End);//合并单元格
      //设置标题指定范围的值
      ExcelObj.SetRangeValue(1,1,p_int_Row_Begin, p_int_Col_Begin,p_str_Value);
      //生成标题参数设置
      ExcelObj.SetRowFontStyle(p_int_Row_Begin,'加粗','宋体',0,15);
      //标题行高
      ExcelObj.SetRowHeight(1,33); //设置指定行的行高

      //加单元格
      ExcelObj.SetRangeOutBorderStyle(2,1,m_int_Row-1, m_StringList_FieldNames.Count+1,2,0,1);//单元格外部加边线
      ExcelObj.SetRangeInnerLineStyle(2,1,m_int_Row-1, m_StringList_FieldNames.Count+1,2,0,1);//单元格内部加边线
      ExcelObj.SetRangeAutoWrap(3,1,3, m_StringList_FieldNames.Count);//指定行自动换行
      //ExcelObj.SetRowFontStyle(3,'加粗','宋体',0,12);//生成排头参数设置
      //最后一行合计所有数值字段
      //ExcelObj.SetRangeValue(m_int_Row,1,m_int_Row,1,'合计'); //往单元格写入'合计';
     { for m_int := 1 to m_StringList_FieldNames.Count do
        //行列号为: m_int_Row,m_int ;
      begin
        if m_Query.Fields[m_int - 1].DataType in [ftSmallint, ftInteger, ftWord,ftFloat,ftBCD] then
        begin
          HJXLCD:=FloatToStr(ExcelObj.Sum(4,m_int,m_int_Row,m_int)); //合计线路长度
          ExcelObj.SetRangeValue(m_int_Row,m_int,m_int_Row,m_int,HJXLCD);
        end;
      end; }
      ExcelObj.CellAutoFit;//自动列宽
      m_StringList_FieldNames.free;
   finally
      m_Query.Free;
      ExcelObj.Destroy;
   end;
end;

 

 

 

 

{利用OLE技术实现的与Excel应用程序的接口
 可以实现表格设计,数据输出,模板定制,
 打印设置,打印预览,打印表格等功能;}

unit Excel_Unit;{单元名称}

interface{接口部分}
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Excel97,OleServer, StdCtrls,Variants,ExcelConst_Unit,ComObj;
const

  //报表平面图参数
  pm_Title = '配 电 线 路 条 图';

  pm_PageRows = 21;

  pm_PageOffset = 530;//     //页与页之间图形距离

  pm_LineLeft   = 123;       //线起始left
  pm_LineTop    = 72;//74;   //线起始top
  pm_LineRight  = 727;//705; //线终止right //水平所以没有y坐标

  pm_GanFirstLeft = 118;     //最左第一个杆的left坐标
  pm_GanOffset    = 60;      //杆间距
  pm_GanTop  = 166;//67;     //杆距顶top
  pm_GanD    = 10;           //杆径  12
  pm_BYQD    = 13;//14;      //变压器大小  13
  pm_BYQY    = -7;           //变压器距杆距离  20

  pm_Distance = 27;//30;     //转角箭头线段长

  dm_GanTop = 143.25;        //断面杆
  dm_HaiBa = 0;              //海拔
  dm_HuChui = 5;             //导线下垂弧度

//下面记录类型用于绘制平断面图  
type
  TRecData = record
    XLMC: String;   //线路名称
    GH: String;     //杆号
    GX: String;     //电杆种类及杆型
    GG: String;     //杆高
    MS: String;     //埋深
    DXPL: String;   //导线排列
    DXXH: String;   //导线型号
    DJ: String;     //档距
    LJCD: String;   //累计长度
    ZJBZ: Integer;  //画转角标志   1: 画转角 0: 不画转角
    ZJ: String;     //线路转角
    JCKY: String;   //交叉跨越
    CZJL: String;   //垂直距离
    HDGG: String;   //横担规格
    LXWZ: String;   //拉线位置
    LXXHGS: String; //拉线型号/根数
    JYZXHSL: String;//绝缘子型号/数量
    GZZSY: String;  //故障指示仪
    BTXS: String;   //变台型式
    PBRL: String;   //配变容量
    HM: String;     //户名
    BLQGG: String;  //避雷器规格
    BLQSL: String;  //避雷器数量
    KGGG: String;   //开关规格
    KGSL: String;   //开关数量
    FZ1ZJ: String;  //分支1转角
    FZ2ZJ: String;  //分支2转角
    FZ1MC: String;  //分支1名称
    FZ2MC: String;  //分支2名称
    IsCable: Integer; //是否有电缆 1: 电缆 0: 不是电缆
    IsTJPole: Integer;  //是否是引出分支杆
  end;
  PRecData = ^TRecData;

type
  TWindowObject = record
                    WinHandle  : HWnd;    {Window Handle}
                    WinCaption : String;  {Window Caption Text (If any)}
                    ProcessID  : Integer; {Process the window belongs to}
                    IsVisible  : Boolean; {Is the window visible?}
                    IsEnabled  : Boolean; {Is the window enabled for mouse/keyboard input?}
                    IsIconic   : Boolean; {Is the window minimized?}
                    WindowRect : TRect;   {Window Dimensions}
                    {Add more properties here if you like,
                     then fill them in at the WindowCallback
                     function.}
                  end;
  PTWindowObject = ^TWindowObject;

//Excel操作类
  TExcel = class(TObject)
  private
    MyExcel: Variant;             //存储Excel对象
    Book :Variant;               //当前工作表
    Sheet:Variant;               //当前工作表
  protected
    { protected declarations }
    function GetRowNum(TStr:string):Integer;   //获取指定单元格的行号()
    function GetColNum(TStr:string):Integer; //获取指定单元格的行号
  public
    { Public declarations }
    IsPageSetup:Boolean;         //是否进行了页面设置
    Template_Path:string;        //模板路径
    Template_Name:string;        //模板名称
    Template_OpenName:String;    //打开的模板的名称
    ActiveSheet:Integer;      //当前活动的页
    UsedRowBeg:Integer;       //当前已使用的起始行
    UsedColBeg:Integer;       //当前已使用的起始列
    UsedRowEnd:Integer;       //当前已使用的终止行
    UsedColEnd:Integer;       //当前已使用的终止列
    CurRowBeg:Integer;        //要使用的起始行
    CurColBeg:Integer;        //要使用的起始列
    CurRowEnd:Integer;        //要使用的终止行
    CurColEnd:Integer;        //要使用的终止列
    //下面为类操作
    Constructor Create();                                        //创建Excel对象
    Destructor Destroy; virtual;                                 //释放Excel对象
    //Excel操作
    procedure IsVisible(IsOrNo:Boolean);                         //控制Excel是否显示
    procedure IsAlert(IsOrNo:Boolean);                           //控制Excel是否出现警告存盘的对话框
    function  SetActiveWindowGridVisible(IsVisible:Boolean=True):Boolean;        //设置当前窗口网格的显示与否
    function  SaveToFile(AFileName:string):Boolean;              //Excel数据保存到文件
    procedure ExitExcel;                                         //退出Excel

    //工作簿操作
    function  New_WorkBook:Boolean;                                 //新建工作簿
    function  OpenExcelFile(FileName:string): Boolean;              //打开指定的Excel文件
    function  IsWorkbookExit:Integer;                               //判断是否存在工作簿
    //工作表操作
    function SetCurrentSheet(SheetNum:Integer):Boolean;             //设置当前工作表
    function GetSheetsNameList(var FileName:TstringList):Boolean;   //得到工作簿中工作表名称列表
    function GetSheetRange:string;                                  //获取当前工作表中的工作范围
    function SetSheetPage(APageType,Orientation:Integer;LeftMargin,RightMargin,TopMargin,
             BottomMargin,HeaderMargin,FooterMargin:Double):Boolean;   //对指定的工作表进行页面设置
    //Range或Cell操作
    function  GetCell(Row,Col:Integer):Variant;                        //得到当前工作表中某行某列的值
    procedure SetCell(Value: String; Row, Col: Integer;AType:Integer); //设置指定单元格的值
    function  GetRangeValue(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer):String;                    //获取指定范围的值
    function  SetRangeValue(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;AValue:string):Boolean;     //设置指定范围的值
    function  InsertPicIntoSheet(ABeginRow,ABeginCol:Integer;APicFile:String):Boolean;//向指定工作表中插入图片
    function  Sum(BeginRow,BeginCol,EndRow,EndCol:Integer):Double;                    //对指定单元格间的值求和
    //文本框操作
    function  InsertTextBox(AX,AY,AWidth,AHeight:Double;AOrient:Integer=msoTextOrientationHorizontalRotatedFarEast):Variant;//在指定位置插入文本框
    function  SetTextBoxFillStyle(ATextBox:Variant;AVisible:Integer;ATransparency:Double=0):Boolean;  //设置文本框填充样式
    function  SetTextBoxLineStyle(ATextBox:Variant;AVisible:Integer;ATransparency:Double=0):Boolean;  //设置文本框边框样式
    function  SetTextBoxFontStyle(ATextBox:Variant;AFontName,AFontStyle:String;
              AFontSize:Integer = 12;AFontColor:Integer=0):Boolean;                     //设置文本框中字体样式
    function  SetTextBoxAlignment(ATextBox:Variant;HAlign:Integer=xlLeft;VAlign:Integer=xlBottom):Boolean; //设置文本框水平和垂直对齐方式
    function  SetTextBoxText(ATextBox:Variant;AText:string):Boolean;                    //设置文本框中文字内容
    //格式化单元格操作
    function  MergeCells(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer):Variant;  //合并单元格
    procedure CellAutoFit;                                                              //设置单元格为自动列宽
    //格式化行高和列宽
    function  SetColumnWidth(AColNum:Integer;AColWidth:Double):Boolean;                 //设置指定列的宽度
    function  SetRowHeight(ARowNum:Integer;ARowHeight:Double):Boolean;                  //设置指定行的行高
    function  SetRangeColWidth(ABeginRow,ABeginCol,AEndRow,AEndCol:Integer;AColWidth:Double):Boolean; //设置指定范围内单元格的列宽
    //格式化指定区域范围
    function  SetRangeFontStyle(BeginRow,BeginCol,EndRow, EndCol:Integer;
                                AFontStyle:string='常规';AFontName:string='宋体';
                                AFontColor:Integer=0;AFontSize:Integer=12):Boolean;               //设置范围
    function  SetRowFontStyle(ARowNum:Integer;AFontStyle:string='常规';AFontName:string='宋体';
                                AFontColor:Integer=0;AFontSize:Integer=12):Boolean;               //设置指定行的字体样式
    function  SetColumnFontStyle(AColNum:Integer;AFontStyle:string='常规';AFontName:string='宋体';
                                 AFontColor:Integer=0;AFontSize:Integer=12):Boolean;              //设置指定列的字体样式

    function  SetRangeOutBorderStyle(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;
                                 AWeight:Integer=2;ALineColor:Integer=0;ALineStyle:Integer=xlContinuous):Boolean; //设置指定范围的外边框样式

    function SetRowOutBorderStyle(ARowNum:Integer;AWeight:Integer=2;
                                 ALineColor:Integer=0;ALineStyle:Integer=xlContinuous):Boolean;        //设置指定行的外边框样式
    function SetColumnOutBorderStyle(AColNum:Integer;AWeight:Integer=2;ALineColor:Integer=0;           //设置指定列的外边框样式
                                ALineStyle:Integer=xlContinuous):Boolean;

    function  SetRangeInnerLineStyle(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;
                   AWeight:Integer=2;ALineColor:Integer=0;ALineStyle:Integer=xlContinuous):Boolean;   //设置指定范围的分隔线样式

    function  SetRowInnerLineStyle(ARowNum:Integer;AWeight:Integer=2;ALineColor:Integer=0;
                                ALineStyle:Integer=xlContinuous):Boolean;                            //设置指定行的分隔线样式
    function  SetColumnInnerLineStyle(AColNum:Integer;AWeight:Integer=2;ALineColor:Integer=0;
                                ALineStyle:Integer=xlContinuous):Boolean;                            //设置指定列的分隔线样式

    function  SetRangeHorizontalAlignment(ABegRowNum,ABegColNum,AEndRowNum,
                               AEndColNum:Integer;AAlign:Integer=xlLeft):Boolean;               //设置范围的水平对其方式
    function  SetRangeVerticalAlignment(ABegRowNum,ABegColNum,AEndRowNum,
                               AEndColNum:Integer;AAlign:Integer=xlBottom):Boolean;             //设置范围的垂直对其方式
    function  SetRowHorizontalAlignment(ARowNum:Integer;AAlign:Integer=xlLeft):Boolean;         //设置指定行的水平对其方式
    function  SetRowVerticalAlignment(ARowNum:Integer;AAlign:Integer=xlBottom):Boolean;         //设置指定行的垂直平对其方式
    function  SetColumnHorizontalAlignment(AColNum:Integer;AAlign:Integer=xlLeft):Boolean;      //设置指定列的水平对其方式
    function  SetColumnVerticalAlignment(AColNum:Integer;AAlign:Integer=xlBottom):Boolean;      //设置指定列的垂直对其方式

    function  SetRangeAutoWrap(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer):Boolean;    //设置指定区域格式为自动换行
    function  CancelRangeAutoWrap(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer):Boolean; //取消指定区域的自动换行
    function  SetRangeBackGround(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;
               APattern:Integer=xlSolid;APatternColor:Integer=xlNone;ABackColor:Integer=xlNone):Boolean;   //设置指定范围的背景图案

    //格式化箭头线
    function FormatArrowLineEndHead(ALine:Variant;AheadStyle:Integer=msoArrowheadTriangle;
             AheadLength:Integer=msoArrowheadLengthMedium;AheadWidth:Integer=msoArrowheadWidthMedium):Boolean;  //格式化箭头线的末端样式
    function FormatArrowLineBeginHead(ALine:Variant;AheadStyle:Integer=msoArrowheadTriangle;
             AheadLength:Integer=msoArrowheadLengthMedium;AheadWidth:Integer=msoArrowheadWidthMedium):Boolean; //格式化箭头线的首端样式
    function  FormatArrowLine(ALine:Variant;AWidth:Double=1.5;AStyle:Integer=msoLineSingle;ADashStyle:Integer=msoLineSolid):Boolean;  //格式化箭头线的线样式
    //数据库中数据转为Excel操作
    procedure SetTheFieldsName(AFields:TStringList;BeginRow,BeginCol,FieldNumber:Integer;IsNeedXuHao:Boolean); //设置Excel工作表中的列名
    procedure TransferDataToExcel(FieldValues:Array of TStringList;BeginRow,BeginCol,FieldNumber,RcdNumber:Integer;IsNeedXuHao:Boolean); //转换数据到Excel
    procedure DataToExcelUpPage(FieldValues:Array of TStringList;BeginRow,BeginCol,FieldNumber,
              RcdNumber:Integer;IsNeedXuHao:Boolean;IsHengXiangOrZongXiang:Boolean=False); //使用Excel将指定的数据打印输出
    //
    procedure Print_PageSetup();                            //打印页面设置
    procedure Print_PrintView;                              //打印预览
    procedure Print_PrintOut;                               //打印输出
    //
    function  Get_Template_Path():String;                  //获取模板路径
    procedure Set_Template_Path(PathName:String);          //设置模板路径
    procedure Set_Template_OpenName(TemplateName:String);  //设置打开模板的名称
    function  Open_Template():Variant;  //打开模板
    procedure Save_Template();  //保存模板
    //
    procedure SetTableTitle(TitleName:String;BeginRow,BeginCol,Row,Col,FontSize:integer;
                            IsNeedXuHao:Boolean=False;IsNeedFrame:Boolean=True);  //设置表的标题(表头)
    //绘制平断面图
    procedure CreatePage(APage: Integer;ABDZMC, AXLMC: String;AValueList: TStringList); //创建页
    procedure FormatCells(APage, AColNum, ARow: Integer;AColName: String
                                          ;AValueList: TStringList);             //绘制平断面图时根据不同情况格式化单元格
    procedure GetCellDMGanCoord(APage, ACellIndex: Integer;var AX, AY: Double);  //获取指定页,指定cell的断面杆的中心坐标
    procedure CreateDMT(AX1, AY1, AGanGao: Double;AIsTJPole, AIsFirstPole,
                 AIsCable: Boolean;var AOldY2: Double);                         //创建断面图
    procedure GetCellGanCoord(APage, ACellIndex: Integer;var AX, AY: Double);   //获取指定页,指定cell的杆的中心坐标
    procedure CreateFZLine(AX, AY,AYOffSet,AAngle: Double;AXLMC: String);       //画分支  创建转角箭头线
    procedure CreateArrowheadLine(AX, AY, AAngle: Double);                      //创建转角箭头线
    function  DoubleToFenMiao(Jiao:Double):string;                              //双精度的度转换为度分秒
    procedure DrawBYQText(AX, AY : Double;AText: String);                       //绘制变压器文本
    procedure DrawBYQ(AX, AY : Double);                                         //绘制变压器
    function GetPoleHight(AHight: Double): Double;                              //以15米杆高为标准计算d当前杆塔图形高度
    //暂不使用的功能
    function  FindTheExcelMainWindow:Integer;          //辅助工程找出当前系统环境中的运行的excel的主窗体句柄:
    procedure DisableSystemCloseBox;                   //使当前Excel应用程序的关闭按钮失效

    //
  Published
    { Published declarations }
  end;

const
  RcdCountOfEachPageOfHengXiang=22;
  RcdCountOfEachPageOfZongXiang=44;

var
  MyWindowPtr : ^TWindowObject;
  V_PageOffset: Double;

implementation {实现部分}



{ Excel }


{
功能描述:创建Excel对象
输入参数: 无
输出参数:无
创建时间: 2006-04-07
创建人:  赵占轻
}
constructor TExcel.Create();
begin
  Inherited;
  try
    MyExcel:=CreateOleObject('Excel.Application');             //创建Excel对象
  except
  end;
  //
//  New(MyWindowPtr);
  //
end;

{
功能描述:使用Excel将指定的数据打印输出
输入参数:FieldValues              待输出的数据列表
          BeginRow                 起始行号
          BeginCol                 起始列号
          FieldNumber              字段数
          RcdNumber                记录数
          IsNeedXuHao              是否需要序号
          IsHengXiangOrZongXiang   纵向还是横向 (True:纵向False:横向)
输出参数:无
创建时间:2006-04-13
创建人:  赵占轻
}
procedure TExcel.DataToExcelUpPage(FieldValues: array of TStringList; BeginRow,
  BeginCol, FieldNumber, RcdNumber: Integer; IsNeedXuHao: Boolean;IsHengXiangOrZongXiang:Boolean=False);
var
  nPageNumber,nLastPageRcdCount,i,nTmp:Integer;
  vSheet:Variant;
begin
  if IsHengXiangOrZongXiang then
  begin
    nLastPageRcdCount:=(RcdNumber mod RcdCountOfEachPageOfZongXiang);
    nPageNumber:=((RcdNumber-nLastPageRcdCount) div RcdCountOfEachPageOfZongXiang);
    ntmp:=RcdCountOfEachPageOfZongXiang;
  end else
  begin
    nLastPageRcdCount:=(RcdNumber mod RcdCountOfEachPageOfHengXiang);
    nPageNumber:=((RcdNumber-nLastPageRcdCount) div RcdCountOfEachPageOfHengXiang);
    nTmp:=RcdCountOfEachPageOfHengXiang;
  end;//
  //循环执行打印工作:
  for i:=1 to nPageNumber do
  begin
    try
      vSheet:=Open_Template;                 //打开模板
    except
      MyExcel.Quit;
      Exit;
    end;//
    TransferDataToExcel(FieldValues,BeginRow,BeginCol,FieldNumber,ntmp,IsNeedXuHao);    //数据输出到Excel
    try
      Print_PrintOut;            //从Excel中打印输出
    except
      ShowMessage('打印机错误,请检查你的打印机设备是否正常!');
      Exit;
    end;//
  end;//
  //执行剩余打印工作:
  if nLastPageRcdCount>0 then
  begin
    try
      vSheet:=Open_Template;
    except
      MyExcel.Quit;
      Exit;
    end;//
    TransferDataToExcel(FieldValues,BeginRow,BeginCol,FieldNumber,nLastPageRcdCount,IsNeedXuHao);
    try
      Print_PrintOut;
    except
      ShowMessage('打印机错误,请检查你的打印机设备是否正常!');
      Exit;
    end;//
  end;//
end;

{
功能描述:释放Excel对象变量
输入参数:无
输出参数: 无
创建时间:2006-04-07
创建人:  赵占轻
}
destructor TExcel.Destroy;
begin
  //
  inherited;
  MyExcel:=UnAssigned;
end;

{
功能描述:使Excel程序关闭按钮失效
输入参数:无
输出参数:无
创建时间:2006-04-18
创建人:  刘惠城
}
procedure TExcel.DisableSystemCloseBox;
var
  hExcelWindow : HWND;
  hSysMenu    : HMENU;
  nrlt:Integer;
begin
  // Find the Excel2000 handle
  nrlt:=FindTheExcelMainWindow;
  if nrlt=0 then
  begin
    Exit;
  end
  else
  begin
    hExcelWindow:=MyWindowPtr.WinHandle;
  end;//
  if hExcelWindow <> 0 then
  begin
    hSysMenu := GetSystemMenu (hExcelWindow, false);
    EnableMenuItem (hSysMenu, SC_CLOSE, MF_DISABLED)
  end;
end;
//-------------------------------------------------------------------------//

{
功能描述:退出Excel
输入参数:无
输出参数:无
创建时间:2003-11-09
创建人:  徐伟亮
}
procedure TExcel.ExitExcel;
begin
  try
    Book.Saved:=True;           //自动退出Excel时保存修改内容
  except
  end;
  MyExcel.Quit;
end;

{
功能描述:辅助工程找出当前系统环境中的运行的excel的主窗体句柄
输入参数:无
输出参数:1:找到结果;0:未找到结果
创建时间:2006-04-18
创建人:  刘惠城
}
function TExcel.FindTheExcelMainWindow:Integer;//will alter
  function WindowCallback(WHandle : HWnd; Var Parm : Pointer) : Boolean; stdcall;
  {This function is called once for each window}
  Var MyString : PChar;
      MyInt : Integer;
      caption1:PChar;
  begin
    caption1:='Microsoft Excel';
    MyString := Allocmem(255);
    GetWindowText(WHandle,MyString,255);
    if StrPos(MyString,caption1)=nil then
    begin
      //FreeMem(caption1,16);
      Result := True;//Not find,continue to enumerate windows;
      Exit;
    end;//
    //
    {Window Handle (Passed by the enumeration)}
    MyWindowPtr.WinHandle := WHandle;
    {Window text}
    MyWindowPtr.WinCaption := String(MyString);
    FreeMem(MyString,255);

    {Process ID}
    MyInt := 0;
    MyWindowPtr.ProcessID := GetWindowThreadProcessId(WHandle,@MyInt);

    {Visiblity}
    MyWindowPtr.IsVisible := IsWindowVisible(WHandle);

    {Enabled}
    MyWindowPtr.IsEnabled := IsWindowEnabled(WHandle);

    {Iconic}
    MyWindowPtr.IsIconic := IsIconic(WHandle);

    {Window Dimensions}
    MyWindowPtr.WindowRect := Rect(0,0,0,0);
    GetWindowRect(WHandle,MyWindowPtr.WindowRect);
    //
    Result := False; {Everything's okay.Stop to enumerate windows}
  end;
  //
begin
  //
  EnumWindows(@WindowCallback,0);
  if MyWindowPtr.WinHandle = 0 then
    begin
      Result:=0;
    end else
    begin
      Result:=1;
    end;
end;

{
功能描述:获取模板路径
输入参数:无
输出参数:无
创建时间:2006-04-11
创建人:   赵占轻
}
function TExcel.Get_Template_Path: String;
begin
  //
  Result:=Template_Path;
end;

{
功能描述:控制是否出现警告存盘的对话框;
输入参数:IsOrNo:控制是否显示提示框
             (True:显示提示框;False:不显示提示框)
输出参数:无
创建时间:2006-04-07
创建人:  赵占轻
}
procedure TExcel.IsAlert(IsOrNo: Boolean);
begin
  MyExcel.DisplayAlerts:=IsOrNo;                 //是否出现警告存盘的对话框;
end;

{
功能描述:控制是否显示Excel
输入参数:IsOrNo:
          (True:显示Excel;False:不显示Excel)
输出参数:无
创建日期:2003-10-20
创建人:  赵占轻
}
procedure TExcel.IsVisible(IsOrNo: Boolean);
begin
  if IsOrNo then
  begin
    MyExcel.Visible:= True;
  end else
  begin
    MyExcel.Visible :=False;
  end;
end;

{
功能描述:判断是否存在工作簿
输入参数:无
输出参数:0:不存在工作簿;1:存在工作簿
创建时间:2006-04-07
创建人:  赵占轻
}
function TExcel.IsWorkbookExit: Integer;
var
  nBookCount:Integer;
begin
  nBookCount:=MyExcel.Workbooks.Count;      //获取工作簿数量
  if nBookCount<=0 then
  begin
    Result:=0;
    Exit;
  end;//
  Result:=1;
end;

{
功能描述:新建工作簿
输入参数:无
输出参数:执行成功返回真否则返回假
创建时间:2006-04-14
创建人:  赵占轻
}
function TExcel.New_WorkBook:Boolean;
begin
  {在启动Excel之前,最好将以前运行的实例关闭
  因为有时会出现异常的情况;}
  //启动Excel应用程序:
  Result:=False;
  try
    if MyExcel.Workbooks.Count>=1 then             //如果Excel已经启动,
    begin                                      //且工作薄存在,则置
      Book:=MyExcel.Workbooks[1];                //第一个工作薄为当前
      Sheet:=MyExcel.Workbooks [1].Worksheets[1];//工作薄;
      Exit;
    end;//
    Book:=MyExcel.Workbooks.Add;
    if MyExcel.Worksheets.Count>=1 then
    begin
      Sheet:=Book.Worksheets.Item[1];
    end;
  except
    Exit;
  end;
  Result:=True;
end;

{
功能描述:打开模板
输入参数:无
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
function TExcel.Open_Template:Variant;
var
  sTmp:String;
begin
  //打开模板
  if MyExcel.Workbooks.Count>=1 then
  begin
    Book:=MyExcel.Workbooks[1];
    Book.Close;
  end;//
  sTmp:=Template_Path+Template_OpenName;
  Book:=MyExcel.Workbooks.Add (sTmp,0);
  Result:=MyExcel.Workbooks [1].Worksheets[1];
end;

{
功能描述:打印页面设置
输入参数:无
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
procedure TExcel.Print_PageSetup;
begin
  //打印设置
  IsPageSetup:=MyExcel.Dialogs.Item[xlDialogPageSetup].Show(
   EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam);
end;

{
功能描述:打印输出
输入参数:无
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
procedure TExcel.Print_PrintOut;
begin
  Sheet.PrintOut(EmptyParam);
end;

{
功能描述:打印预览
输入参数:无
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
procedure TExcel.Print_PrintView;
begin
  Sheet.PrintPreview;
end;

{
功能描述:保存模板
输入参数:无
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
procedure TExcel.Save_Template;
var
  sTmp:String;
begin
  //保存模板
  sTmp:=Template_Path+Template_Name;
  Book.SaveAs(sTmp,xlTemplate);
end;

{
功能描述:设置表的标题
输入参数:TitleName    标题名称
          BeginRow     开始行
          BeginCol     开始列
          Row          所占用的行数
          Col          列数
          FontSize     字体大小
          IsNeedXuHao  是否需要序号
          IsNeedFrame  是否需要框架
输出参数:无
创建时间:2003-10-20
创建人:  赵占轻
}
procedure TExcel.SetTableTitle(TitleName: String;
                               BeginRow,BeginCol,Row, Col,FontSize: integer;
                               IsNeedXuHao:Boolean=False;IsNeedFrame:Boolean=True);
var
  vRange,vCell1,vCell2:Variant;
  nCol,nRow:Integer;
  sTitleName:String;
begin
  sTitleName:=TitleName;                      //记录标题名
  nCol:=Col;                                    //记录标题所占用的列数
  nRow:=Row;                                    //记录标题所占用的行数
  vCell1:=Sheet.Cells[BeginRow,BeginCol];        //记录起始单元格
  if IsNeedXuHao then                           //需要序号
  begin
    MergeCells(BeginRow,BeginCol,nRow,nCol+1);               //合并单元格
    SetRangeHorizontalAlignment(BeginRow,BeginCol,nRow,nCol+1,xlHAlignCenter);//水平对其方式为居中
    SetRangeVerticalAlignment(BeginRow,BeginCol,nRow,nCol+1,xlVAlignCenter); //垂直对其方式为居中
    //设置字体样式
    SetRangeFontStyle(BeginRow,BeginCol,nRow,nCol+1,'正常','宋体',5,FontSize);         //设置指定范围的字体样式
    SetRangeValue(BeginRow,BeginCol,nRow,nCol+1,sTitleName);     //设置标题
    if IsNeedFrame then                           //需要边框
    begin
      SetRangeOutBorderStyle(BeginRow,BeginCol,nRow,nCol,1,0,1);
    end;  
  end
  else                                          //不需要序号
  begin
    MergeCells(BeginRow,BeginCol,nRow,nCol);               //合并单元格
    SetRangeHorizontalAlignment(BeginRow,BeginCol,nRow,nCol,xlHAlignCenter);//水平对其方式为居中
    SetRangeVerticalAlignment(BeginRow,BeginCol,nRow,nCol,xlVAlignCenter); //垂直对其方式为居中
    //设置字体样式
    SetRangeFontStyle(BeginRow,BeginCol,nRow,nCol,'正常','宋体',5,FontSize);         //设置指定范围的字体样式
    SetRangeValue(BeginRow,BeginCol,nRow,nCol,sTitleName);
    if IsNeedFrame then                           //需要边框
    begin
      SetRangeOutBorderStyle(BeginRow,BeginCol,nRow,nCol,1,0,1);
    end;  
  end;
end;

{
功能描述:设置Excel工作表中的列名
输入参数: AFields      列名列表
          BeginRow     起始行号
          BeginCol     起始列号
          FieldNumber  列数
          IsNeedXuHao  是否需要序号
输出参数:无
创建时间:2006-04-13
创建人:  赵占轻
}
procedure TExcel.SetTheFieldsName(AFields:TStringList;BeginRow,BeginCol,FieldNumber:Integer;IsNeedXuHao:Boolean);
var
  vRange,vCell1,vCell2:Variant;
  nCol,i,j:Integer;
  sValue:String;
begin
  nCol:=BeginCol+FieldNumber-1;
  //从数据库中读出字段名并输出到第一列:
  vCell1:=Sheet.Cells[BeginRow,BeginCol];
  vCell2:=Sheet.Cells[BeginRow,nCol];
  vRange:=Sheet.Range[vCell1,vCell2];
//  SetRangeFontStyle(vRange,'常规','宋体',5,13);      //设置指定范围的字体样式
  if IsNeedXuHao then
  begin
    Sheet.Cells[BeginRow,BeginCol]:='序号';
    for i:=BeginCol to FieldNumber do
    begin
      sValue:=AFields[i-1];
      Sheet.Cells[BeginRow,i+1]:=sValue;
      Sheet.Cells[BeginRow,i+1].Borders.LineStyle:=1;
    end;
    exit;
  end;
  //-----------------------------------------//
  j:=0;
  for i:=BeginCol to FieldNumber+BeginCol-1 do
    begin
      sValue:=AFields[j];
      Sheet.Cells[BeginRow,i].Borders.LineStyle:=1;
      Sheet.Cells[BeginRow,i]:=sValue;
      Sheet.Cells[BeginRow,i].HorizontalAlignment:= xlHAlignCenter;
      Sheet.Cells[BeginRow,i].VerticalAlignment:=xlVAlignCenter;
      j:=j+1;
    end;
end;

{
功能描述:设置打开模板的名称
输入参数: TemplateName:模板名称
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
procedure TExcel.Set_Template_OpenName(TemplateName: String);
begin
  //
  Template_OpenName:=TemplateName;
end;

{
功能描述:设置模板路径
输入参数:PathName:路径名
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
procedure TExcel.Set_Template_Path(PathName: String);
begin
  //
  Template_Path:=PathName;
end;

{
功能描述:转换数据到Excel
输入参数:FieldValues  字段值列表
          BeginRow     起始行号
          BeginCol     起始列号
          FieldNumber  字段数
          RcdNumber    记录数
          IsNeedXuHao  是否需要序号
输出参数:无
创建时间:2006-04-13
创建人:  赵占轻
}
procedure TExcel.TransferDataToExcel(FieldValues:Array of TStringList; BeginRow, BeginCol,
  FieldNumber,RcdNumber: Integer; IsNeedXuHao: Boolean);
var
  i,j,nSerial:Integer;
  sValue:String;
begin
  //
  //设置序号值:
  if IsNeedXuHao then
  begin
    nSerial:=1;
    for i:=BeginRow to BeginRow+RcdNumber do                             //
    begin
       //
      Sheet.Cells[i,BeginCol].Borders.LineStyle:=1;
      Sheet.Cells[i,BeginCol]:=nSerial;
      Sheet.Cells[i,BeginCol].HorizontalAlignment:= xlHAlignCenter;
      Sheet.Cells[i,BeginCol].VerticalAlignment:=xlVAlignCenter;
      //
      if i=BeginRow+RcdNumber-1 then
      begin
        break;
      end;//
      nSerial:=nSerial+1;
    end;//
    BeginCol:=BeginCol+1;
  end;//
  // 传输数据到Excel工作表
  for i:=0 to RcdNumber-1 do
  begin
    for j:=0 to FieldNumber-1 do
    begin
      sValue:=FieldValues[i][j] ;
      Sheet.Cells[BeginRow+i,BeginCol+j].Borders.LineStyle:=1;
      Sheet.Cells[BeginRow+i,BeginCol+j]:=sValue;
      Sheet.Cells[BeginRow+i,BeginCol+j].HorizontalAlignment:= xlHAlignCenter;
      Sheet.Cells[BeginRow+i,BeginCol+j].VerticalAlignment:=xlVAlignCenter;
    end;//
  end;//
end;

{
功能描述:得到当前工作表中某行某列的值
输入参数:Row:指定行号;
          Col:指定单元格列号
输出参数:返回指定单元格的值
创建时间:2006-04-11
创建人:  赵占轻
}
function TExcel.GetCell(Row,Col:Integer):Variant;
begin
  if (Row=0) or (Col=0) then
  begin
    Result:=-1;
    Exit;
  end;
  Result:=Sheet.Cells[Row,Col];
end;

{
功能描述:对指定单元格间的值求和
输入参数:BeginRow   起始单元格的行号
          BeginCol   起始单元格的列号
          EndRow     终止单元格的行号
          EndCol     终止单元格的列号
输出参数:返回总和值
创建时间:2006-04-11
创建人:  赵占轻
}
function TExcel.Sum(BeginRow,BeginCol,EndRow,EndCol:Integer):Double;
var
  dTotal:Double;
  vRange,vCell1,vCell2:Variant;
begin
  vCell1:=Sheet.Cells[BeginRow,BeginCol];          //获取起始单元格
  vCell2:=Sheet.Cells[EndRow,EndCol];              //获取终止单元格
  vRange:=MyExcel.Range[vCell1,vCell2];
  //计算总和
  dTotal:=MyExcel.Sum (vRange,vRange,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
  ,EmptyParam,EmptyParam);

  //
  Result:=dTotal/2.0;
end;//

{
功能描述:设置指定单元格的值
输入参数:Value: 设置的单元格的值
          Row:  指定单元格的行号
          Col:   指定单元格的列号
          AType:单元格值的类型  (0:字符型;1:整形;2,3:数值型)
输出参数:无
创建时间:2006-04-11
创建人:  赵占轻
}
procedure TExcel.SetCell(Value: String; Row, Col: Integer;AType:Integer);
begin
  case AType of
    0: //字符型
      begin
        //格式化单元格为文本样式,确保数据格式正确
        Sheet.Cells[Row,Col].NumberFormatLocal:='@';
      end;
    1:  //整型
      begin
        Sheet.Cells[Row,Col].NumberFormatLocal:='0';
      end;
    2:
      begin
        //格式化单元格为文本样式,确保数据格式正确
        Sheet.Cells[Row,Col].NumberFormatLocal:='0.00000';
      end;
    3: //浮点型
      begin
        Sheet.Cells[Row,Col].NumberFormatLocal:='0.00000';
      end;
  end;
  Sheet.Cells[Row,Col]:=Value;
end;

{
功能描述:设置单元格为自动列宽
输入参数:无
输出参数:无
创建时间:2003-11-20
创建人:  赵占轻
}
procedure TExcel.CellAutoFit;
begin
  MyExcel.Columns.AutoFit;
end;

{
功能描述: 打开指定的Excel文件
输入参数: FileName:要打开的文件名   如:c:\test.xls
输出参数: 执行成功返回真否则返回假
创建时间: 2006-04-07
设计人:   赵占轻
}
function TExcel.OpenExcelFile(FileName: string): Boolean;
begin
  Result:=False;
  try
    Book:=MyExcel.Workbooks.Open(FileName);
    Sheet:=Book.WorkSheets.Item[1];
  except
    Exit;
  end;
  Result:=True;
end;


{
功能描述:    得到指定的工作表
输入参数:    SheetNum:工作表顺序号;ASheet:记录获取的工作表
输出参数:    执行成功返回真否则返回假
输入输出参数:ASheet:返回获取的工作表
创建时间:    2006-04-11
创建人:       赵占轻
}

{
功能描述:获取当前活动工作簿中指定工作表的工作范围
输入参数:ASheet:要操作的工作表;SheetNum:指定工作簿中工作表的序号
输出参数:工作区地址字符串 如"A1:Z200"
创建时间: 2006-04-07
创建人:   赵占轻
}
function TExcel.GetSheetRange:string;
var
  sTemp:String;
  i:Integer;
begin
  sTemp:=Sheet.UsedRange.AddressLocal;                //取指定工作表中的工作范围
  //分析字符串提取行列号
  for i:=1 to Length(sTemp) do
  begin
    if sTemp[i]<>'$' then
    begin
      Result:=Result+sTemp[i];
    end;
  end;
  if Pos(':',ReSult)=0 then
  begin
    UsedRowBeg:=GetRowNum(ReSult);  //起始行号
    UsedColBeg:=GetColNum(Result);  //起始列号
    UsedRowEnd:=UsedRowBeg;           //终止行号
    UsedColEnd:=UsedColBeg;           //终止列号
    Result:=Result+':'+Result;
  end
  else
  begin
    sTemp:=Copy(Result,1,Pos(':',Result)-1);
    UsedRowBeg:=GetRowNum(sTemp); //起始行号
    UsedColBeg:=GetColNum(sTemp); //其实列号
    sTemp:=Copy(Result,Pos(':',Result)+1,Length(Result)-Pos(':',Result));
    UsedRowEnd:=GetRowNum(sTemp);  //终止行号
    UsedColEnd:=GetColNum(sTemp);  //终止列号
  end;
end;

{
功能描述:获取指定单元格的列号
输入参数: TStr:单元格地址
输出参数: 单元格列号
创建时间: 2006-04-07
创建人:   赵占轻
}
function TExcel.GetColNum(TStr:string):Integer;
begin
  Result:=StrToInt(Sheet.Range[TStr].Cells.Column);
end;

{
功能描述:获取指定单元格的行号
输入参数: TStr:单元格地址
输出参数: 单元格行号
创建时间: 2006-04-07
创建人:   赵占轻
}
function TExcel.GetRowNum(TStr:string):Integer;
begin
  Result:=StrToInt(Sheet.Range[TStr].Cells.Row);
end;

{
功能描述:    得到当前工作簿中所有工作表名称
输入参数:    FileName:用于返回获取的所有工作表名称
输出参数:    操作结果 True:执行成功;False: 执行失败
输入输出参数:FileName:用于返回获取的所有工作表名称
创建时间:    2003-10-24
创建人:      赵占轻
}
function TExcel.GetSheetsNameList(var FileName:TstringList):Boolean;
var
  i:Integer;
  vSheet:Variant;
begin
  try
    for i:=1 to MyExcel.ActiveWorkbook.Sheets.Count do
    begin
      vSheet:=MyExcel.ActiveWorkbook.Worksheets[i];       //获取第i个工作表
      FileName.Add(VSheet.Name);                          //记录第i个工作表的名称
    end;
  except
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{
功能描述:合并单元格
输入参数:ABegRowNum:  待合并单元格的起始行号
          ABegColNum:   起始列号
          AEndRowNum:  终止行号
          AEndColNum:  终止列号
输出参数: 返回合并后的单元格
创建时间: 2006-04-11
创建人:  赵占轻
}
function  TExcel.MergeCells(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer):Variant;           //合并单元格
var
  vRange:Variant;
  vBegCell,vEndCell:Variant;
begin
  try
    vBegCell:=Sheet.Cells[ABegRowNum,ABegColNum];
    vEndCell:=Sheet.Cells[AEndRowNum,AEndColNum];
    vRange:=Sheet.Range[vBegCell,vEndCell];              //获取指定单元格间的范围
    vRange.Merge;                                  //合并单元格
    Result:=vRange;
  except
    Result:=Null;
  end;
end;

{
功能描述:设置指定范围单元格的字体样式
输入参数:BeginRow    指定范围的起始单元格行号;
          BeginCol    指定范围的起始单元格列号;
          EndRow      指定范围的终止单元格行号
          EndCol      指定范围的终止单元格列号
          AFontStyle  目标字体样式(如粗体);
          AFontName   目标字体名称;
          AFontSize   目标字体大小
          AFontColor  目标字体颜色
输出参数:执行成功返回真;否则返回假
创建时间:2006-04-12
创建人:  赵占轻
}
function TExcel.SetRangeFontStyle(BeginRow,BeginCol,EndRow, EndCol:Integer;
                                AFontStyle:string='常规';AFontName:string='宋体';
                                AFontColor:Integer=0;AFontSize:Integer=12):Boolean;  //设置范围  
var
  vRange:Variant;
  vBegCel,vEndCell:Variant;
begin
  try
    vBegCel:=Sheet.Cells[BeginRow,BeginCol];
    vEndCell:=Sheet.Cells[EndRow, EndCol];
    vRange:=Sheet.Range[vBegCel,vEndCell];
    vRange.Font.ColorIndex:=AFontColor;                 //设置字体颜色
    vRange.Font.Name:=AFontName;                        //设置字体名称
    vRange.Font.Size:=AFontSize;                        //设置字体大小
    vRange.Font.FontStyle:=AFontStyle;                  //设置字体样式
  except
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{
功能描述:设置指定范围的外边框样式
输入参数:ABegRowNum    待设置范围的起始行号
          ABegColNum    待设置范围的起始列号
          AEndRowNum    待设置范围的终止行号
          AEndColNum    待设置范围的终止列号
          AWeight       边线粗度
          ALineColor    边线颜色
          ALineStyle    边线样式
          
输出参数:执行成功返回真否则返回假
创建时间:2006-05-08
创建人:  赵占轻
}
function TExcel.SetRangeOutBorderStyle(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;
                                 AWeight:Integer=2;ALineColor:Integer=0;ALineStyle:Integer=xlContinuous):Boolean;
var
  vBegCell,vEndCell:Variant;
  vRange:Variant;
begin
  vBegCell:=Sheet.Cells[ABegRowNum,ABegColNum];
  vEndCell:=Sheet.Cells[AEndRowNum,AEndColNum];
  vRange:=Sheet.Range[vBegCell,vEndCell];
  try
    //设置左边框
    vRange.Borders.Item[xlEdgeLeft].LineStyle :=ALineStyle;
    vRange.Borders.Item[xlEdgeLeft].Weight := AWeight;
    vRange.Borders.Item[xlEdgeLeft].ColorIndex := ALineColor;
  except
    Result:=False;
    Exit;
  end;
  try
    //设置顶边框
    vRange.Borders.Item[xlEdgeTop].LineStyle := ALineStyle;
    vRange.Borders.Item[xlEdgeTop].Weight := AWeight;
    vRange.Borders.Item[xlEdgeTop].ColorIndex := ALineColor;
  except
    Result:=False;
    Exit;
  end;
  try
    //设置底边框
    vRange.Borders.Item[xlEdgeBottom].LineStyle := ALineStyle;
    vRange.Borders.Item[xlEdgeBottom].Weight := AWeight;
    vRange.Borders.Item[xlEdgeBottom].ColorIndex := ALineColor;
  except
    Result:=False;
    Exit;
  end;
  try
    //设置右边框
    vRange.Borders.Item[xlEdgeRight].LineStyle := ALineStyle;
    vRange.Borders.Item[xlEdgeRight].Weight := AWeight;
    vRange.Borders.Item[xlEdgeRight].ColorIndex := ALineColor;
  except
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{
功能描述:设置指定范围的分隔线样式
输入参数:ABegRowNum   待设置范围的起始行号
          ABegColNum   待设置范围的起始列号
          AEndRowNum   待设置范围的终止行号
          AEndColNum   待设置范围的终止列号
          AWeight      分割线粗度
          ALineColor   分割线颜色
          ALineStyle   分割线样式
          
输出参数:执行成功返回真否则返回假
创建时间:2006-04-13
创建人:  赵占轻
}
function  TExcel.SetRangeInnerLineStyle(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;
                   AWeight:Integer=2;ALineColor:Integer=0;ALineStyle:Integer=xlContinuous):Boolean;        //设置指定范围的分隔线样式
var
  vBegCell,vEndCell:Variant;
  vRange:Variant;
begin
  vBegCell:=Sheet.Cells[ABegRowNum,ABegColNum];
  vEndCell:=Sheet.Cells[AEndRowNum,AEndColNum];
  vRange:=Sheet.Range[vBegCell,vEndCell];
  try
    //设置垂直线样式
    vRange.Borders.Item[xlInsideVertical].LineStyle :=ALineStyle;
    vRange.Borders.Item[xlInsideVertical].Weight := AWeight;
    vRange.Borders.Item[xlInsideVertical].ColorIndex := ALineColor;
  except
    Result:=False;
    Exit;
  end;
  try
    //设置水平线样式
    vRange.Borders.Item[xlInsideHorizontal].LineStyle := ALineStyle;
    vRange.Borders.Item[xlInsideHorizontal].Weight := AWeight;
    vRange.Borders.Item[xlInsideHorizontal].ColorIndex := ALineColor;
  except
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{
功能描述:设置当前工作表,并将指定的工作表存储到类属性Sheet中供其他操作使用
输入参数:待指定的工作表的序号
输出参数:执行成功返回真否则返回假
创建时间:2006-04-13
创建人:  赵占轻
}
function TExcel.SetCurrentSheet(SheetNum:Integer):Boolean;             //设置当前工作簿
begin
  Result:=False;
  try
    Sheet:=MyExcel.Worksheets.Item[SheetNum];
    Result:=True;
  except
  end;
end;

{
功能描述:设置范围的水平对其方式
输入参数:ABegRowNum   待设置的范围的起始行号
          ABegColNum   待设置的范围的起始列号
          AEndRowNum   待设置的范围的终止行号
          AEndColNum   待设置的范围的终止列号
          AAlign       对其方式
输出参数:执行成功返回真否则返回假
创建时间:2006-04-13
创建人:  赵占轻
}
function TExcel.SetRangeHorizontalAlignment(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;AAlign:Integer=xlLeft):Boolean;
var
  vBegCell,vEndCell:Variant;
  vRange:Variant;
begin
  try
    vBegCell:=Sheet.Cells[ABegRowNum,ABegColNum];
    vEndCell:=Sheet.Cells[AEndRowNum,AEndColNum];
    vRange:=Sheet.Range[vBegCell,vEndCell];
    vRange.HorizontalAlignment:= AAlign;   //水平对其方式为居中
  except
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{
功能描述:设置范围的垂直对其方式
输入参数:ABegRowNum          待设置的范围起始行号
          ABegColNum          待设置的范围起始列号
          AEndRowNum          待设置的范围终止行号
          AEndColNum          待设置的范围终止列号
          AAlign              对其方式
输出参数:执行成功返回真否则返回假
创建时间:2006-04-13
创建人:  赵占轻
}
function TExcel.SetRangeVerticalAlignment(ABegRowNum,ABegColNum,AEndRowNum,AEndColNum:Integer;AAlign:Integer=xlBottom):Boolean;  //设置范围的垂直对其方式
var
  vBegCell,vEndCell:Variant;
  vRange:Variant;
begin
  try
    vBegCell:=Sheet.Cells[ABegRowNum,ABegColNum];
    vEndCell:=Sheet.Cells[AEndRowNum,AEndColNum];
    vRange:=Sheet.Range[vBegCell,vEndCell];
    vRange.VerticalAlignment:= AAlign;   //垂直对其方式为居中
  except
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{
功能描述:对指定的工作表进行页面设置
输入参数:APageType:       纸张类型;
          Orientation:      页面方向(横向或纵向)
          LeftMargin:      左边距
          RightMargin:     右边距
          TopMargin:       上边距
          BottomMargin:    下边距
          HeaderMargin:    页眉到顶端边距
          FooterMargin:     页脚到底端边距
          
输出参数:执行成功返回真否则返回假
创建时间: 2006-04-18
创建人:  赵占轻
}
function TExcel.SetSheetPage(APageType,Orientation:Integer;LeftMargin,RightMargin,
         TopMargin,BottomMargin,HeaderMargin,FooterMargin:Double):Boolean;
begin
  try
    //页面设置
    Sheet.PageSetup.PaperSize := APageType;   //     设定纸张
    Sheet.PageSetup.Orientation := Orientation;//     横向纸张
    //设定页边距
    Sheet.PageSetup.LeftMargin := MyExcel.InchesToPoints(LeftMargin);
    Sheet.PageSetup.RightMargin := MyExcel.InchesToPoints(RightMargin);
    Sheet.PageSetup.TopMargin := MyExcel.InchesToPoints(TopMargin);
    Sheet.PageSetup.BottomMargin := MyExcel.InchesToPoints(BottomMargin);  //1cm
    Sheet.PageSetup.HeaderMargin := MyExcel.InchesToPoints(HeaderMargin);
    Sheet.PageSetup.FooterMargin := MyExcel.InchesToPoints(FooterMargin);
  except
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

{
功能描述:设置指定列的宽度
输入参数:AColNum:   待设置的列的列号
          AColWidth:目标列宽
输出参数:执行成功返回真否则返回假
创建时间:2006-04-13
创建人:  赵占轻
}
function TExcel.SetColumnWidth(AColNum:Integer;AColWidth:Double):Boolean;
begin
  try
    Sheet.Columns[AColNum].ColumnWidth:=AColWidth;
  except
    Result:=False;
    Exit;
  end;
  Result:=True;

end;

{
功能描述:设置指定范围内单元格的列宽
输入参数:ABeginRow  指定范围的起始行号
          ABeginCol  指定范围的起始列号
          AEndRow    指定范围的终止行号
          AEndCol    指定范围的终止列号
          AColWidth:列宽
输出参数:执行成功返回真否则返回假
创建时间:2006-04-17
创建人:  赵占轻
}
function TExcel.SetRangeColWidth(ABeginRow,ABeginCol,AEndRow,AEndCol:Integer;AColWidth:Double):Boolean;
var
  vRange,vBeginCell,vEndCell:Variant;
begin
  try
    vBeginCell:=Sheet.Cells[ABeginRow,ABeginCol];
    vEndCell:=Sheet.Cells[AEndRow,AEndCol];
    vRange:=Sheet.Range[vBeginCell,vEndCell];
    vRange.ColumnWidth:=AColWidth;
 except
   Result:=False;
   Exit;
 end;
 Result:=True;
end;

{
功能描述:绘制平断面图时根据不同情况格式化单元格
输入参数:APage      当前页号
          AColNum    列数
          ARow       行号
          AColName   列名
          AValueList 待填充的值的列表
输出参数:无
创建时间:2006-04-18
创建人:  赵占轻
}
procedure TExcel.FormatCells(APage, AColNum, ARow: Integer;AColName: String;AValueList: TStringList);
var
  nBegRow,nEndRow:Double;
  i: Integer;
  sValue: String;
  nIndex: Integer;
  dX,dY: Double;
  dX1, dY1: Double;
  nPageTop:Integer;
  dAngle: Double;
  dOldY2: Double;
  dGanGao: Double;
  bIsFirstPole, bIsTJPole: Boolean;
  bIsCable: Boolean;
begin
  dAngle:=0;
  nPageTop := (APage-1)*pm_PageRows;        //每页行数
  //处理行高
  case ARow of
    6, 13:
      begin
        //情况特殊,需要加高
        Sheet.Rows[nPage

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
基于MATLAB的均值滤波算法实现发布时间:2022-07-18
下一篇:
基于MATLAB的中值滤波算法实现发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap