zoukankan      html  css  js  c++  java
  • 收集的一些零散代码

    1.格式化文本,动态添加空格

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    function formatStr(const srcStr: string;Leng:integer): string;
    var
      i, iLen: integer;
    begin
      //先把原字符串赋值给返回字符串Result
      Result:= srcStr;
      //计算相差多少空格
      iLen:= Leng - Length(srcStr);
      //在返回字符串后,补齐空格
      for i:= 1 to iLen do
      begin
        Result:= Result + ' ';
      end;
    end;

    2.给Panle添加有颜色的边框

    代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    unit Unit1;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls;
      type
      TPanel = class(ExtCtrls.TPanel)
      public
        procedure Paint; override;
      end;
    type
      TForm1 = class(TForm)
        Panel1: TPanel;
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;
    implementation
     
    {$R *.dfm}
     
    { TPanel }
    procedure TPanel.Paint;
    begin
      inherited;
      Canvas.Pen.Color :=clBlue;
      Canvas.Rectangle(ClientRect);
    end;
     
    end.

     3.将DBGRID的数据导出到EXCEL

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    function ExportToExcel(dbgrid:tdbgrid):boolean;
       const
           xlNormal=-4143;
       var
           i,j,k:integer;
           str,filename:string;
           excel:OleVariant;
           SavePlace:    TBookmark;
           savedialog:tsavedialog;
           ProgressBar1:TProgressBar;
       begin
           result:=false;
           filename:='';
           if   dbgrid.DataSource.DataSet.RecordCount>65536    then
                 begin   
                     if application.messagebox('需要导出的数据过大,Excel最大只能容纳65536行,是否还要继续?','询问',mb_yesno+mb_iconquestion)=idno    then
                        exit;
                 end;   
           screen.Cursor:=crHourGlass;
           try   
               excel:=CreateOleObject('Excel.Application');
               excel.workbooks.add;   
           except
               screen.cursor:=crDefault;
               showmessage('无法调用Excel!');   
               exit;   
           end;   
           savedialog:=tsavedialog.Create(nil);   
           savedialog.Filter:='Excel文件(*.xls)|*.xls';   
           if    savedialog.Execute    then
                 begin
                     if    FileExists(savedialog.FileName)    then   
                           try   
                               if    application.messagebox('该文件已经存在,要覆盖吗?','询问',mb_yesno+mb_iconquestion)=idyes    then   
                                     DeleteFile(PChar(savedialog.FileName))
                               else   
                                     begin
                                         Excel.Quit;
                                         savedialog.free;
                                         screen.cursor:=crDefault;
                                         Exit;
                                     end;   
                           except
                               Excel.Quit;
                               savedialog.free;
                                 screen.cursor:=crDefault;
                               Exit;   
                           end;
                     filename:=savedialog.FileName;   
                 end;
           savedialog.free;
           application.ProcessMessages;   
           if    filename=''    then   
                 begin   
                     result:=false;
                     Excel.Quit;   
                     screen.cursor:=crDefault;
                     exit;   
                 end;   
           k:=0;   
           for    i:=0    to    dbgrid.Columns.count-1    do   
               begin   
                   if    dbgrid.Columns.Items[i].Visible    then   
                         begin   
                             //Excel.Columns[k+1].ColumnWidth:=dbgrid.Columns.Items[i].Title.Column.Width;   
                             excel.cells[1,k+1]:=dbgrid.Columns.Items[i].Title.Caption;
                             inc(k);   
                         end;   
               end;   
         
           dbgrid.DataSource.DataSet.DisableControls;   
           saveplace:=dbgrid.DataSource.DataSet.GetBookmark;   
           dbgrid.DataSource.dataset.First;   
           i:=2;   
           if    dbgrid.DataSource.DataSet.recordcount>65536    then
                 ProgressBar1:=ProgressBarform(65536)
           else   
                 ProgressBar1:=ProgressBarform(dbgrid.DataSource.DataSet.recordcount);   
           while    not    dbgrid.DataSource.dataset.Eof    do   
               begin   
                   k:=0;   
                   for    j:=0    to    dbgrid.Columns.count-1    do   
                       begin   
                           if    dbgrid.Columns.Items[j].Visible    then
                                 begin   
                                     excel.cells[i,k+1].NumberFormat:='@';   
                                     if    not    dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).isnull    then   
                                           begin   
                                               str := dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).value;
                                               Excel.Cells[i, k + 1] := Str;
                                           end;   
                                     inc(k);   
                                 end
                           else
                                 continue;
                       end;   
                   if    i=65536    then   
                         break;   
                   inc(i);   
                   ProgressBar1.StepBy(1);
                   dbgrid.DataSource.dataset.next;
               end;
           progressbar1.Owner.Free;
           application.ProcessMessages;
           dbgrid.DataSource.dataset.GotoBookmark(SavePlace);
           dbgrid.DataSource.dataset.EnableControls;
         
           try
               if    copy(FileName,length(FileName)-3,4)<>'.xls'    then
                     FileName:=FileName+'.xls';
               Excel.ActiveWorkbook.SaveAs(FileName,xlNormal,'''',False,False);
           except
               Excel.Quit;
                 screen.cursor:=crDefault;
               exit;
           end;
           //Excel.Visible    :=    true;
           Excel.Quit;
           screen.cursor:=crDefault;
           Result:= true;
       end;

    4.将Adoquery的数据导出到Excel

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    function queryExportToExcel(queryexport:tadoquery):boolean;
       const
           xlNormal=-4143;
       var
           i,j,k:integer;
           str,filename:string;
           excel:OleVariant;
           savedialog:tsavedialog;
           ProgressBar1:TProgressBar;
       begin
           result:=false;
           filename:='';
           if   queryexport.RecordCount>65536    then
                 begin   
                     if    application.messagebox('需要导出的数据过大,Excel最大只能容纳65536行,是否还要继续?','询问',mb_yesno+mb_iconquestion)=idno    then
                           exit;   
                 end;   
           screen.Cursor:=crHourGlass;
           try   
               excel:=CreateOleObject('Excel.Application');
               excel.workbooks.add;   
           except   
                 screen.cursor:=crDefault;   
               showmessage('无法调用Excel!');   
               exit;   
           end;   
           savedialog:=tsavedialog.Create(nil);   
           savedialog.Filter:='Excel文件(*.xls)|*.xls';   
           if    savedialog.Execute    then
                 begin
                     if    FileExists(savedialog.FileName)    then   
                           try   
                               if    application.messagebox('该文件已经存在,要覆盖吗?','询问',mb_yesno+mb_iconquestion)=idyes    then
                                     DeleteFile(PChar(savedialog.FileName))
                               else   
                                     begin
                                         Excel.Quit;
                                         savedialog.free;
                                         screen.cursor:=crDefault;
                                         Exit;
                                     end;
                           except
                               Excel.Quit;
                               savedialog.free;
                                 screen.cursor:=crDefault;
                               Exit;   
                           end;
                     filename:=savedialog.FileName;   
                 end;
           savedialog.free;
           application.ProcessMessages;   
           if filename='' then
              begin
                 result:=false;
                 Excel.Quit;
                 screen.cursor:=crDefault;
                 exit;
              end;   
           k:=0;   
           for i:=0 to queryexport.FieldCount-1    do
               begin
                excel.cells[1,k+1]:=queryexport.Fields[i].FieldName;
                inc(k);
               end;
           queryexport.First;
           i:=2;   
           if    queryexport.recordcount>65536    then
                 ProgressBar1:=ProgressBarform(65536)
           else   
                 ProgressBar1:=ProgressBarform(queryexport.recordcount);
           while    not    queryexport.Eof    do
               begin
                   k:=0;   
                   for j:=0 to queryexport.FieldCount-1    do
                       begin   
                          excel.cells[i,k+1].NumberFormat:='@';
                          if not queryexport.fieldbyname(queryexport.Fields[j].FieldName).isnull    then
                             begin
                                str:=queryexport.fieldbyname(queryexport.Fields[j].FieldName).AsString;
                                Excel.Cells[i,    k    +    1]    :=    Str;
                             end;
                       inc(k);
                       end;
                   if i=65536 then
                      break;
                   inc(i);   
                   ProgressBar1.StepBy(1);   
                   queryexport.next;   
               end;
           progressbar1.Owner.Free;
           application.ProcessMessages;
           try
               if copy(FileName,length(FileName)-3,4)<>'.xls' then
                  FileName:=FileName+'.xls';
               Excel.ActiveWorkbook.SaveAs(FileName,xlNormal,'''',False,False);
           except
               Excel.Quit;
                 screen.cursor:=crDefault;
               exit;
           end;
           //Excel.Visible    :=    true;
           Excel.Quit;
           screen.cursor:=crDefault;
           Result :=  true;
       end;
     
    end.
  • 相关阅读:
    简单查询plan
    EXP AND IMP
    (4.16)sql server迁移DB文件(同一DB内)
    sql server日志传送实践(基于server 2008 R2)
    (1.3)学习笔记之mysql体系结构(C/S整体架构、内存结构、物理存储结构、逻辑结构、SQL查询流程)
    (1.2)学习笔记之mysql体系结构(数据库文件)
    (1.1)学习笔记之mysql体系结构(内存、进程、线程)
    SSAS(SQL Server 分析服务)、***S(SQL Server报表服务)、SSIS(SQL Server集成服务)
    教你使用SQL查询(1-12)
    Sql Server内置函数实现MD5加密
  • 原文地址:https://www.cnblogs.com/stroll/p/3995693.html
Copyright © 2011-2022 走看看