360ITO技术社区
  • 首页
  • 文章
  • 快讯
  • 讨论
  • 问答
  • 小贴士
  • 代码块
  • 开源
  • 老论坛
登录 | 注册

360ITO技术社区  > 文章

订阅文章

WORD转Sqlite数据库开发手记(3)

smallghost 发布于 8年前 ( comment 0条评论  查看:5199  收藏:0 )

通过前两张(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);
2、初始化SQLITE数据库并创建数据表

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;
3、开始转换数据

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 有用 0 无用
本站原创,欢迎转载;尊重他人劳动,转载时保留以下信息:
本文转自:360ITO技术社区
原文标题:WORD转Sqlite数据库开发手记(3)
原文地址:http://www.360ito.com/article/91.html
360ito.com
delphi

共有0个评论 我要评论»

按时间排 按有用数排 按支持数排

网友回复/评论仅代表其个人看法,并不表明本社区同意其观点或证实其描述。

请尽量让自己的回复能够对别人有帮助

1.不欢迎无意义的回复/评论和类似“顶”、“沙发”之类没有营养的文字
如果只是想简单的表个态,请点 有用无用支持反对 等按钮
2.发言之前请再仔细看一遍文章,或许是您遗漏、误解了,理性讨论、切莫乱喷
3.严禁发布违法、违规的信息,请勿到处招贴广告、发布软文;
4.如果您发现自己的回复/评论不见了,请参考以上3条
5.不停制造违规、垃圾信息的,账户将被禁止

热门标签

  • android 20
  • Flash 15
  • 游戏 12
  • Linux 12
  • Python 11
  • 工作笔记 11
  • 社交游戏 7
  • delphi 5
  • jquery 5
  • 编程 4
  • 谷歌 4
  • git 4
  • Centos 4
  • JavaScript 3
  • 开发者 3
  • C/C++ 3
  • 安全 2
  • 代码 2
  • 浏览器 2
  • 移动应用 2

相关文章

周热点

月热点

Copyright ©2011-2012 360ITO技术社区 All Rights Reserved. | 关于 | 联系我们 | 杭州精创信息技术有限公司 浙ICP备09019653号-26|
▲