通用EXCEL清册比对合并

    信息化时代,办公电子化已成了每个部门单位不可或缺的组成部分,其中也免不了要经常
使用微软的EXCEL电子表格,通过对电子表格的灵活运用,极大方便和提高了办公效率。
 在具体工作中,经常会碰到两张电子表格的比对或合并问题。即你所需要的信息往往是在
两张不同的表格中。如果要手工把两张表格的内容根据某个关键字段比对或合并在一起,将会
消耗您大量宝贵的时间和精力。为此,作者专门编写了一个通用的EXCEL清册比对合并软件,来
提高效率,并把它分享给每一位需要的朋友。大家如果在使用中发现问题或有更好的建议可以
联系QQ:175624411,或来信EMAIL:zjdyzqh@163.com,我将尽力予以吸纳改善该软件系统。

效果图:

代码如下:

//工徎文件

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2},
  Unit3 in 'Unit3.pas' {Form3};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.Title := '清册比对';
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

//主单元文件

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls, ComCtrls, ExtCtrls, Buttons;

type
  TForm1 = class(TForm)
    btnGetData: TButton;
    sgSheet1: TStringGrid;
    Label1: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    cmbSheet1: TComboBox;
    btnOk: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    chkTitle: TCheckBox;
    chkDispLabel: TCheckBox;
    Panel3: TPanel;
    chkDataRange: TCheckBox;
    rgDispLabel: TRadioGroup;
    edtDispLabelSheet1: TEdit;
    rgDataRange: TRadioGroup;
    Panel4: TPanel;
    edtDispLabelSheet2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    edtFile1: TEdit;
    bbtnOpen1: TBitBtn;
    Label6: TLabel;
    cmbSheet2: TComboBox;
    edtFile2: TEdit;
    bbtnOpen2: TBitBtn;
    sgSheet2: TStringGrid;
    edtTitle: TEdit;
    odFile1: TOpenDialog;
    odFile2: TOpenDialog;
    sbTip: TStatusBar;
    Label7: TLabel;
    Timer1: TTimer;
    procedure btnGetDataClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure sgSheet1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure sgSheet1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormDestroy(Sender: TObject);
    procedure cmbSheet1KeyPress(Sender: TObject; var Key: Char);
    procedure sgSheet1Click(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure bbtnOpen1Click(Sender: TObject);
    procedure bbtnOpen2Click(Sender: TObject);
    procedure chkTitleClick(Sender: TObject);
    procedure chkDispLabelClick(Sender: TObject);
    procedure chkDataRangeClick(Sender: TObject);
    procedure sgSheet2SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
    procedure InitControlState;
    function InvalidCheck : Boolean;

  public
    { Public declarations }
  end;



var
  Form1: TForm1;

implementation

{$R *.dfm}

uses ComObj, Gauges, Unit2, Unit3;

var
  xlApp, xlWorkbook: Variant;
  FCheck, FUnCheck: TBitmap;  //复选框位图
  slSource: TStringList;   //结果表内容
  StartRowSheet1, StartRowSheet2: Cardinal;  //起始行
  EndRowSheet1, EndRowSheet2: Cardinal;    //结尾行
  PrimaryColSheet1, PrimaryColSheet2: Cardinal; //关键列
  RowDispLabel1, RowDispLabel2: Cardinal;  //字段所在行
  t: byte;// 控制提示字符动起来

procedure TForm1.bbtnOpen1Click(Sender: TObject);
var
  i: integer;
begin
  odFile1.Filter := '*.xls';
  if odFile1.Execute then
  begin
    if not FileExists(odFile1.FileName) then
    begin
      ShowMessage('你选择的文件不存在,请重新选择!');
      Exit;
    end;
    edtFile1.ReadOnly := False;
    edtFile1.Text := odFile1.FileName;
    edtFile1.ReadOnly := True;

    //打开工作薄
    xlWorkbook := xlApp.WorkBooks.Open(edtFile1.Text);
    //初始化工作表选项
    cmbSheet1.Clear;
    for i := 1 to xlWorkbook.Sheets.Count do
      cmbSheet1.Items.Add(xlWorkbook.Sheets[i].name);
    cmbSheet1.ItemIndex := 0;

    xlWorkbook.Close;
  end;
end;

procedure TForm1.bbtnOpen2Click(Sender: TObject);
var
  i: integer;
begin
  odFile2.Filter := '*.xls';
  if odFile2.Execute then
  begin
    if not FileExists(odFile2.FileName) then
    begin
      ShowMessage('你选择的文件不存在,请重新选择!');
      Exit;
    end;
    edtFile2.ReadOnly := False;
    edtFile2.Text := odFile2.FileName;
    edtFile2.ReadOnly := True;

    //打开工作薄
    xlWorkbook := xlApp.WorkBooks.Open(edtFile2.Text);
    //初始化工作表选项
    cmbSheet2.Clear;
    for i := 1 to xlWorkbook.Sheets.Count do
      cmbSheet2.Items.Add(xlWorkbook.Sheets[i].name);
    cmbSheet2.ItemIndex := 0;

    xlWorkbook.Close;
  end;
end;

procedure TForm1.btnGetDataClick(Sender: TObject);
var
  x, y: Integer;
  frm: TForm; //显示进度条的窗体
  gg: TGauge;
{var
  id : THandle;}
begin
  //有效性检验
  if (cmbSheet1.Text = '') or (cmbSheet2.Text = '') then
  begin
    ShowMessage('请选择要操作的表格后再继续!');
    Exit;
  end;
  chkDispLabel.Enabled := True;
  chkDataRange.Enabled := True;
  btnGetData.Enabled := False;
  Screen.Cursor := crHourGlass;

  //取得表一的已使用单元格行列总数
  xlWorkbook := xlApp.WorkBooks.Open(edtFile1.Text);

  xlWorkbook.Sheets[cmbSheet1.Text].Activate;
  sgSheet1.RowCount := xlWorkbook.ActiveSheet.UsedRange.Rows.Count;
  sgSheet1.ColCount := xlWorkbook.ActiveSheet.UsedRange.Columns.Count;

  //显示进度条
  frm := TForm.Create(nil);
  frm.BorderStyle := bsNone; //去掉边框
  frm.Width := 402;
  frm.Height := 27;
  frm.Position := poScreenCenter;
    gg := TGauge.Create(frm);
    gg.Width := 400;
    gg.Height := 25;
    gg.Left := 1;
    gg.Top := 1;
    gg.MinValue := 0;
    gg.MaxValue := sgSheet1.RowCount;
    gg.Parent := frm;
    gg.BackColor := clSilver;
    gg.ForeColor := clMoneyGreen;

  frm.Show;        //ShowModal会阻塞
  frm.BringToFront;
  //绘复选框状态  准备
  for x := 0 to sgSheet1.ColCount - 1 do
    sgSheet1.Cells[x, 0] := 'y';

  //赋值到StringGrid
  for x := 0 to sgSheet1.RowCount-1 do
  begin
    for y := 0 to sgSheet1.ColCount-1 do
    begin
      try
        sgSheet1.Cells[y, x+1] := xlWorkbook.ActiveSheet.cells[x+1, y+1].value;
        Application.ProcessMessages;
      except
        ShowMessage('出错在表格'+'['+Inttostr(x+1)+','+Inttostr(y+1)+']单元格,请检查!');
        exit;
      end;

    end;
    gg.Progress := gg.Progress + 1;
  end;

  //设置列宽
  for y := 0 to sgSheet1.ColCount-1 do
    sgSheet1.ColWidths[y] := xlWorkbook.ActiveSheet.Columns[y+1].ColumnWidth*5;

  //关闭
  xlWorkbook.Close;

  //取得表二的已使用单元格行列总数
  xlWorkbook := xlApp.WorkBooks.Open(edtFile2.Text);

  xlWorkbook.Sheets[cmbSheet2.Text].Activate;
  sgSheet2.RowCount := xlWorkbook.ActiveSheet.UsedRange.Rows.Count;
  sgSheet2.ColCount := xlWorkbook.ActiveSheet.UsedRange.Columns.Count;

 //进度条复位
  gg.Progress := 0;
  gg.MaxValue := sgSheet2.RowCount;
  //赋值到StringGrid
  for x := 0 to sgSheet2.ColCount - 1 do
    sgSheet2.Cells[x, 0] := 'y';

  for x := 0 to sgSheet2.RowCount-1 do
  begin
    for y := 0 to sgSheet2.ColCount-1 do
    begin
      sgSheet2.Cells[y, x+1] := xlWorkbook.ActiveSheet.cells[x+1, y+1].value;
      Application.ProcessMessages;
    end;
    gg.Progress := gg.Progress + 1;
  end;
  //设置列宽
  for y := 0 to sgSheet2.ColCount-1 do
    sgSheet2.ColWidths[y] := xlWorkbook.ActiveSheet.Columns[y+1].ColumnWidth*5;

  //退出进度条窗体
  frm.ModalResult := 1;
  frm.Free;

  btnGetData.Enabled := True;
  Screen.Cursor := crDefault;
 // CreateThread(nil, 0, @GetData, nil, 0, id);
end;

procedure TForm1.btnOkClick(Sender: TObject);
var
  i,j,k : integer;
  ccols: integer;
  s : string; //表格的行内容
  arrFlag1, arrFlag2: array of Boolean; //存放两张表有否相同记录的标志
  sdSaveName: TSaveDialog;
  sFile: string;  //保存的文件名
  colWidth: array of integer;//输出表列宽
begin
  slSource.Clear;
  //有效性检查
  if not InvalidCheck then Exit;
  chkDispLabel.Enabled := False;
  chkDataRange.Enabled := False;
  btnOk.Enabled := False;
  Screen.Cursor := crHourGlass;
 //保存已选输出列的列宽到数组
  for i := 0 to sgSheet1.ColCount - 1 do
    if sgSheet1.Cells[i, 0] = 'y' then
    begin
      //表一列宽
      setLength(colWidth, Length(colWidth)+1);
      colWidth[High(colWidth)] := sgSheet1.ColWidths[i];
    end;
  //表二列宽
  for i := 0 to sgSheet2.ColCount - 1 do
  if sgSheet2.Cells[i, 0] = 'y' then
  begin
    setLength(colWidth, Length(colWidth)+1);
    colWidth[High(colWidth)] := sgSheet2.ColWidths[i];
  end;

  //开始比对合并
  slSource.Add(edtTitle.Text);
  if chkDispLabel.Checked then
  begin
    for i := 0 to sgSheet1.ColCount - 1 do
      if sgSheet1.Cells[i, 0] = 'y' then
      begin
        s := s + sgSheet1.Cells[i, RowDispLabel1] + Chr(9);
        //保存已选输出列的列宽到数组
        setLength(colWidth, Length(colWidth)+1);
        colWidth[High(colWidth)] := sgSheet1.ColWidths[i] div 5;
      end;

    for i := 0 to sgSheet2.ColCount - 1 do
    if sgSheet2.Cells[i, 0] = 'y' then
    begin
      s := s + sgSheet2.Cells[i, RowDispLabel2] + Chr(9);
     //保存已选输出列的列宽到数组
      setLength(colWidth, Length(colWidth)+1);
      colWidth[High(colWidth)] := sgSheet2.ColWidths[i] div 5;
    end;
    //添加到字符串列表
    slSource.Add(s);
  end;

  s := '';
  SetLength(arrFlag1, sgSheet1.RowCount);
  SetLength(arrFlag2, sgSheet2.RowCount);
  for i := Low(arrFlag1) to High(arrFlag1) do
    arrFlag1[i] := False;
  for i := Low(arrFlag2) to High(arrFlag2) do
    arrFlag2[i] := False;
  //表间数据比对循环
  for i := StartRowSheet1 to EndRowSheet1 do
  begin
    for j := StartRowSheet2 to EndRowSheet2 do
    begin
      if sgSheet1.Cells[PrimaryColSheet1, i] = sgSheet2.Cells[PrimaryColSheet2, j] then
      begin
        for k := 0 to sgSheet1.ColCount - 1 do
          if sgSheet1.Cells[k, 0] = 'y' then
            begin
              s := s + sgSheet1.Cells[k, i] + Chr(9);
            end;

        for k := 0 to sgSheet2.ColCount - 1 do
          if sgSheet2.Cells[k, 0] = 'y' then
            begin
              s := s + sgSheet2.Cells[k, j] + Chr(9);
            end;

        slSource.Add(s);
        //设置表二相应行号的比对标记
        arrFlag1[i] := True;
        arrFlag2[j] := True;
        //退出  for j 的循环
        Break;
      end;//if sgSheet1.cells
    end;  //for j
    s := '';
  end; //for i

  s := '';
  //将表一独有或表二独有的记录添加到结果列表
  for i := StartRowSheet1 to EndRowSheet1 do
  begin
    //表一独有
    if not arrFlag1[i] then
    begin
      for k := 0 to sgSheet1.ColCount - 1 do
        if sgSheet1.Cells[k, 0] = 'y' then
          begin
            s := s + sgSheet1.Cells[k, i] + Chr(9);
          end;

      slSource.Add(s);
      s := '';
    end;
  end;

   //表二独有
  for i := StartRowSheet2 to EndRowSheet2 do
  begin
    if not arrFlag2[i] then
    begin
      for k := 0 to sgSheet1.ColCount - 1 do
        if sgSheet1.Cells[k, 0] = 'y' then
          begin
            s := s + Chr(64) + Chr(9);
          end;

      for k := 0 to sgSheet2.ColCount - 1 do
        if sgSheet2.Cells[k, 0] = 'y' then
          begin
            s := s + sgSheet2.Cells[k, i] + Chr(9);
          end;

      slSource.Add(s);
      s := '';
    end;

  end;

  btnOk.Enabled := True;
  Screen.Cursor := crDefault;
  //保存结果
  sdSaveName := TSaveDialog.Create(nil);
  try
    sdSaveName.Filter := '*.xls';
    if sdSaveName.Execute then
    begin
      if ExtractFileExt(sdSaveName.FileName) <> '.xls' then
        sdSaveName.FileName := sdSaveName.FileName + '.xls';

      if FileExists(sdSaveName.FileName) then
      begin
        if ID_NO = Application.MessageBox('文件已存在,是否覆盖!', '提示', MB_YesNo) then
        begin
          Exit;
        end;
      end;
      sFile := sdSaveName.FileName;
      slSource.SaveToFile(sFile);
      ShowMessage('恭喜,保存成功!');


      //关闭打开的Excel工作薄 ,为打开结果表准备
      if not VarIsEmpty(xlApp) then
      begin
        xlApp.Workbooks.Close;
        xlWorkbook := UnAssigned;
      end;

      xlWorkbook := xlApp.WorkBooks.Open(sFile);

      //是否要设置格式
      if ID_YES = Application.MessageBox('是否要设置结果表显示格式!', '提示', MB_YesNo) then
      begin
        //设置单元格格式
        //列宽 和//标题
        ccols := xlWorkbook.ActiveSheet.UsedRange.Columns.Count;

        for i := 0 to ccols - 1 do
          xlWorkbook.ActiveSheet.Columns[i+1].ColumnWidth := colWidth[i];

        xlWorkbook.ActiveSheet.Range['A1:'+Char(64+ccols)+'1'].Merge;
        xlWorkbook.ActiveSheet.Rows[1].RowHeight := 1.2/0.035;
        xlWorkbook.ActiveSheet.Rows[1].Font.Name := '隶书';
        xlWorkbook.ActiveSheet.Rows[1].Font.Size := 16;
        xlWorkbook.ActiveSheet.Rows[1].Font.Bold := True;
        xlWorkbook.ActiveSheet.Rows[1].Font.Color := clBlue;
        xlWorkbook.ActiveSheet.Rows[1].HorizontalAlignment := 3; //水平居中
        xlWorkbook.ActiveSheet.Rows[1].VerticalAlignment:= 2;   //垂直居中
        //按指定条件 设置单元格格式
        With TForm2.Create(Self, xlWorkbook) do
        begin
          ShowModal;
        end;
      end;

      //是否打开工作表提示
     { if ID_NO = Application.MessageBox('是否打开保存的结果表!', '提示', MB_YesNo) then
      begin
        Exit;
      end; }
      //显示
      xlApp.Visible := True;
    end;

  finally
    sdSaveName.Free;
  end;

end;

procedure TForm1.chkDataRangeClick(Sender: TObject);
begin
  rgDataRange.Enabled := chkDataRange.Checked;
  if chkDataRange.Checked then rgDataRange.ItemIndex := 0;

end;

procedure TForm1.chkDispLabelClick(Sender: TObject);
begin
  rgDispLabel.Enabled := chkDispLabel.Checked;
  if chkDispLabel.Checked then rgDispLabel.ItemIndex := 0;
end;

procedure TForm1.chkTitleClick(Sender: TObject);
begin
  edtTitle.Enabled := chkTitle.Checked;
end;

procedure TForm1.cmbSheet1KeyPress(Sender: TObject; var Key: Char);
begin
  key := #0;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in ['0'..'9', #13, #8]) then key := #0;
end;

procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in ['0'..'9', #13, #8]) then key := #0;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
  bmp: TBitmap;
begin

  btnGetData.Caption := '提取数据';
  btnOk.Caption := '比对合并';
  sbTip.Panels[0].Text := '  ' + FormatDateTime('yyyy-mm-dd', now) + '  '+
                          FormatDateTime('ddd', now);

  //打开Excel
  try
    xlApp := CreateOleObject('EXCEL.Application');
  except
    ShowMessage('打开Excel文件有误或你的系统未安装Excel!');
    xlApp.Quit;
    xlApp := Unassigned;
    exit;
  end;

  //创建CheckBox选择框图
  FCheck := TBitmap.Create;
  FUnCheck := TBitmap.Create;

    //画CheckBox 选择框  (先取得系统的复选框位图)
  bmp := TBitmap.Create;
  try
    bmp.Handle := LoadBitmap(0, PChar(OBM_CHECKBOXES));
    with FUnCheck do
    begin
      width := bmp.Width div 4;
      height := bmp.Height div 3;
      Canvas.CopyRect(Canvas.ClipRect, bmp.Canvas, Canvas.ClipRect);
    end;
    with FCheck do
    begin
      width := bmp.Width div 4;
      height := bmp.Height div 3;
      Canvas.CopyRect(Canvas.ClipRect, bmp.Canvas, Rect(width, 0, 2*width, height));
    end;
  finally
    bmp.Free;
  end;

  //初始化控件状态
  InitControlState;
  edtTitle.Enabled := True;
  edtTitle.Text := '结果数据表';
  edtTitle.Enabled := False;
  //初始化 StringList
  slSource:= TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin

  xlWorkbook := Unassigned;

  if not VarIsEmpty(xlApp) then
  begin
    xlApp.Quit;
    xlApp := Unassigned;
  end;

  FCheck.Free;
  FUnCheck.Free;

  slSource.Free;

end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_F1) then
  begin
    with TForm3.Create(Self) do
    begin
      ShowModal;
    end;
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  //初始化焦点放在打开文件按钮上
  bbtnOpen1.SetFocus;
end;

procedure TForm1.sgSheet1Click(Sender: TObject);
begin
  if TStringGrid(Sender).Row = 0  then
    if TStringGrid(Sender).Cells[TStringGrid(Sender).Col, 0] = 'y' then
       TStringGrid(Sender).Cells[TStringGrid(Sender).Col, 0] := 'n'
    else TStringGrid(Sender).Cells[TStringGrid(Sender).Col, 0] := 'y';
end;

procedure TForm1.sgSheet1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  //if not (gdFocused in State) then Exit;

  with TStringGrid(Sender) do
  begin
    //去除蓝色的选框
    Canvas.Brush.Color := clWindow;
    Canvas.FillRect(Rect);
    Canvas.Font.Color := clWindowText;
    Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, Cells[ACol, ARow]);
    //如果选中则画焦点框
    if gdFocused in State then Canvas.DrawFocusRect(Rect);

    if ARow = 0 then
    begin
      if Cells[ACol, ARow] = 'y' then
      begin
        Canvas.Draw(Rect.Left+2, Rect.Top+2, FCheck);
        Canvas.TextOut(Rect.Left+3+FCheck.Width, Rect.Top+2, '列输出');
      end;

      if Cells[ACol, ARow] = 'n' then
      begin
        Canvas.Draw(Rect.Left+2, Rect.Top+2, FUnCheck);
        Canvas.TextOut(Rect.Left+3+FCheck.Width, Rect.Top+2, '列输出');
      end;
    end;
   end;
