日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C#調(diào)用WORD處理的小項目 轉(zhuǎn)

系統(tǒng) 3082 0

轉(zhuǎn) ??? http://www.cnblogs.com/happygrass/archive/2009/02/13/1388224.html

?最近一個朋友讓我?guī)退鲆粋€小功能,其實就是把WORD文檔里的內(nèi)容存到數(shù)據(jù)庫里去,可以實現(xiàn)搜索并轉(zhuǎn)EXCEL的功能,需求很簡單,想不到加上部署折騰了我一個星期,我先把需求詳細描述一下:

????? 提供一個WORD文檔的樣板,這個WORD文檔里大部分是文本,其中插入了一個EXCEL表格,WORD的內(nèi)容如下:

??? ?

房地產(chǎn)價值監(jiān)證確認書

?

???????????????????? 編號:(2009交)價確字第? ?號

? 鄧征兵 ??

根據(jù)您的委托,我單位派遣專業(yè)評估人員對位于 ?? 大祥區(qū)翠園? ??? 的房地產(chǎn)(《房屋所有權(quán)證》)號為 0013210 ,房屋所有權(quán)人 ?? 鄧文兵 ?? 房屋所在層次/總層數(shù) ?6 / ?8 ,進行了現(xiàn)場勘估。

評定估價對象房屋的結(jié)構(gòu)等級為 ???? 磚混 ????? ,建成年代為 ? 90年代末 ?? ,成新度為 ?? 9成 ???

確認房屋價值如下表:

這里有個EXCEL表格

監(jiān)證目的:交易課稅

?

備注: ?????????? ?????????????????????????????????? ??????

???????????????????? ??????????????????????????????????

邵陽市房產(chǎn)產(chǎn)權(quán)監(jiān)理處價格管理科

現(xiàn)場評估: 黃生忠

審??? 批:

?? ????????????????????????????  2009 年 2 月 10 日

就是把這個文件中相關(guān)內(nèi)容存入數(shù)據(jù)庫,同時要能夠?qū)崿F(xiàn)查詢,比如根據(jù)委托人查詢,同時要把查詢的結(jié)果能轉(zhuǎn)EXCEL。

功能就是這樣的,先把其中用到的技術(shù)點挑出來,

一讀WORD里的內(nèi)容,這個并不難;

?


Microsoft.Office.Interop.Word.ApplicationClass?wordApp? = ? new ?Microsoft.Office.Interop.Word.ApplicationClass();
????????????
object ?file? = ?nam;
????????????
object ?nullobj? = ?System.Reflection.Missing.Value;
????????????
try
????????????{
????????????????Microsoft.Office.Interop.Word.Document?doc?
= ?wordApp.Documents.Open(
????????????????
ref ?file,? ref ?nullobj,? ref ?nullobj,
????????????????
ref ?nullobj,? ref ?nullobj,? ref ?nullobj,
????????????????
ref ?nullobj,? ref ?nullobj,? ref ?nullobj,
????????????????
ref ?nullobj,? ref ?nullobj,? ref ?nullobj,? ref ?nullobj,? ref ?nullobj,? ref ?nullobj,? ref ?nullobj);

????????????????
// doc.ActiveWindow.Selection.WholeStory();
????????????????
????????????????
// doc.ActiveWindow.Selection.Copy();

????????????????
// IDataObject?data?=?Clipboard.GetDataObject();
????????????????
????????????????fileContent?
= ?doc.Content.Text;??? // 這里讀取所有的WORD里的文本?????????????
}

?

二讀WORD文件里EXCEL里的數(shù)據(jù),這個是比較困難的,我試了很多方式,也沒有查到相關(guān)的資料,最后在國外論壇上看見了VB的代碼,然后修改了一下,可以用;

?


foreach ?(Microsoft.Office.Interop.Word.InlineShape?ish? in ?doc.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 ]? as ?Microsoft.Office.Interop.Excel.Worksheet).Cells[ 2 ,? 2 ]? as ?Microsoft.Office.Interop.Excel.Range).Text.ToString();
????????????????????????????
if ?(buildArea? != ? "" )
????????????????????????????{
????????????????????????????????buildUnitPrice?
= ?((objEXl.Worksheets[ 1 ]? as ?Microsoft.Office.Interop.Excel.Worksheet).Cells[ 2 ,? 3 ]? as ?Microsoft.Office.Interop.Excel.Range).Text.ToString();
????????????????????????????????buildCountPrice?
= ?((objEXl.Worksheets[ 1 ]? as ?Microsoft.Office.Interop.Excel.Worksheet).Cells[ 2 ,? 4 ]? as ?Microsoft.Office.Interop.Excel.Range).Text.ToString();
????????????????????????????????userKind?
= ? " 住宅 " ;
????????????????????????????}
????????????????????????????
else
????????????????????????????{
????????????????????????????????buildArea?
= ?((objEXl.Worksheets[ 1 ]? as ?Microsoft.Office.Interop.Excel.Worksheet).Cells[ 3 ,? 2 ]? as ?Microsoft.Office.Interop.Excel.Range).Text.ToString();
????????????????????????????????buildUnitPrice?
= ?((objEXl.Worksheets[ 1 ]? as ?Microsoft.Office.Interop.Excel.Worksheet).Cells[ 3 ,? 3 ]? as ?Microsoft.Office.Interop.Excel.Range).Text.ToString();
????????????????????????????????buildCountPrice?
= ?((objEXl.Worksheets[ 1 ]? as ?Microsoft.Office.Interop.Excel.Worksheet).Cells[ 3 ,? 4 ]? as ?Microsoft.Office.Interop.Excel.Range).Text.ToString();
????????????????????????????????userKind?
= ? " 非住宅 " ;
????????????????????????????}????????????????????????????
????????????????????????????objEXl.Application.Quit();????????????????????????????
????????????????????????}
????????????????????}
????????????????}

