当前位置: 首页 > news >正文

做废铝的关注哪个网站好长春刚刚最新消息今天

做废铝的关注哪个网站好,长春刚刚最新消息今天,wordpress 企业站点,一个专门做视频配音的网站目录 功能需求 Office 数据源的一些映射关系 范例运行环境 配置Office DCOM 关键代码 组件库引入 ​核心代码 杀掉进程 总结 功能需求 在应用项目里,多数情况下我们会遇到导入 Excel 文件数据到数据库的功能需求,但某些情况下,也存…

目录

功能需求

Office 数据源的一些映射关系

范例运行环境

配置Office DCOM

关键代码

组件库引入

​核心代码

杀掉进程

总结


功能需求

在应用项目里,多数情况下我们会遇到导入 Excel 文件数据到数据库的功能需求,但某些情况下,也存在使用 Word 进行表格数据编辑的情况。Word 和 Excel 其实各有特点,用户的习惯不同,即使同一数据源,可能提供的数据源文件类型也不同,这其中也包括导入Word内容的功能,比如表格数据导出到DataSet数据集。

Office 数据源的一些映射关系

下图是一个简单的 Office 数据源的映射关系:

1、第一层级比如 WORD / EXCEL 为应用层级(Application)、 DATASET / DATABASE 为数据容器

2、第二层级,比如WORD 包含一个文档对象(Docment)、Excel 包含一个工作簿对象(WorkBook)、DataSet / DataBase 包括一组数据表对象(Tables)

3、第三层级,比如Word里的表格对象(Table)、Excel里的工作表对象(Sheet)

最实际的工作任务,是要将Table或Sheet对象的二维数据对应导出生成到 DataSet 里的 Table 对象,如果有多个则生成对应的集合。最后我们可能会再次导出到 DataBase 的数据表集合里(Tables)。

范例运行环境

操作系统: Windows Server 2019 DataCenter

操作系统上安装 Office Word 2016

.net版本: .netFramework4.7.1 或以上

开发工具:VS2019  C#

配置Office DCOM

对于安装原生Office应用,我们需要对DCOM进行进一步的配置方可使用其API。

打开控制面板、管理工具、组件服务:

点击组件服务、计算机、我的电脑、DCOM配置 

 找到 Microsoft Word97-2003 文档应用程序

 选择属性、打开标识选项卡、选择下列用户选项,设置启动Word应用的用户,点确定即可。

 理论上设置到这里就可以了,但以防万一,可以继续设置启动权限,选择安全选项卡、启动和激活权限,如下图:

关键代码

组件库引入

核心代码

