轉(zhuǎn) http://www.cnblogs.com/happygrass/archive/2009/02/13/1388224.html
最近一個朋友讓我?guī)退鲆粋€小功能,其實就是把WORD文檔里的內(nèi)容存到數(shù)據(jù)庫里去,可以實現(xiàn)搜索并轉(zhuǎn)EXCEL的功能,需求很簡單,想不到加上部署折騰了我一個星期,我先把需求詳細(xì)描述一下:
提供一個WORD文檔的樣板,這個WORD文檔里大部分是文本,其中插入了一個EXCEL表格,WORD的內(nèi)容如下:
房地產(chǎn)價值監(jiān)證確認(rèn)書
編號:(2009交)價確字第 號
鄧征兵 :
根據(jù)您的委托,我單位派遣專業(yè)評估人員對位于 大祥區(qū)翠園 的房地產(chǎn)(《房屋所有權(quán)證》)號為 0013210 ,房屋所有權(quán)人 鄧文兵 房屋所在層次/總層數(shù) 6 / 8 ,進行了現(xiàn)場勘估。
評定估價對象房屋的結(jié)構(gòu)等級為 磚混 ,建成年代為 90年代末 ,成新度為 9成 。
確認(rèn)房屋價值如下表:
這里有個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();
}
}
}
三正則表達(dá)式的應(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 + "失敗!";
}
}