?

三正則表達式的應(yīng)用,我要獲取比如鄧征兵這個委托人,我不可能用SUBSTRING來取數(shù)據(jù)吧,這樣效果太低了;

?


Regex?reg1? = ? new ?Regex( " [\u4E00-\u9FFF]+ " ,?RegexOptions.IgnoreCase);
????????????????userName?
= ?reg1.Match(doc.Paragraphs[ 4 ].Range.Text).Value.Trim(); // 委托人
????????????????Regex?reg2? = ? new ?Regex( " 評估人員對位于([\\S|\\s]+)的房地產(chǎn) " ,?RegexOptions.IgnoreCase);
????????????????HouseAddr?
= ?reg2.Match(fileContent).Groups[ 1 ].Value.Trim(); // 房子地址doc.Paragraphs[5].Range.Text
????????????????Regex?reg3? = ? new ?Regex( " 號為([\\S|\\s]+),房屋所有權(quán)人 " ,?RegexOptions.IgnoreCase);
????????????????propertyNo?
= ?reg3.Match(fileContent).Groups[ 1 ].Value.Trim(); // 房產(chǎn)證號doc.Paragraphs[5].Range.Text
????????????????Regex?reg4? = ? new ?Regex( " 房屋所有權(quán)人([\\S|\\s]+)房屋所在層次 " ,?RegexOptions.IgnoreCase);
????????????????propertyUser?
= ?reg4.Match(fileContent).Groups[ 1 ].Value.Trim(); // 房屋所有權(quán)人doc.Paragraphs[5].Range.Text
????????????????Regex?reg5? = ? new ?Regex( " 層次/總層數(shù)([\\S|\\s]+),進行了現(xiàn)場勘估 " ,?RegexOptions.IgnoreCase);
????????????????buildCount?
= ?reg5.Match(fileContent).Groups[ 1 ].Value.Trim(); // 層次/總層數(shù)doc.Paragraphs[5].Range.Text
????????????????Regex?reg6? = ? new ?Regex( " 建成年代為([\\S|\\s]+),成新度為 " ,?RegexOptions.IgnoreCase);
????????????????buildYear?
= ?reg6.Match(fileContent).Groups[ 1 ].Value.Trim(); // 建成年代doc.Paragraphs[6].Range.Text
????????????????Regex?reg7? = ? new ?Regex( " 現(xiàn)場評估:([\\S|\\s]+)審 " ,?RegexOptions.IgnoreCase);
????????????????evaluateUser?
= ?reg7.Match(fileContent).Groups[ 1 ].Value.Trim(); // 現(xiàn)場評估doc.Paragraphs[13].Range.Text
????????????????Regex?reg8? = ? new ?Regex( " [\\d|\\s]+年[\\d|\\s]+月[\\d|\\s]+日 " ,?RegexOptions.IgnoreCase);
????????????????evaluateDate?
= ?reg8.Match(fileContent).Value.Trim(); // doc.Paragraphs[15].Range.Text.Trim()時間

四轉(zhuǎn)EXCEL,網(wǎng)上很多人都是用datagrid直接轉(zhuǎn)EXCEL,這樣只能倒出第一頁的數(shù)據(jù),不適合我的程序;

?


DataTable?dt? = ?GetAllData();
????????StringWriter?sw?
= ? new ?StringWriter();
????????sw.WriteLine(
" 序號\t委托方\t產(chǎn)權(quán)人\t產(chǎn)權(quán)證號\t房屋座落\t建筑面積(平方米)\t建成年代\t層次/層數(shù)\t使用性質(zhì)\t評估單價(元/平方米)\t評估總價值(元)\t現(xiàn)場評估人員\t評估日期\t備注 " );

????????
if ?(dt? != ? null )
????????{
????????????
foreach ?(DataRow?dr? in ?dt.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里的數(shù)據(jù)的時候,無法關(guān)閉EXCEL,需要用程序來關(guān)閉;

?


public ? void ?KillProcess( string ?processName)
????{
????????System.Diagnostics.Process?myproc?
= ? new ?System.Diagnostics.Process();
????????
// 得到所有打開的進程??
???????? try
????????{
????????????
foreach ?(Process?thisproc? in ?Process.GetProcessesByName(processName))
????????????{
????????????????
if ?( ! thisproc.CloseMainWindow())
????????????????{
????????????????????
if ?(thisproc? != ? null )
????????????????????????thisproc.Kill();
????????????????}
????????????}
????????}
????????
catch ?(Exception?Exc)
????????{
????????????
throw ?Exc;
????????????
// ?msg.Text+=??"殺死"??+??processName??+??"失敗!";??
????????}
????}??

C#調(diào)用WORD處理的小項目 轉(zhuǎn)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 合肥市| 呈贡县| 鄱阳县| 西昌市| 阳东县| 岚皋县| 沂源县| 阿拉善右旗| 沙湾县| 敦煌市| 大理市| 富平县| 巴林右旗| 凉城县| 黄梅县| 高碑店市| 宽城| 吉木乃县| 壶关县| 佛冈县| 新津县| 加查县| 中超| 新巴尔虎左旗| 延津县| 平果县| 老河口市| 隆安县| 嵊泗县| 余江县| 固安县| 呼和浩特市| 富裕县| 阿坝| 梁平县| 庆安县| 天峻县| 鹿邑县| 衢州市| 阿坝县| 塔河县|