unit ImportFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, JvExControls, JvComponent, JvWizard, StdCtrls, Buttons, ExtCtrls,
ActiveX, Grids, JvExGrids, JvStringGrid, ComObj, JvProgressBar,
JvComponentBase, JvThread, EasyListView;
type
TFrmImport = class(TForm)
JvWizard: TJvWizard;
PgSelectFile: TJvWizardInteriorPage;
EdtFileName: TLabeledEdit;
BtnBrowse: TBitBtn;
DlgOpen: TOpenDialog;
PgPreview: TJvWizardInteriorPage;
Label1: TLabel;
CmbSheets: TComboBox;
ChkHeader: TCheckBox;
JSgView: TJvStringGrid;
PgSelectTelCol: TJvWizardInteriorPage;
Label2: TLabel;
CmbSimpleTel: TComboBox;
PgProgress: TJvWizardInteriorPage;
BtnCancel: TBitBtn;
PbProgress: TJvGradientProgressBar;
LblPerCount: TLabel;
LblTotal: TLabel;
LblCurr: TLabel;
LblErr: TLabel;
LblRep: TLabel;
ImportThread: TJvThread;
procedure BtnBrowseClick(Sender: TObject);
procedure PgSelectFileNextButtonClick(Sender: TObject;
var Stop: Boolean);
procedure CmbSheetsChange(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure PgPreviewNextButtonClick(Sender: TObject; var Stop: Boolean);
procedure PgSelectTelColNextButtonClick(Sender: TObject;
var Stop: Boolean);
procedure ImportThreadExecute(Sender: TObject; Params: Pointer);
procedure ImportThreadFinish(Sender: TObject);
procedure BtnCancelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FParams: Pointer;
Excel, Books: OleVariant;
FTotal, FCurr, FPer, FErr, FRep: Integer;
procedure UpdateDisplay;
class procedure Execute(Params: Pointer);
end;
var
FrmImport: TFrmImport;
implementation
uses SendFrm;
{$R *.dfm}
{ TFrmImport }
class procedure TFrmImport.Execute(Params: Pointer);
begin
with TFrmImport.Create(Application) do
try
FParams := Params;
ShowModal;
finally
Free;
end;
end;
procedure TFrmImport.BtnBrowseClick(Sender: TObject);
begin
if not DlgOpen.Execute then
Exit;
EdtFileName.Text := DlgOpen.FileName;
end;
procedure TFrmImport.PgSelectFileNextButtonClick(Sender: TObject;
var Stop: Boolean);
var
i: Integer;
begin
Stop := EdtFileName.Text = '';
if Stop then Exit;
try
if VarIsEmpty(Excel) then
Excel := CreateOleObject('Excel.Application');
if VarIsEmpty(Books) then
Books := CreateOleObject('Excel.Sheet');
except
MessageBox(Handle, '请检查是否安装Excel', '错误', MB_ICONERROR + MB_OK);
Exit;
end;
CmbSheets.Clear;
Books := Excel.WorkBooks.Open(EdtFileName.Text);
for i := 1 to Books.Sheets.Count do
CmbSheets.Items.Add(Books.Sheets[i].Name);
CmbSheets.ItemIndex := 0;
CmbSheetsChange(nil);
end;
procedure TFrmImport.CmbSheetsChange(Sender: TObject);
var
Row, Col: Integer;
begin
Books.Sheets[CmbSheets.Text].Activate;
JSgView.ColCount := Books.ActiveSheet.UsedRange.Columns.Count;
for Col := 1 to JSgView.ColCount do
begin
if ChkHeader.Checked then
JSgView.Cells[Col-1 , 0] := Books.ActiveSheet.Cells[1, Col]
else
JSgView.Cells[Col-1, 0] := Format('F%d', [Col]);
end;
for Row := 1 to JSgView.RowCount do
for Col := 1 to JSgView.ColCount do
begin
if ChkHeader.Checked then
JSgView.Cells[Col-1, Row] := Books.ActiveSheet.Cells[Row+1, Col]
else
JSgView.Cells[Col-1, Row] := Books.ActiveSheet.Cells[Row, Col];
end;
end;
procedure TFrmImport.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not VarIsEmpty(Books) then
Books.Close;
if not VarIsEmpty(Excel) then
Excel.Quit;
Books := Unassigned;
Excel := Unassigned;
end;
procedure TFrmImport.PgPreviewNextButtonClick(Sender: TObject;
var Stop: Boolean);
var
Col: Integer;
begin
for Col := 1 to Books.ActiveSheet.UsedRange.Columns.Count do
if ChkHeader.Checked then
CmbSimpleTel.Items.Add(Books.ActiveSheet.Cells[1, Col])
else
CmbSimpleTel.Items.Add(Format('F%d', [Col]));
CmbSimpleTel.ItemIndex := 0;
end;
procedure TFrmImport.PgSelectTelColNextButtonClick(Sender: TObject;
var Stop: Boolean);
begin
BtnCancel.Visible := True;
FPer := 0;
if ChkHeader.Checked then
FTotal := Books.ActiveSheet.UsedRange.Rows.Count - 1
else
FTotal := Books.ActiveSheet.UsedRange.Rows.Count;
FCurr := 0;
FErr := 0;
FRep := 0;
UpdateDisplay;
ImportThread.Execute(Self);
end;
procedure TFrmImport.UpdateDisplay;
begin
PbProgress.Position := FPer;
LblPerCount.Caption := Format('已完成 %d%%', [FPer]);
LblTotal.Caption := Format('共计导入: %d', [FTotal]);
LblCurr.Caption := Format('当前导入: %d', [FCurr]);
LblErr.Caption := Format('错误数据: %d', [FErr]);
LblRep.Caption := Format('重复数据: %d', [FRep]);
end;
procedure TFrmImport.ImportThreadExecute(Sender: TObject; Params: Pointer);
var
E, B: OleVariant;
Col, Row: Integer;
F: TFrmSend;
S: string;
Item: TEasyItem;
begin
CoInitialize(nil);
E := CreateOleObject('Excel.Application');
B := CreateOleObject('Excel.Sheet');
B := E.WorkBooks.Open(TFrmImport(Params).EdtFileName.Text);
B.Sheets[TFrmImport(Params).CmbSheets.Text].Activate;
F := TFrmSend(TFrmImport(Params).FParams);
Col := TFrmImport(Params).CmbSimpleTel.ItemIndex + 1;
for Row := 1 to TFrmImport(Params).FTotal do
begin
if TFrmImport(Params).ChkHeader.Checked then
S := B.ActiveSheet.Cells[Row+1, Col]
else
S := B.ActiveSheet.Cells[Row, Col];
if F.IsMobile(S) then
Inc(F.FMobileCount)
else if F.IsUnicom(S) then
Inc(F.FUnicomCount)
else if F.IsTelecom(S) then
Inc(F.FTelecomCount)
else begin
Inc(TFrmImport(Params).FCurr);
Inc(TFrmImport(Params).FErr);
TFrmImport(Params).FPer := Trunc((TFrmImport(Params).FCurr/TFrmImport(Params).FTotal)*100);
ImportThread.Synchronize(TFrmImport(Params).UpdateDisplay);
Continue;
end;
if F.CheckRepeat(S) then
begin
Inc(TFrmImport(Params).FCurr);
Inc(TFrmImport(Params).FRep);
TFrmImport(Params).FPer := Trunc((TFrmImport(Params).FCurr/TFrmImport(Params).FTotal)*100);
ImportThread.Synchronize(TFrmImport(Params).UpdateDisplay);
Continue;
end;
Item := F.ELVCache.Items.Add;
Item.Caption := S;
Item.ImageIndex := 0;
F.FCacheHash.Add(S, Item);
Inc(TFrmImport(Params).FCurr);
TFrmImport(Params).FPer := Trunc((TFrmImport(Params).FCurr/TFrmImport(Params).FTotal)*100);
ImportThread.Synchronize(TFrmImport(Params).UpdateDisplay);
if ImportThread.Terminated then
Break;
end;
if not VarIsEmpty(B) then
B.Close;
if not VarIsEmpty(E) then
E.Quit;
B := Unassigned;
E := Unassigned;
CoUninitialize;
end;
procedure TFrmImport.ImportThreadFinish(Sender: TObject);
begin
BtnCancel.Visible := False;
PgProgress.EnableButton(bkFinish, True);
PgProgress.Title.Text := '导入完成';
PgProgress.Subtitle.Text := '数据导入完成,点击<完成>关闭向导';
end;
procedure TFrmImport.BtnCancelClick(Sender: TObject);
begin
if not ImportThread.Terminated then
ImportThread.Terminate
else
Application.ProcessMessages;
end;
initialization
CoInitialize(nil);
finalization
CoUninitialize;
end.