end;

//选择单元格时,把内容显示出来
procedure TForm1.sgSheet1SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  if Arow > 0  then
  begin
    if chkDispLabel.Checked and (rgDispLabel.ItemIndex = 0) then
    begin
      edtDispLabelSheet1.ReadOnly := False;
      edtDispLabelSheet1.Text := sgSheet1.Cells[ACol, ARow]+ '['+IntToStr(ARow)+']';
      edtDispLabelSheet1.ReadOnly := True;
      RowDispLabel1 := ARow; //表一的字段所在行
    end;

    if chkDataRange.Checked  then
    begin
      if rgDataRange.ItemIndex = 0 then
      begin
        Edit3.ReadOnly := False;
        Edit3.Text := sgSheet1.Cells[ACol, ARow] + '['+IntToStr(ARow)+']';
        Edit3.ReadOnly := True;
        StartRowSheet1 := ARow;//表一的数据起始行
        PrimaryColSheet1 := ACol;
      end;
      if rgDataRange.ItemIndex = 1 then
      begin
        Edit4.ReadOnly := False;
        Edit4.Text := sgSheet1.Cells[ACol, ARow]+ '['+IntToStr(ARow)+']';
        Edit4.ReadOnly := True;
        EndRowSheet1 := ARow; //表一的数据结尾行
      end;
    end;
  end;
