unit CWord;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, LogFile, StdCtrls, ComCtrls, ExtCtrls, CEdit, Buttons,
word2000, COMobj, Office2000, OleServer, filectrl, cvcode;
type
TForm1 = class(TForm)
Open: TButton;
AQ: TADOQuery;
DataSource1: TDataSource;
AQBANK_CODE: TStringField;
AQBANK_NAME: TStringField;
OpenDialog1: TOpenDialog;
Memo1: TMemo;
procedure CreateClick(Sender: TObject);
procedure OpenClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CreateClick(Sender: TObject);
var
WWord: Variant; //word module
WDocument: Variant; //word document
begin
try
WWord := GetActiveOLEObject('Word.Application');
except
on E: Exception do
try WWord := CreateOLEObject('Word.Application');
except
ShowMessage('Create Fail');
raise;
abort;
end;
end;
WDocument := WWord.Documents.add(NewTemplate := False);
WWord.Visible := True;
end;
procedure TForm1.OpenClick(Sender: TObject);
var
WWord: OLEVariant;
WDocument: OLEVariant;
WTable: OLEVariant;
strTemp: string;
iCol: Integer;
iRow: Integer;
strCode: string;
strName: string;
begin
try
WWord := GetActiveOLEObject('Word.Application');
except
on E: Exception do
try WWord := CreateOLEObject('Word.Application');
except
ShowMessage('Open Fail');
raise;
abort;
end;
end;
strTemp := 'E:/0.doc'; //DOC的路径,随便设置的
WWord.Documents.open(strTemp);
WDocument := WWord.Documents.Item(1);
WTable := WDocument.Tables.Item(1);
with AQ do
begin
Close;
SQL.Clear;
sql.Add(Memo1.Text);
end;
aq.open;
aq.Close;
for iCol := 1 to WTable.Columns.Count do
begin
for iRow := 2 to WTable.Rows.Count do
begin
begin
if not iCol mod 2 = 0 then
begin
AQ.Close;
Aq.SQL.Clear;
if not Wtable.Cell(iRow, iCol).Range.Text = '' then
begin
aq.SQL.Add('INSERT INTO BANK(BANK_CODE, BANK_NAME) VALUES');
AQ.SQL.Add(',' + QuotedStr(WTable.cell(iRow, iCol).Range.Text));
AQ.SQL.Add(',' + QuotedStr(WTable.cell(iRow, iCol + 1).Range.Text) + ')' + #13#10);
aq.ExecSQL;
end;
end;
end;
end;
end;
ShowMessage('Success!');
AQ.Close;
end;
end.