E
Ekin
Unregistered / Unconfirmed
GUEST, unregistred user!
线程操作EXCEL导入的问题,下面代码是将EXCEL表的数据导入到ClientDataset里的代码,不用线程直接导入没问题,但记录多的时候速度有点不理想且有“假死”的现象,所以改成现在的多线程,以下代码可以运行起来,也可以导入数据,如果将CDS的数据写到界面的GRID控件的时候就会报错“Canvasdo
es not allow drawing”和内存地址错误;程序关闭退出后貌似操作过的EXCEL还在任务管理器的进程列表中,本人对线程序不太了解,那位大哥给看看问题出在那里。unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGrid, StdCtrls, DBClient,ActiveX,ComObj, RzStatus;type TForm1 = class(TForm) ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
Button1: TButton;
ClientDataSet1id: TStringField;
ClientDataSet1des: TStringField;
procedure Button1Click(Sender: TObject);
private { Private declarations } public { Public declarations } end;
type TMyThread = class(TThread) protected procedure Execute;
override;
end;
var Form1: TForm1;implementation{$R *.dfm}{ TMyThread }procedure TMyThread.Execute;var ExcelApp:variant;
I:Integer;
begin
with TForm1(Screen.ActiveForm)do
begin
CoInitialize(nil);
FreeOnTerminate := True;
ExcelApp:=CreateOleObject('Excel.Application');
ExcelApp.visible:=false;
ExcelApp.WorkBooks.open('E:/TEST.XLS');
ExcelApp.Worksheets[1].activate;
if ExcelApp.activesheet.usedRange.Rows.count<2 then
begin
showmessage('选定的Excel文件中没有内容');
exit;
end else
begin
ClientDataSet1.Close;
ClientDataSet1.CreateDataSet;
screen.Cursor:=crSqlWait;
try with ClientDataSet1do
begin
for i:=2 to ExcelApp.activesheet.UsedRange.Rows.Count+1do
begin
Append;
FieldByName('ID').Value :=trim(ExcelApp.activesheet.Cells[i,1].value);
FieldByName('DES').Value :=trim(ExcelApp.activesheet.Cells[i,2].value);
end;
end;
except on E:Exception do
begin
Showmessage(E.Message);
screen.Cursor:=crdefault;
Exit;
end;
end;
end;
ExcelApp.WorkBooks.close;
ExcelApp.Quit;
ExcelApp:=Unassigned;
screen.Cursor:=crdefault;
CoUninitialize;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMyThread.Create(False);
end;
end.
es not allow drawing”和内存地址错误;程序关闭退出后貌似操作过的EXCEL还在任务管理器的进程列表中,本人对线程序不太了解,那位大哥给看看问题出在那里。unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGrid, StdCtrls, DBClient,ActiveX,ComObj, RzStatus;type TForm1 = class(TForm) ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
Button1: TButton;
ClientDataSet1id: TStringField;
ClientDataSet1des: TStringField;
procedure Button1Click(Sender: TObject);
private { Private declarations } public { Public declarations } end;
type TMyThread = class(TThread) protected procedure Execute;
override;
end;
var Form1: TForm1;implementation{$R *.dfm}{ TMyThread }procedure TMyThread.Execute;var ExcelApp:variant;
I:Integer;
begin
with TForm1(Screen.ActiveForm)do
begin
CoInitialize(nil);
FreeOnTerminate := True;
ExcelApp:=CreateOleObject('Excel.Application');
ExcelApp.visible:=false;
ExcelApp.WorkBooks.open('E:/TEST.XLS');
ExcelApp.Worksheets[1].activate;
if ExcelApp.activesheet.usedRange.Rows.count<2 then
begin
showmessage('选定的Excel文件中没有内容');
exit;
end else
begin
ClientDataSet1.Close;
ClientDataSet1.CreateDataSet;
screen.Cursor:=crSqlWait;
try with ClientDataSet1do
begin
for i:=2 to ExcelApp.activesheet.UsedRange.Rows.Count+1do
begin
Append;
FieldByName('ID').Value :=trim(ExcelApp.activesheet.Cells[i,1].value);
FieldByName('DES').Value :=trim(ExcelApp.activesheet.Cells[i,2].value);
end;
end;
except on E:Exception do
begin
Showmessage(E.Message);
screen.Cursor:=crdefault;
Exit;
end;
end;
end;
ExcelApp.WorkBooks.close;
ExcelApp.Quit;
ExcelApp:=Unassigned;
screen.Cursor:=crdefault;
CoUninitialize;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMyThread.Create(False);
end;
end.