end;

procedure TForm1.sgSheet2SelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  if Arow > 0  then
  begin
    if chkDispLabel.Checked and (rgDispLabel.ItemIndex = 1) then
    begin
      edtDispLabelSheet2.ReadOnly := False;
      edtDispLabelSheet2.Text := sgSheet2.Cells[ACol, ARow]+ '['+IntToStr(ARow)+']';
      edtDispLabelSheet2.ReadOnly := True;
      RowDispLabel2 := ARow; //表二的字段所在行
    end;

    if chkDataRange.Checked  then
    begin
      if rgDataRange.ItemIndex = 2 then
      begin
        Edit5.ReadOnly := False;
        Edit5.Text := sgSheet2.Cells[ACol, ARow] + '['+IntToStr(ACol)+','+IntToStr(ARow)+']';
        Edit5.ReadOnly := True;
        StartRowSheet2 := ARow;//表二的数据起始行
        PrimaryColSheet2 := ACol;
      end;
      if rgDataRange.ItemIndex = 3 then
      begin
        Edit6.ReadOnly := False;
        Edit6.Text := sgSheet2.Cells[ACol, ARow]+ '['+IntToStr(ARow)+']';
        Edit6.ReadOnly := True;
        EndRowSheet2 := ARow; //表二的数据结尾行
      end;
    end;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  //根据 t 值的不同动起来
  case t of
    0: sbTip.Panels[2].Alignment := taLeftJustify;
    1: sbTip.Panels[2].Alignment := taCenter;
    2: sbTip.Panels[2].Alignment := taRightJustify;
  end;
  inc(t);
  if t = 3  then  t:= 0;