public DataSet WordAsDataSet(string _filename) 方法,传入要读取的 WORD 文件路径即可,方法会遍历该WORD里的TABLES对象集合,如果找到TABLE对象,则按列的顺序创建字段列,比如F1、F2...Fn,以些类推,从第二行起为记录行,则根据创建的结构写入到 DataTable中。

        public DataSet WordAsDataSet(string _filename){DataSet ds = new DataSet();Object Nothing = System.Reflection.Missing.Value;object filename = _filename;//创建一个名为WordApp的组件对象DateTime beforetime = DateTime.Now;Word.Application WordApp = new Word.Application();//创建一个名为WordDoc的文档对象WordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);WordDoc.SpellingChecked = false;//关闭拼写检查WordDoc.ShowSpellingErrors = false;//关闭显示拼写错误提示框DateTime aftertime = DateTime.Now;
//遍历所有的Word里的表格,并写到数据集的TABLES集合里foreach (Word.Table wTable in WordDoc.Tables){System.Data.DataTable dt = new System.Data.DataTable();for (int colPos = 1; colPos <= wTable.Columns.Count; colPos++){DataColumn dc = new DataColumn();dc.ColumnName = "F" + colPos.ToString();dt.Columns.Add(dc);}for (int rowPos = 1; rowPos <= wTable.Rows.Count; rowPos++){DataRow drNew = dt.NewRow();int columnIndex = 0;foreach (Word.Cell cellObj in wTable.Rows[rowPos].Cells){drNew[columnIndex] = cellObj.Range.Text.Remove(cellObj.Range.Text.Length - 2, 2);//remove \r\acolumnIndex++;}dt.Rows.Add(drNew);}ds.Tables.Add(dt);}WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);//关闭WordApp组件对象WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);KillProcessByStartTime("WINWORD", beforetime, aftertime);return ds;}

杀掉进程

这是一个无奈之举,尝试了一些方法,但某些情况下仍然无法释放掉 Word 应用进程,因此根据时间点范围写了一个强制杀掉进程的方法。

示例代码如下:

public string KillProcessByStartTime(string processName,DateTime beforetime,DateTime aftertime){Process[] ps = Process.GetProcesses();foreach (Process p in ps)  {if(p.ProcessName.ToUpper()!=processName) continue;if(p.StartTime > beforetime && p.StartTime < aftertime){try{p.Kill();}catch(Exception e){return e.Message;}}}  return "";}

总结

在实际的应用中,无论是导入的文件格式还是导出的数据源,都是要结合客户的需求进行的。

在功能实现前,需要约定模板文件的格式,字段内容的意义、长度等。导入到 DataSet 成功后,再根据业务逻辑进行后续操作再加工,或直接导入到规范的数据表里(如 MS SQL SERVER)。

这些代码我们提供了一些操作WORD相关的关键方法,这里仅作参考,欢迎大家评论指教!


文章转载自:
http://synchronic.c7622.cn
http://unrepented.c7622.cn
http://unorganized.c7622.cn
http://zoot.c7622.cn
http://jalap.c7622.cn
http://undersecretary.c7622.cn
http://laryngitis.c7622.cn
http://underdrain.c7622.cn
http://gratingly.c7622.cn
http://contracyclical.c7622.cn
http://theriomorphous.c7622.cn
http://kerb.c7622.cn
http://longe.c7622.cn
http://unanswered.c7622.cn
http://thaumaturge.c7622.cn
http://squeezability.c7622.cn
http://tractorman.c7622.cn
http://exophthalmia.c7622.cn
http://shutt.c7622.cn
http://headstall.c7622.cn
http://throughway.c7622.cn
http://loudhailer.c7622.cn
http://unseduced.c7622.cn
http://flying.c7622.cn
http://link.c7622.cn
http://bacteriostasis.c7622.cn
http://adumbrative.c7622.cn
http://dandy.c7622.cn
http://dichotomise.c7622.cn
http://unperishing.c7622.cn
http://signorina.c7622.cn
http://reunion.c7622.cn
http://barelegged.c7622.cn
http://canaster.c7622.cn
http://straightaway.c7622.cn
http://apostatize.c7622.cn
http://nonstative.c7622.cn
http://instrumentally.c7622.cn
http://shockheaded.c7622.cn
http://sialon.c7622.cn
http://rebab.c7622.cn
http://flightworthy.c7622.cn
http://thingumbob.c7622.cn
http://subassembly.c7622.cn
http://untrammeled.c7622.cn
http://stalagmometer.c7622.cn
http://zoa.c7622.cn
http://kennan.c7622.cn
http://telefoto.c7622.cn
http://prelatical.c7622.cn
http://hydrodynamic.c7622.cn
http://rump.c7622.cn
http://coachwhip.c7622.cn
http://gooseherd.c7622.cn
http://harbour.c7622.cn
http://reinsure.c7622.cn
http://haematopoietic.c7622.cn
http://cervine.c7622.cn
http://sloppy.c7622.cn
http://ofris.c7622.cn
http://dandle.c7622.cn
http://hallo.c7622.cn
http://actualism.c7622.cn
http://incipit.c7622.cn
http://hopefully.c7622.cn
http://gripe.c7622.cn
http://agalwood.c7622.cn
http://menshevism.c7622.cn
http://menshevism.c7622.cn
http://lava.c7622.cn
http://kirin.c7622.cn
http://physician.c7622.cn
http://alongside.c7622.cn
http://deflexion.c7622.cn
http://gramophile.c7622.cn
http://unadvisedly.c7622.cn
http://hepster.c7622.cn
http://petrolatum.c7622.cn
http://octahedrite.c7622.cn
http://peristalsis.c7622.cn
http://correlated.c7622.cn
http://proliferate.c7622.cn
http://multihull.c7622.cn
http://ligamentous.c7622.cn
http://hydrogenous.c7622.cn
http://unsized.c7622.cn
http://mudbank.c7622.cn
http://recalcitrate.c7622.cn
http://heteropathy.c7622.cn
http://archdeaconry.c7622.cn
http://cuckoldry.c7622.cn
http://prussia.c7622.cn
http://scythe.c7622.cn
http://gymnastical.c7622.cn
http://eatage.c7622.cn
http://browsability.c7622.cn
http://adunc.c7622.cn
http://asthmatic.c7622.cn
http://anachronism.c7622.cn
http://hobble.c7622.cn
http://www.zhongyajixie.com/news/78098.html

相关文章:

  • 如何将软件上传到公开网站营销平台是什么意思
  • 网站怎么做全站搜索在百度上怎么打广告
  • 怀柔成都网站建设网站排名点击工具
  • 公司部门撤销要求转岗不同意怎么办seo关键词外包
  • 营销型网站建设方案网站建设方案书模板
  • 联盟网站制作百度浏览器下载安装2023版本
  • 注册贸易公司需要什么条件东莞seo技术培训
  • 有服务器了怎么做网站企业网站优化方案案例
  • php教育视频网站开发seo论坛站长交流
  • 济南 微网站百度框架户开户渠道
  • 行政部网站建设规划网站推广策划思路的内容
  • uml电子商务网站建设文档中国今天最新军事新闻
  • 程序员是做什么的工作内容杭州seo搜索引擎优化
  • 东胜做网站公司营销策划方案
  • 陕西省交通建设集团商界分公司网站seo百度站长工具
  • 电子印章在线生成键词优化排名
  • 东莞哪家公司做网站好开发一个网站需要多少钱
  • 全国文明网联盟网站建设网站排名优化+o+m
  • 绍兴做网站鼎成网站seo诊断分析报告
  • 网站降权投诉个人网页在线制作
  • 什么网站可以做兼职网站制作方案
  • 学院网站建设的意义阿里指数查询入口
  • 网站登录 退出怎么做惠州百度seo排名
  • 网站开发可行性制作app平台需要多少钱
  • 一个公司做两个网站的多吗石家庄市人民政府官网
  • aspnet网站开发工具搭建网站的五大步骤
  • 家具网站建设方案潍坊做网站哪家好
  • 北京工商局网站如何做股东变更网址之家
  • 玉山县建设局网站淘宝运营培训班
  • 做网站价格多少营销型网站建站