XLSReadWriteII4 自用.

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExcelXP, OleServer, ExtCtrls,ComObj, jpeg,
  WinSkinData,XLSReadWriteII4;

 

复制 XLSReadWriteII4 源文件放入 项目

 

 

procedure TForm8.btn1Click(Sender: TObject);
var
  topen : TOpenDialog;
  exfile :string;
  ExcelApp:Variant;
  wcount,i : Integer;
  XLS: TXLSReadWriteII4;
  tslist : TStringList;
begin
  InitializeCriticalSection(CS);
  topen :=  TOpenDialog.Create(self);
  tslist :=   TStringList.Create;
  topen.Filter := 'excel文件|*.xls';
  if topen.Execute then
     exfile := topen.FileName;
  if not FileExists(exfile) then
  begin
    ShowMessage('文件加载失败.');
    Exit;
  end;
  edt1.Text :=  exfile;
  XLS := TXLSReadWriteII4.Create(nil);
  XLS.Filename :=  exfile;
  XLS.Read;
  cbb1.Clear;
  if XLS.Sheets.Count = 0 then
  begin
    showmessage('此为空excel,没有sheet文档.');
    exit;
  end;
  wcount := XLS.Sheets.Count-1;

  for i:=0 to wcount do
   begin
     cbb1.Items.Add(XLS.Sheet[i].Name);
   end;
   cbb1.ItemIndex := 0;
   grsentselxls.selectxlsname :=  exfile;
   grsentselxls.selectwheet := cbb1.Text;
   XLS.Clear;
  { 旧版本
  try
    ExcelApp:=CreateOleObject('Excel.Application');
    except
      ShowMessage('加载excel失败.');
      Exit;
  end;
   ExcelApp.workBooks.Open(exfile);
   wcount := ExcelApp.Worksheets.count;
   for i:=1 to wcount do
   begin
     cbb1.Items.Add(ExcelApp.Worksheets[i].name)
   end;   
   cbb1.SelText := ExcelApp.Worksheets[1].name;
   grsentselxls.selectxlsname :=  exfile;
   grsentselxls.selectwheet := cbb1.Text;
   ExcelApp.WorkBooks.Close; //关闭工作簿
   ExcelApp.Quit; //退出 Excel
   ExcelApp:=Unassigned;//释放excel进程
   }
end;

procedure TForm8.btn2Click(Sender: TObject);
var
  tdth :Tdoingth;
  th2 : Tdoingthaa;
begin
   if (edt1.Text = '')  and  (cbb1.text = '')  then
   begin
     ShowMessage('文件名选择不能为空.');
     exit;
   end;
   Self.Hide;
   importexcel := Timportexcel.Create(Self);
   importexcel.Show;
   importexcel.loadexcel(grsentselxls.selectxlsname);
   grsentselxls.selectwheetint :=  cbb1.ItemIndex;
   //弹出导出小窗口
   //tdth := Tdoingth.Create(false);
   th2 := Tdoingthaa.Create(False);
end;

procedure TForm8.cbb1Change(Sender: TObject);
begin
  grsentselxls.selectwheet := cbb1.Text;
end;


  

//加载excel
procedure Timportexcel.loadexcel(excelname:String);
var
   cols,rows,i,Maxc,Maxr : Integer;
   ExcelApp:Variant;
   cstr,rstr :string;
   todth1 : Tdoingth;
   XLS: TXLSReadWriteII4;
begin
    InitializeCriticalSection(CS);

    Unit3.importexcel.loadstatus := True;
    CoInitialize(nil);
    XLS := TXLSReadWriteII4.Create(nil);
    try
      XLS.Filename := excelname;
      XLS.Read;
    except
      ShowMessage('加载excel失败.');
      Exit;
    end;
    if not FileExists(excelname) then
    begin
      ShowMessage('文件加载失败.');
      Exit;
    end;
    lbl1.Caption := '导入中,请稍等...';
     Maxc := XLS.Sheet[grsentselxls.selectwheetint].LastCol; //列数
     Maxr :=XLS.Sheet[grsentselxls.selectwheetint].LastRow;   //  行数
     strngrd1.RowCount :=  Maxr+1;
     strngrd1.ColCount := Maxc+1;
     for rows :=1 to  Maxr do     //
     begin
       if  Unit3.importexcel.loadstatus = False then
          Break;
       strngrd1.Cells[0,rows] := IntToStr(rows);
       for cols := 1 to  Maxc do
       begin
         strngrd1.Cells[cols,rows] := XLS.Sheet[grsentselxls.selectwheetint].AsString[cols,rows];
       end;
     end;
     if  Unit3.importexcel.loadstatus = True then
       lbl1.Caption := '已导入,点击号码列点击鼠标右键导入即可.'
     else
       lbl1.Caption := '导入已被终止,点击号码列点击鼠标右键导入即可.' ;

     lbl3.Caption := IntToStr(Maxr)+'';
     lbl4.Caption := IntToStr(Maxc)+'';
    XLS.Clear;
    {
    try
    ExcelApp:=CreateOleObject('Excel.Application');
    except
      ShowMessage('加载excel失败.');
      Exit;
    end;
    if not FileExists(excelname) then
    begin
      ShowMessage('文件加载失败.');
      Exit;
    end;
     ExcelApp.workBooks.Open(excelname); //打开已存在工作簿
     ExcelApp.Worksheets[grsentselxls.selectwheet].activate;
     lbl1.Caption := '导入中,请稍等...';
     Maxc :=ExcelApp.WorkSheets[grsentselxls.selectwheet].UsedRange.Columns.Count; //列数
     Maxr :=ExcelApp.WorkSheets[grsentselxls.selectwheet].UsedRange.Rows.Count;  //  行数
     strngrd1.RowCount :=  Maxr+1;
     strngrd1.ColCount := Maxc+1;
     for rows :=1 to  Maxr do     //
     begin
       if  Unit3.importexcel.loadstatus = False then
          Break;
       strngrd1.Cells[0,rows] := IntToStr(rows);
       for cols := 1 to  Maxc do
       begin
         strngrd1.Cells[cols,rows] := ExcelApp.Cells[rows,cols].Value;
       end;
     end;
     ExcelApp.WorkBooks.Close; //关闭工作簿
     ExcelApp.Quit; //退出 Excel    
     ExcelApp:=Unassigned;//释放excel进程
     if  Unit3.importexcel.loadstatus = True then
       lbl1.Caption := '已导入,点击号码列点击鼠标右键导入即可.'
     else
       lbl1.Caption := '导入已被终止,点击号码列点击鼠标右键导入即可.' ;
      }


      CoUnInitialize;
 end;

//远中当列
procedure Timportexcel.coselect(Sender: TObject);
var
  myRect,myRect1: TGridRect;
  t : TRect;
begin
    myRect := strngrd1.Selection;
    myRect1.Left := myRect.Left;
    myRect1.Top := 1;
    myRect1.Bottom :=strngrd1.RowCount;
    myRect1.Right :=myRect.Left;
    //strngrd1.Selection := myRect1;     不用全选 中
end;

 

posted @ 2014-04-09 15:25  hanks  阅读(537)  评论(0)    收藏  举报