end;

procedure TForm1.InitControlState;
var
  i : integer;
begin
  edtFile1.ReadOnly := True;
  edtFile2.ReadOnly := True;
  for i := 0 to Panel2.ControlCount - 1 do
    if Panel2.Controls[i] is TEdit then
      TEdit(Panel2.Controls[I]).ReadOnly := True;
  rgDispLabel.Enabled := False;
  rgDataRange.Enabled := False;

  chkDispLabel.Enabled := False;
  chkDataRange.Enabled := False;
end;

function TForm1.InvalidCheck : Boolean;
begin
  Result := True;
  //如果数据区域未设置则提示
  if not chkDataRange.Checked then
  begin
    Result := False;
    ShowMessage('请先设置数据区域后再继续!');
    Exit;
  end
  else if (Edit3.Text='') or (Edit4.Text='') or (Edit5.Text='') or
          (Edit6.Text='') or (rgDataRange.ItemIndex <> 4) then
  begin
    Result := False;
    ShowMessage('请先把数据区域的各项参数设置完整后再继续!');
    Exit;
  end;

  //字段设置完整性检查
  if chkDispLabel.Checked then
  begin
    if (edtDispLabelSheet1.Text='') or (edtDispLabelSheet2.Text='') or
       (rgDispLabel.ItemIndex <> 2) then
    begin
      Result := False;
      ShowMessage('请先把字段区域的各项参数设置完整后再继续!');
      Exit;
    end;
  end;
end;

end.

//主窗体文件

