通过前两张(1、2)的介绍,已经大致了解了整个转换的流程,接下来我们开始实际编码。Sqlite我是用了ASqlite3Components控件,网上可以找到。另外在使用Sqlite数据库前需要准备一个Sqlite动态库。去http://www.sqlite.org/download.html可以下载到。
下面开始一步步完成整个转换工具:
1、打开WORD文件
try
FWordApp := CreateOleObject('Word.Application');
FWordApp.Visible := False;
except
on e:Exception do
begin
ShowMessage('Word启动失败!错误原因:'+e.Message);
exit;
end;
end;
FWordApp.Documents.Open(FWordFileNameEdit.Text);
function TFormMain.OnInitSqlitDB(arg_file:String;arg_tableName:String):Boolean;
var
count:Integer;
begin
Result := False;
try
// 初始化数据库
FASQLite3DB.DriverDLL := g_AppPath + 'sqlite.dll';
FASQLite3DB.Database := arg_file;
FASQLite3DB.Connected := true;
// 如果需要的数据库表不存在就创建新的
FTmpASQLite3Query.SQL.Text := Format('SELECT COUNT(*) AS i_count FROM [sqlite_master] WHERE [name] = %s;',[QuotedStr(arg_tableName)]);
FTmpASQLite3Query.Active := True;
count := FTmpASQLite3Query.Fields[0].AsInteger;
FTmpASQLite3Query.Active := False;
if (count = 0) then
begin
FTmpASQLite3Query.SQL.Text := Format('create table %s (' +
'_id INTEGER ,' +
'cname nvarchar,' +
'ename nvarchar,' +
'description text);',[arg_tableName]);
FTmpASQLite3Query.ExecSQL;
end
else
begin
FTmpASQLite3Query.SQL.Text := Format('delete from %s;',[arg_tableName]);
FTmpASQLite3Query.ExecSQL;
end;
except
on e:Exception do
begin
MessageBox(Handle,e.Message,'异常:',MB_OK + MB_ICONERROR);
exit;
end;
end;
Result := True;
end;
procedure TFormMain.OnStartConver(sender:TObject);
var
count:integer;
rowCount:Integer;
ename:string;
cname:string;
description:string;
table:Variant;
sql:String;
begin
// 计算进度
OnSetWaitHintInfo('准备开始转换...');
table := FWordApp.ActiveDocument.tables.item(1);
rowCount := table.rows.count;
OnViewProgressBar(rowCount);
FASQLite3DB.StartTransaction;
try
for count := 1 to rowCount do
begin
OnSetWaitHintInfo(Format('%d/%d',[count,rowCount]));
OnSetWaitStep(count);
cname := table.Cell(count,1).Range.Text;
Delete(cname,Length(cname) - 1,2);
ename := table.Cell(count,2).Range.Text;
Delete(ename,Length(ename) - 1,2);
description := table.Cell(count,3).Range.Text;
Delete(description,Length(description) - 1,2);
sql := Format('insert into %s (_id,cname,ename,description) values(%d,%s,%s,%s);',[FTableName,count,QuotedStr(cname),QuotedStr(ename),QuotedStr(description)]);
FTmpASQLite3Query.SQL.Text := sql;
FTmpASQLite3Query.ExecSQL;
end;
FASQLite3DB.Commit;
finally
if FASQLite3DB.InTransaction then
begin
FASQLite3DB.RollBack;
end;
HideWaitFrom;
FWordApp.Quit;
FASQLite3DB.Connected := False;
end;
end;
转换后的结果:
有需要源码的童鞋留下邮箱!
共有0个评论 我要评论»
网友回复/评论仅代表其个人看法,并不表明本社区同意其观点或证实其描述。
1.不欢迎无意义的回复/评论和类似“顶”、“沙发”之类没有营养的文字
如果只是想简单的表个态,请点 有用无用支持反对 等按钮
2.发言之前请再仔细看一遍文章,或许是您遗漏、误解了,理性讨论、切莫乱喷
3.严禁发布违法、违规的信息,请勿到处招贴广告、发布软文;
4.如果您发现自己的回复/评论不见了,请参考以上3条
5.不停制造违规、垃圾信息的,账户将被禁止