C#调用WORD处理的实例代码(包含excel)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

最近程序人生(http://www.manong123.com)一个朋友让我帮他做一个小功能,其实就是把Word文档里的内容存到数据库里去,可以实现搜索并转Excel的功能,需求很简单,想不到加上部署折腾了我一个星期,我先把需求详细描述一下:

提供一个WORD文档的样板,这个WORD文档里大部分是文本,其中插入了一个EXCEL表格,WORD的内容如下:

房地产价值监证确认书

编号:(2009交)价确字第 号

邓征兵 :

根据您的委托,我单位派遣专业评估人员对位于 大祥区翠园 的房地产(《房屋所有权证》)号为 0013210 ,房屋所有权人 邓文兵 房屋所在层次/总层数 6 / 8 ,进行了现场勘估。

评定估价对象房屋的结构等级为 砖混 ,建成年代为 90年代末 ,成新度为 9成 。

确认房屋价值如下表:

这里有个EXCEL表格

监证目的:交易课税

备注:

邵阳市房产产权监理处价格管理科

现场评估:黄生忠

审 批:

2009 年 2 月 10 日

就是把这个文件中相关内容存入数据库,同时要能够实现查询,比如根据委托人查询,同时要把查询的结果能转EXCEL。

功能就是这样的,先把其中用到的技术点挑出来,

一读WORD里的内容,这个并不难;

Microsoft.Office.Interop.Word.applicationClass wordApp =newMicrosoft.Office.Interop.Word.ApplicationClass();objectfile =nam;objectnullobj =System.Reflection.Missing.Value;try{

Microsoft.Office.Interop.Word.Document doc=wordApp.Documents.Open(reffile,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj,refnullobj);//doc.ActiveWindow.Selection.WholeStory();//doc.ActiveWindow.Selection.Copy();//IDataObject data = Clipboard.GetDataObject();fileContent= doc.Content.Text;//这里读取所有的WORD里的文本}

二读WORD文件里EXCEL里的数据,这个是比较困难的,我试了很多方式,也没有查到相关的资料,最后在国外论坛上看见了VB的代码,然后修改了一下,可以用;

foreach(Microsoft.Office.Interop.Word.InlineShape ishindoc.InlineShapes)

{if(ish.Type ==Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeEmbeddedOLEObject)

{if(ish.OLEFormat.PRogID =="Excel.Sheet.8")

{//ish.OLEFormat.DoVerb(ref nullobj);ish.OLEFormat.Activate();

Microsoft.Office.Interop.Excel.Workbook objEXl=(Microsoft.Office.Interop.Excel.Workbook)ish.OLEFormat.Object;

buildArea= ((objEXl.Worksheets[1]asMicrosoft.Office.Interop.Excel.Worksheet).Cells[2,2]asMicrosoft.Office.Interop.Excel.Range).Text.ToString();if(buildArea !="")

{

buildUnitPrice= ((objEXl.Worksheets[1]asMicrosoft.Office.Interop.Excel.Worksheet).Cells[2,3]asMicrosoft.Office.Interop.Excel.Range).Text.ToString();

buildCountPrice= ((objEXl.Worksheets[1]asMicrosoft.Office.Interop.Excel.Worksheet).Cells[2,4]asMicrosoft.Office.Interop.Excel.Range).Text.ToString();

userKind="住宅";

}else{

buildArea= ((objEXl.Worksheets[1]asMicrosoft.Office.Interop.Excel.Worksheet).Cells[3,2]asMicrosoft.Office.Interop.Excel.Range).Text.ToString();

buildUnitPrice= ((objEXl.Worksheets[1]asMicrosoft.Office.Interop.Excel.Worksheet).Cells[3,3]asMicrosoft.Office.Interop.Excel.Range).Text.ToString();

buildCountPrice= ((objEXl.Worksheets[1]asMicrosoft.Office.Interop.Excel.Worksheet).Cells[3,4]asMicrosoft.Office.Interop.Excel.Range).Text.ToString();

userKind="非住宅";

}

objEXl.Application.Quit();

}

}

}

三正则表达式的应用,我要获取比如邓征兵这个委托人,我不可能用SUBSTRING来取数据吧,这样效果太低了;

Regex reg1 =newRegex("[\u4E00-\u9FFF]+", RegexOptions.IgnoreCase);

userName= reg1.Match(doc.Paragraphs[4].Range.Text).Value.Trim();//委托人Regex reg2 =newRegex("评估人员对位于([\\S|\\s]+)的房地产", RegexOptions.IgnoreCase);

HouseAddr= reg2.Match(fileContent).Groups[1].Value.Trim();//房子地址doc.Paragraphs[5].Range.TextRegex reg3 =newRegex("号为([\\S|\\s]+),房屋所有权人", RegexOptions.IgnoreCase);

propertyNo= reg3.Match(fileContent).Groups[1].Value.Trim();//房产证号doc.Paragraphs[5].Range.TextRegex reg4 =newRegex("房屋所有权人([\\S|\\s]+)房屋所在层次", RegexOptions.IgnoreCase);

propertyUser= reg4.Match(fileContent).Groups[1].Value.Trim();//房屋所有权人doc.Paragraphs[5].Range.TextRegex reg5 =newRegex("层次/总层数([\\S|\\s]+),进行了现场勘估", RegexOptions.IgnoreCase);

buildCount= reg5.Match(fileContent).Groups[1].Value.Trim();//层次/总层数doc.Paragraphs[5].Range.TextRegex reg6 =newRegex("建成年代为([\\S|\\s]+),成新度为", RegexOptions.IgnoreCase);

buildYear= reg6.Match(fileContent).Groups[1].Value.Trim();//建成年代doc.Paragraphs[6].Range.TextRegex reg7 =newRegex("现场评估:([\\S|\\s]+)审", RegexOptions.IgnoreCase);

evaluateUser= reg7.Match(fileContent).Groups[1].Value.Trim();//现场评估doc.Paragraphs[13].Range.TextRegex reg8 =newRegex("[\\d|\\s]+年[\\d|\\s]+月[\\d|\\s]+日", RegexOptions.IgnoreCase);

evaluateDate= reg8.Match(fileContent).Value.Trim();//doc.Paragraphs[15].Range.Text.Trim()时间

四转EXCEL,网上很多人都是用datagrid直接转EXCEL,但是程序人生的朋友说这样只能倒出第一页的数据,不适合他要的程序;

DataTable dt =GetAllData();

StringWriter sw=newStringWriter();

sw.WriteLine("序号\t委托方\t产权人\t产权证号\t房屋座落\t建筑面积(平方米)\t建成年代\t层次/层数\t使用性质\t评估单价(元/平方米)\t评估总价值(元)\t现场评估人员\t评估日期\t备注");if(dt !=null)

{foreach(DataRow drindt.Rows)

{

sw.WriteLine(dr["id"] +"\t"+ dr["userName"] +"\t"+ dr["propertyUser"] +"\t"+ dr["propertyNo"] +"\t"+dr["HouseAddr"] +"\t"+ dr["buildArea"] +"\t"+ dr["buildYear"] +"\t"+ dr["buildCount"] +"\t"+dr["userKind"] +"\t"+ dr["buildUnitPrice"] +"\t"+ dr["buildCountPrice"] +"\t"+ dr["evaluateUser"]+"\t"+ dr["evaluateDate"] +"\t"+"");

}

}

sw.Close();

Response.AddHeader("content-disposition","attachment; filename=MyExcelFile.xls");

Response.ContentType="application/excel";

Response.ContentEncoding= System.Text.Encoding.GetEncoding("GB2312");

Response.Write(sw);

Response.End();

五,KILL进程,我在查看WORD里EXCEL里的数据的时候,无法关闭EXCEL,需要用程序来关闭;

publicvoidKillProcess(stringprocessName)

{

System.Diagnostics.Process myproc=newSystem.Diagnostics.Process();//得到所有打开的进程try{foreach(Process thisprocinProcess.GetProcessesByName(processName))

{if(!thisproc.CloseMainWindow())

{if(thisproc !=null)

thisproc.Kill();

}

}

}catch(Exception Exc)

{throwExc;//msg.Text+= "杀死" + processName + "失败!";}

}

大家有空去程序人生 看看,很好的文章 。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航