object Form1: TForm1
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu, biMinimize]
  Caption = #36890#29992#28165#20876#27604#23545#21512#24182
  ClientHeight = 541
  ClientWidth = 676
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poOwnerFormCenter
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  OnKeyDown = FormKeyDown
  OnShow = FormShow
  PixelsPerInch = 96
  TextHeight = 13
  object Label4: TLabel
    Left = 271
    Top = 70
    Width = 12
    Height = 39
    Caption = #24037#20316#34920
    WordWrap = True
  end
  object Label5: TLabel
    Left = 271
    Top = 295
    Width = 12
    Height = 39
    Caption = #24037#20316#34920
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
    WordWrap = True
  end
  object Label1: TLabel
    Left = 8
    Top = 56
    Width = 48
    Height = 19
    Caption = #34920#19968#65306
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clMenuHighlight
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label2: TLabel
    Left = 5
    Top = 289
    Width = 48
    Height = 19
    Caption = #34920#20108#65306
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clMenuHighlight
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentColor = False
    ParentFont = False
  end
  object Label3: TLabel
    Left = 13
    Top = 91
    Width = 48
    Height = 13
    Caption = #36873#25321#25991#20214
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label6: TLabel
    Left = 401
    Top = 66
    Width = 80
    Height = 19
    Caption = #21442#25968#35774#32622#65306
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clMenuHighlight
    Font.Height = -16
    Font.Name = 'Tahoma'
    Font.Style = []
    ParentFont = False
  end
  object Label7: TLabel
    Left = 8
    Top = 320
    Width = 48
    Height = 13
    Caption = #36873#25321#25991#20214
  end
  object sgSheet1: TStringGrid
    Left = 5
    Top = 117
    Width = 363
    Height = 166
    DefaultDrawing = False
    FixedCols = 0
    FixedRows = 0
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goColSizing]
    TabOrder = 0
    OnClick = sgSheet1Click
    OnDrawCell = sgSheet1DrawCell
    OnKeyDown = FormKeyDown
    OnSelectCell = sgSheet1SelectCell
  end
  object cmbSheet1: TComboBox
    Left = 290
    Top = 88
    Width = 80
    Height = 21
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
    ParentFont = False
    TabOrder = 1
    OnKeyDown = FormKeyDown
    OnKeyPress = cmbSheet1KeyPress
  end
  object Panel1: TPanel
    Left = 385
    Top = 467
    Width = 113
    Height = 44
    TabOrder = 2
    object btnGetData: TButton
      Left = 1
      Top = 1
      Width = 111
      Height = 41
      Caption = 'btnGetData'
      TabOrder = 0
      OnClick = btnGetDataClick
    end
  end
  object Panel2: TPanel
    Left = 385
    Top = 91
    Width = 283
    Height = 369
    TabOrder = 3
    object chkTitle: TCheckBox
      Left = 16
      Top = 20
      Width = 86
      Height = 17
      Caption = #36755#20986#34920#26631#39064
      TabOrder = 0
      OnClick = chkTitleClick
      OnKeyDown = FormKeyDown
    end
    object chkDispLabel: TCheckBox
      Left = 16
      Top = 62
      Width = 97
      Height = 17
      Caption = #26174#31034#23383#27573#35774#32622
      TabOrder = 1
      OnClick = chkDispLabelClick
      OnKeyDown = FormKeyDown
    end
    object chkDataRange: TCheckBox
      Left = 16
      Top = 187
      Width = 97
      Height = 17
      Caption = #25968#25454#21306#22495#35774#32622
      TabOrder = 2
      OnClick = chkDataRangeClick
      OnKeyDown = FormKeyDown
    end
    object rgDispLabel: TRadioGroup
      Left = 16
      Top = 81
      Width = 170
      Height = 81
      Items.Strings = (
        #34920#19968#23383#27573#25152#22312#34892#65288#20219#24847#21015#65289
        #34920#20108#23383#27573#25152#22312#34892#65288#20219#24847#21015#65289
        #23383#27573#35774#32622#23436#25104)
      TabOrder = 3
    end
    object edtDispLabelSheet1: TEdit
      Left = 187
      Top = 97
      Width = 89
      Height = 21
      ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
      TabOrder = 4
      OnKeyDown = FormKeyDown
    end
    object rgDataRange: TRadioGroup
      Left = 16
      Top = 205
      Width = 169
      Height = 148
      Items.Strings = (
        #34920#19968#36215#22987#34892#65288#20851#38190#21015#65289
        #34920#19968#32467#23614#34892#65288#20219#24847#21015#65289
        #34920#20108#36215#22987#34892#65288#20851#38190#21015#65289
        #34920#20108#32467#23614#34892#65288#20219#24847#21015#65289
        #25968#25454#21306#22495#35774#32622#23436#25104)
      TabOrder = 5
    end
    object edtDispLabelSheet2: TEdit
      Left = 186
      Top = 121
      Width = 89
      Height = 21
      ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
      TabOrder = 6
      OnKeyDown = FormKeyDown
    end
    object Edit3: TEdit
      Left = 186
      Top = 222
      Width = 89
      Height = 21
      ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
      TabOrder = 7
      OnKeyDown = FormKeyDown
    end
    object Edit4: TEdit
      Left = 186
      Top = 246
      Width = 89
      Height = 21
      ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
      TabOrder = 8
      OnKeyDown = FormKeyDown
    end
    object Edit5: TEdit
      Left = 186
      Top = 272
      Width = 89
      Height = 21
      ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
      TabOrder = 9
      OnKeyDown = FormKeyDown
    end
    object Edit6: TEdit
      Left = 186
      Top = 298
      Width = 89
      Height = 21
      ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
      TabOrder = 10
      OnKeyDown = FormKeyDown
    end
    object edtTitle: TEdit
      Left = 107
      Top = 18
      Width = 170
      Height = 21
      ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
      TabOrder = 11
      OnKeyDown = FormKeyDown
    end
  end
  object Panel3: TPanel
    Left = 544
    Top = 470
    Width = 124
    Height = 41
    TabOrder = 4
    object btnOk: TButton
      Left = 1
      Top = 1
      Width = 122
      Height = 40
      Caption = 'btnOk'
      TabOrder = 0
      OnClick = btnOkClick
    end
  end
  object Panel4: TPanel
    Left = 0
    Top = 0
    Width = 676
    Height = 49
    Align = alTop
    BevelKind = bkTile
    BorderWidth = 2
    BorderStyle = bsSingle
    Caption = #36890#29992'EXCEL'#28165#20876#25968#25454#27604#23545#21512#24182#31995#32479'   V0.1'
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clTeal
    Font.Height = -21
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
    TabOrder = 5
  end
  object edtFile1: TEdit
    Left = 65
    Top = 88
    Width = 141
    Height = 21
    ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
    TabOrder = 6
    OnKeyDown = FormKeyDown
  end
  object bbtnOpen1: TBitBtn
    Left = 204
    Top = 87
    Width = 47
    Height = 25
    Caption = #25171#24320
    Default = True
    DoubleBuffered = True
    Glyph.Data = {
      E6040000424DE604000000000000360000002800000014000000140000000100
      180000000000B0040000232E0000232E00000000000000000001FFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD6
      E7D852A75D67B3758FC79DB4D9BED5EBDCF0F7F2FFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF85B68A0080
      18007B0C008019008221008E32109B472BAB614DBA7D72CB9A98DBB8BCE8D1DC
      F2E6F8FCF9FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5196572B9142008923
      00903100983B009D4000A14500A54A00A74B00A34800A74E00AC5802B0602DBE
      7DF0FAF5FFFFFFFFFFFFFFFFFFFFFFFFFDFDFD207B28368F4602913100933500
      9B3F00A34900AA5300B25C01B96805BB6E06B66908B96E03B96D00B86BCCF1E2
      FFFFFFFFFFFFFFFFFFFFFFFFEBF2EC036B0D2C833616994500993900A44A00AF
      5900B96803C3790AC47E0DC47F10C48012C48013C6840BC784A9ECD6FFFFFFFF
      FFFFFFFFFFFFFFFFD2E2D30062001976202E9D54009A3B03A95015B35D1EC06F
      12CB8614D59C17D09517CE911DD39B1ED6A020DAA689EDD3FFFFFFFFFFFFFFFF
      FFFFFFFFB4D0B60065000574103C9C57009B3E05A95121B46130C0752DCD8E17
      D8A222DEAE26DEAD2DE3B632E8C046F0CE82F9E4FDFEFEFFFFFFFFFFFFFFFFFF
      93BE97006700006F05439A551A9A50009A4108A65410AD5E16B9760DCF9624E1
      B131E8BF3CEECB42F4D462FBE37CFEEDECFCFAFFFFFFFFFFFFFFFFFF70AA7600
      650000730D0D892E59B48066A76C87AD788AA86D76C49750D1AA1EDAAB30EBC1
      3FF1CE47F7D871FEEA78FFF0D0FDF7FFFFFFFFFFFFFFFFFF4F96550164000473
      10017E1B049032717719DD8D3DD57C107FB8775CCA927CD5B01FDEB431E4BF3D
      EACB60F1DB61F5E0AFF9EEFFFFFFFFFFFFFFFFFF30863A726000A4711FA77F2E
      B08832B88531CA9A44C68E16CFA454BF8B30D5A56693CFB466DBBF78DFC8B6ED
      E3C8F6EEEBFDFAFFFFFFFFFFFFF2F7F71C81239E6D04CD903FC68C18D6A324DA
      AE2BD2A321C99213CE8A0EC7800BBC5B00A2B07A43FEE055E3BFFFFCFDFFFFFF
      FFFFFFFFFFFFFFFFFFDCEAE10A760F92760ECD8C3DBF8114CB9C28D5AE37DCB3
      2ADDB52ACBBF58B2AF59ACA5607CDFBB2BF7D559DEBDFFFEFEFFFFFFFFFFFFFF
      FFFFFFFFFFC5DAC7006D020A780F3A934030A2532CAA5AB3913EE0AA3DD4B43E
      2ED8AC0DD3A215D5AA17CFA40CC09281DAC1FFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFA2C4A400630000710B00760F00801E00811EB68220D5992DB8A33C18B0752E
      B37E60C7A28DD8C0B6E7D8F6FCFAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF86AF88
      0045000058000D6A182C88435D974FC28724B27408C29E51EEF8F4FFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE3EAE3AAC2AAC7
      D8C7EAF0EAFFFFFFFFEAD5C18B2BA66D1BD0B286FFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFEFDF7F1E7F8F7F5FEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFF}
    ModalResult = 6
    ParentDoubleBuffered = False
    Spacing = 1
    TabOrder = 7
    OnClick = bbtnOpen1Click
    OnKeyDown = FormKeyDown
  end
  object cmbSheet2: TComboBox
    Left = 290
    Top = 314
    Width = 82
    Height = 21
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = []
    ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
    ParentFont = False
    TabOrder = 8
    OnKeyDown = FormKeyDown
    OnKeyPress = cmbSheet1KeyPress
  end
  object edtFile2: TEdit
    Left = 65
    Top = 314
    Width = 141
    Height = 21
    ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
    TabOrder = 9
  end
  object bbtnOpen2: TBitBtn
    Left = 204
    Top = 312
    Width = 47
    Height = 25
    Margins.Left = 1
    Margins.Top = 1
    Margins.Right = 1
    Margins.Bottom = 1
    Caption = #25171#24320
    DoubleBuffered = True
    Glyph.Data = {
      E6040000424DE604000000000000360000002800000014000000140000000100
      180000000000B0040000232E0000232E00000000000000000001FFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD6
      E7D852A75D67B3758FC79DB4D9BED5EBDCF0F7F2FFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF85B68A0080
      18007B0C008019008221008E32109B472BAB614DBA7D72CB9A98DBB8BCE8D1DC
      F2E6F8FCF9FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5196572B9142008923
      00903100983B009D4000A14500A54A00A74B00A34800A74E00AC5802B0602DBE
      7DF0FAF5FFFFFFFFFFFFFFFFFFFFFFFFFDFDFD207B28368F4602913100933500
      9B3F00A34900AA5300B25C01B96805BB6E06B66908B96E03B96D00B86BCCF1E2
      FFFFFFFFFFFFFFFFFFFFFFFFEBF2EC036B0D2C833616994500993900A44A00AF
      5900B96803C3790AC47E0DC47F10C48012C48013C6840BC784A9ECD6FFFFFFFF
      FFFFFFFFFFFFFFFFD2E2D30062001976202E9D54009A3B03A95015B35D1EC06F
      12CB8614D59C17D09517CE911DD39B1ED6A020DAA689EDD3FFFFFFFFFFFFFFFF
      FFFFFFFFB4D0B60065000574103C9C57009B3E05A95121B46130C0752DCD8E17
      D8A222DEAE26DEAD2DE3B632E8C046F0CE82F9E4FDFEFEFFFFFFFFFFFFFFFFFF
      93BE97006700006F05439A551A9A50009A4108A65410AD5E16B9760DCF9624E1
      B131E8BF3CEECB42F4D462FBE37CFEEDECFCFAFFFFFFFFFFFFFFFFFF70AA7600
      650000730D0D892E59B48066A76C87AD788AA86D76C49750D1AA1EDAAB30EBC1
      3FF1CE47F7D871FEEA78FFF0D0FDF7FFFFFFFFFFFFFFFFFF4F96550164000473
      10017E1B049032717719DD8D3DD57C107FB8775CCA927CD5B01FDEB431E4BF3D
      EACB60F1DB61F5E0AFF9EEFFFFFFFFFFFFFFFFFF30863A726000A4711FA77F2E
      B08832B88531CA9A44C68E16CFA454BF8B30D5A56693CFB466DBBF78DFC8B6ED
      E3C8F6EEEBFDFAFFFFFFFFFFFFF2F7F71C81239E6D04CD903FC68C18D6A324DA
      AE2BD2A321C99213CE8A0EC7800BBC5B00A2B07A43FEE055E3BFFFFCFDFFFFFF
      FFFFFFFFFFFFFFFFFFDCEAE10A760F92760ECD8C3DBF8114CB9C28D5AE37DCB3
      2ADDB52ACBBF58B2AF59ACA5607CDFBB2BF7D559DEBDFFFEFEFFFFFFFFFFFFFF
      FFFFFFFFFFC5DAC7006D020A780F3A934030A2532CAA5AB3913EE0AA3DD4B43E
      2ED8AC0DD3A215D5AA17CFA40CC09281DAC1FFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFA2C4A400630000710B00760F00801E00811EB68220D5992DB8A33C18B0752E
      B37E60C7A28DD8C0B6E7D8F6FCFAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF86AF88
      0045000058000D6A182C88435D974FC28724B27408C29E51EEF8F4FFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE3EAE3AAC2AAC7
      D8C7EAF0EAFFFFFFFFEAD5C18B2BA66D1BD0B286FFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFEFDF7F1E7F8F7F5FEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFF}
    ParentDoubleBuffered = False
    TabOrder = 10
    OnClick = bbtnOpen2Click
    OnKeyDown = FormKeyDown
  end
  object sgSheet2: TStringGrid
    Left = 8
    Top = 339
    Width = 363
    Height = 166
    DefaultDrawing = False
    FixedCols = 0
    FixedRows = 0
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goColSizing]
    TabOrder = 11
    OnClick = sgSheet1Click
    OnDrawCell = sgSheet1DrawCell
    OnKeyDown = FormKeyDown
    OnSelectCell = sgSheet2SelectCell
  end
  object sbTip: TStatusBar
    Left = 0
    Top = 517
    Width = 676
    Height = 24
    Color = clMoneyGreen
    Panels = <
      item
        Width = 150
      end
      item
        Alignment = taCenter
        BiDiMode = bdLeftToRight
        ParentBiDiMode = False
        Text = '  CopyRight By ZongQingHong    QQ:175624411'
        Width = 300
      end
      item
        Alignment = taCenter
        Text = #25353' F1 '#26597#30475#20351#29992#24110#21161
        Width = 50
      end>
    SizeGrip = False
  end
  object odFile1: TOpenDialog
    Left = 176
    Top = 152
  end
  object odFile2: TOpenDialog
    Left = 200
    Top = 384
  end
  object Timer1: TTimer
    OnTimer = Timer1Timer
    Left = 528
    Top = 56
  end
end

//格式设置单元文件

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CheckLst;

type
  TForm2 = class(TForm)
    chklist: TCheckListBox;
    cmbStyle: TComboBox;
    btnSet: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure chklistClickCheck(Sender: TObject);
    procedure cmbStyleChange(Sender: TObject);
    procedure btnSetClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure chklistClick(Sender: TObject);

  private
    { Private declarations }

  public
    { Public declarations }
    //Constructor Create(AOwner: TComponent; sFileName: string); reintroduce;
    Constructor Create(AOwner: TComponent; AxlBook: Variant); reintroduce;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses ComObj;

var
  arrCheck, arrStyle: array of Byte;  //复选框状态和设置风格数据
  tmpxlBook: Variant;
  rCount, cCount: Cardinal;

procedure TForm2.btnSetClick(Sender: TObject);
var
  i, j : integer;
begin
  for I := 0 to chkList.Items.Count - 1 do
  begin
   //如果选中了列,且设置格式为文本
    if (arrCheck[i] = 1) and (arrStyle[i] = 1) then
    begin
      for j := 2 to rCount do
        tmpxlBook.ActiveSheet.Cells[j, i+1].NumberFormatLocal := '000000';
    end;
  end;
  ShowMessage('格式设置完成');
  ModalResult := 1;
end;

procedure TForm2.chklistClick(Sender: TObject);
begin
  cmbStyle.ItemIndex := arrStyle[chkList.ItemIndex];
end;

procedure TForm2.chklistClickCheck(Sender: TObject);
begin
  arrCheck[chkList.ItemIndex] := Byte(chkList.State[chkList.ItemIndex]);
end;

procedure TForm2.cmbStyleChange(Sender: TObject);
begin
  if arrCheck[chkList.ItemIndex] = 1 then
     arrStyle[chkList.ItemIndex] := cmbStyle.ItemIndex;
end;

Constructor TForm2.Create(AOwner: TComponent; AxlBook: Variant);
var
  i: integer;
begin
  inherited Create(AOwner);

  tmpxlBook := AxlBook;

  cCount := tmpxlBook.ActiveSheet.UsedRange.Columns.Count;
  rCount := tmpxlBook.ActiveSheet.UsedRange.Rows.Count;

  //chkList.Clear;
  for I := 0 to cCount - 1 do
  begin
    chkList.Items.Add(tmpxlBook.ActiveSheet.Cells[2,I+1].Value);
  end;

  SetLength(arrCheck, cCount);
  SetLength(arrStyle, cCount);

  for i := Low(arrCheck) to High(arrCheck) do
    arrCheck[i] := Byte(chkList.State[i]);

  for i := Low(arrStyle) to High(arrStyle) do
    arrStyle[i] := 0;

  btnSet.Caption := '设置';
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  //tmpxlBook.Close;
  tmpxlBook := UnAssigned;
end;

end.

//格式设置窗体文件

object Form2: TForm2
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu, biMinimize]
  Caption = #26684#24335#35774#32622
  ClientHeight = 243
  ClientWidth = 346
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poMainFormCenter
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 11
    Width = 117
    Height = 13
    Caption = #36873#25321#35201#35774#32622#30340#23383#27573#21015
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object Label2: TLabel
    Left = 184
    Top = 13
    Width = 52
    Height = 13
    Caption = #36873#25321#26684#24335
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'Tahoma'
    Font.Style = [fsBold]
    ParentFont = False
  end
  object chklist: TCheckListBox
    Left = 8
    Top = 32
    Width = 161
    Height = 203
    OnClickCheck = chklistClickCheck
    ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
    ItemHeight = 13
    TabOrder = 0
    OnClick = chklistClick
  end
  object cmbStyle: TComboBox
    Left = 184
    Top = 32
    Width = 145
    Height = 21
    ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
    TabOrder = 1
    Text = #40664#35748
    OnChange = cmbStyleChange
    Items.Strings = (
      #40664#35748
      #25991#26412)
  end
  object btnSet: TButton
    Left = 216
    Top = 184
    Width = 89
    Height = 51
    Caption = 'btnSet'
    TabOrder = 2
    OnClick = btnSetClick
  end
end

//使用帮助单元文件

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm3 = class(TForm)
    lbHelp: TListBox;
    procedure lbHelpKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.lbHelpKeyPress(Sender: TObject; var Key: Char);
begin
  key := #0;
end;

end.

//使用帮助窗体文件

object Form3: TForm3
  Left = 0
  Top = 0
  BorderIcons = [biSystemMenu]
  Caption = #36719#20214#20351#29992#24110#21161
  ClientHeight = 347
  ClientWidth = 537
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poOwnerFormCenter
  PixelsPerInch = 96
  TextHeight = 13
  object lbHelp: TListBox
    Left = 0
    Top = 0
    Width = 537
    Height = 340
    BevelKind = bkFlat
    Color = clSkyBlue
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clBlack
    Font.Height = -16
    Font.Name = #21326#25991#26999#20307
    Font.Style = []
    ImeName = #20013#25991' ('#31616#20307') - '#19975#33021#20116#31508#20869#32622#36755#20837#27861
    IntegralHeight = True
    Items.Strings = (
      '    '#20449#24687#21270#26102#20195#65292#21150#20844#30005#23376#21270#24050#25104#20102#27599#20010#37096#38376#21333#20301#19981#21487#25110#32570#30340#32452#25104#37096#20998#65292
      #20854#20013#20063#20813#19981#20102#35201#32463#24120#20351#29992#24494#36719#30340'EXCEL'#30005#23376#34920#26684#65292#36890#36807#23545#30005#23376#34920#26684#30340#28789
      #27963#36816#29992#65292#26497#22823#26041#20415#21644#25552#39640#20102#21150#20844#25928#29575#12290
      '    '#22312#20855#20307#24037#20316#20013#65292#32463#24120#20250#30896#21040#20004#24352#30005#23376#34920#26684#30340#27604#23545#25110#21512#24182#38382#39064#12290#21363#20320#25152
      #38656#35201#30340#20449#24687#24448#24448#26159#22312#20004#24352#19981#21516#30340#34920#26684#20013#12290#22914#26524#35201#25163#24037#25226#20004#24352#34920#26684#30340#20869#23481
      #26681#25454#26576#20010#20851#38190#23383#27573#27604#23545#25110#21512#24182#22312#19968#36215#65292#23558#20250#28040#32791#24744#22823#37327#23453#36149#30340#26102#38388#21644#31934
      #21147#12290#20026#27492#65292#20316#32773#19987#38376#32534#20889#20102#19968#20010#36890#29992#30340'EXCEL'#28165#20876#27604#23545#21512#24182#36719#20214#65292#26469#25552
      #39640#25928#29575#65292#24182#25226#23427#20998#20139#32473#27599#19968#20301#38656#35201#30340#26379#21451#12290#22823#23478#22914#26524#22312#20351#29992#20013#21457#29616#38382#39064
      #25110#26377#26356#22909#30340#24314#35758#21487#20197#32852#31995'QQ'#65306'175624411'#65292'EMAIL'#65306'zjdyzqh@163.com'#65292
      #25105#23558#23613#21147#20104#20197#21560#32435#25913#21892#35813#36719#20214#31995#32479#12290
      #25805#20316#20351#29992#35828#26126#65306
      '    '#27493#39588'1'#65306#22312#34920#19968#65292#36873#25321#35201#25805#20316#30340'EXCEL'#25991#20214#65292#21518#36873#25321#20854#20013#30340#19968#24352#30005#23376#34920
      #20316#20026#34920#19968#12290
      '    '#27493#39588'2'#65306#22312#34920#20108#65292#36873#25321#35201#25805#20316#30340'EXCEL'#25991#20214#65292#21518#20877#36873#25321#30456#24212#30340#30005#23376#34920
      #20316#20026#34920#20108#12290
      '    '#27493#39588'3'#65306#23436#25104'1'#12289'2'#27493#21518#65292#28857#8220#33719#21462#25968#25454#8221#25353#38062#65292#21462#24471#20004#24352#30005#23376#34920#30340#25968#25454#12290
      '    '#27493#39588'4'#65306#36827#34892#21442#25968#35774#32622#65288#24517#39035#35774#32622#65289
      '    '#65288'1'#65289#33509#35201#35774#32622#36755#20986#30005#23376#34920#30340#26631#39064#65292#21017#21246#36873#8220#26631#39064#8221#22797#36873#26694#65292#21363#21487#36755#20837#12290
      '    '#65288'2'#65289#33509#35201#36755#20986#23383#27573#26174#31034#26631#31614#65292#21017#21246#36873#8220#23383#27573#26631#31614#8221#22797#36873#26694#21518#65292#36873#20013
      #8220#34920#19968' '#30340#23383#27573#21517#25152#22312#34892#65288#20219#24847#21015#65289#8221#36873#39033#65292#20877#28857#30005#23376#34920#19968#30340#23383#27573#25152#22312#34892#12290
      ' '#36873#20013#8220#34920#20108#30340#23383#27573#21517#25152#22312#34892#65288#20219#24847#21015#65289#8221#36873#39033#65292#20877#28857#34920#20108#30340#23383#27573#25152#22312#34892#12290
      ' '#35774#32622#23436#25104#21518#65292#19968#23450#35201#36873#20013#8220#35774#32622#23436#25104#8221#36873#39033#12290
      '    '#65288'3'#65289#21246#36873#8220#25968#25454#21306#22495#35774#32622#8221#22797#36873#26694#21518#65292#36873#20013#8220#34920#19968#30340#25968#25454#36215#22987#34892
      #65288#20851#38190#21015#65289#8221'  '#36873#39033#65292#20877#28857#20987#34920#19968#20013#20851#38190#21015#30340#31532#19968#34892#25968#25454#65307#20854#20182#36873#39033#35774#32622
      #21516#12290#35774#32622#23436#25104#21518#65292#19968#23450#35201#36873#20013#8220#25968#25454#35774#32622#23436#25104#8221#36873#39033#12290
      '    '#27493#39588'5'#65306
      '    '#65288'1'#65289#28857#20987#8220#27604#23545#21512#24182#8221#25353#38062#65292#25353#25552#31034#36755#20837#25991#20214#21517#21518#20445#23384#12290
      '    '#65288'2'#65289#25552#31034#26159#21542#35201#35774#32622#29983#25104#30340#32467#26524#34920#30340#26684#24335#12290#65371#28857#20987#8220#26159#8221#65292#26174#31034#35774#32622
      #31383#21475#12290' '#22914#26524#26377#23383#27573#26174#31034#21517#65292#20250#26174#31034#25152#26377#30340#23383#27573#65292#21487#26681#25454#38656#35201#35774#32622#21508#23383#27573
      #30340#25991#26412#23646#24615#65288#20165#25552#20379#26368#24120#29992#30340#25991#26412#26684#24335#26174#31034#25968#23383#21151#33021#65289#65307#22914#26524#26080#23383#27573#26174
      #31034#21517#65292#21017#26174#31034#31532#19968#34892#30340#21508#21015#25968#25454#20869#23481#65292#25805#20316#21516#21069#12290#65373)
    ParentFont = False
    TabOrder = 0
    OnKeyPress = lbHelpKeyPress
  end
end 
posted @ 2011-07-16 13:08  客栈老人  阅读(631)  评论(0编辑  收藏  举报