轉 http://www.cnblogs.com/happygrass/archive/2009/02/13/1388224.html
最近一個朋友讓我幫他做一個小功能,其實就是把WORD文檔里的內容存到數據庫里去,可以實現搜索并轉EXCEL的功能,需求很簡單,想不到加上部署折騰了我一個星期,我先把需求詳細描述一下:
提供一個WORD文檔的樣板,這個WORD文檔里大部分是文本,其中插入了一個EXCEL表格,WORD的內容如下:
房地產價值監證確認書
編號:(2009交)價確字第 號
鄧征兵 :
根據您的委托,我單位派遣專業評估人員對位于 大祥區翠園 的房地產(《房屋所有權證》)號為 0013210 ,房屋所有權人 鄧文兵 房屋所在層次/總層數 6 / 8 ,進行了現場勘估。
評定估價對象房屋的結構等級為 磚混 ,建成年代為 90年代末 ,成新度為 9成 。
確認房屋價值如下表:
這里有個EXCEL表格
監證目的:交易課稅
備注:
邵陽市房產產權監理處價格管理科
現場評估:黃生忠
審 批:
2009 年 2 月 10 日
就是把這個文件中相關內容存入數據庫,同時要能夠實現查詢,比如根據委托人查詢,同時要把查詢的結果能轉EXCEL。
功能就是這樣的,先把其中用到的技術點挑出來,
一讀WORD里的內容,這個并不難;

Code
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里的數據,這個是比較困難的,我試了很多方式,也沒有查到相關的資料,最后在國外論壇上看見了VB的代碼,然后修改了一下,可以用;

Code
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();
}
}
}
三正則表達式的應用,我要獲取比如鄧征兵這個委托人,我不可能用SUBSTRING來取數據吧,這樣效果太低了;

Code
Regex reg1 = new Regex("[\u4E00-\u9FFF]+", RegexOptions.IgnoreCase);
userName = reg1.Match(doc.Paragraphs[4].Range.Text).Value.Trim();//委托人
Regex reg2 = new Regex("評估人員對位于([\\S|\\s]+)的房地產", RegexOptions.IgnoreCase);
HouseAddr = reg2.Match(fileContent).Groups[1].Value.Trim();//房子地址doc.Paragraphs[5].Range.Text
Regex reg3 = new Regex("號為([\\S|\\s]+),房屋所有權人", RegexOptions.IgnoreCase);
propertyNo = reg3.Match(fileContent).Groups[1].Value.Trim();//房產證號doc.Paragraphs[5].Range.Text
Regex reg4 = new Regex("房屋所有權人([\\S|\\s]+)房屋所在層次", RegexOptions.IgnoreCase);
propertyUser = reg4.Match(fileContent).Groups[1].Value.Trim();//房屋所有權人doc.Paragraphs[5].Range.Text
Regex reg5 = new Regex("層次/總層數([\\S|\\s]+),進行了現場勘估", RegexOptions.IgnoreCase);
buildCount = reg5.Match(fileContent).Groups[1].Value.Trim();//層次/總層數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("現場評估:([\\S|\\s]+)審", RegexOptions.IgnoreCase);
evaluateUser = reg7.Match(fileContent).Groups[1].Value.Trim();//現場評估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()時間
四轉EXCEL,網上很多人都是用datagrid直接轉EXCEL,這樣只能倒出第一頁的數據,不適合我的程序;

Code
DataTable dt = GetAllData();
StringWriter sw = new StringWriter();
sw.WriteLine("序號\t委托方\t產權人\t產權證號\t房屋座落\t建筑面積(平方米)\t建成年代\t層次/層數\t使用性質\t評估單價(元/平方米)\t評估總價值(元)\t現場評估人員\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里的數據的時候,無法關閉EXCEL,需要用程序來關閉;

Code
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 + "失敗!";
}
}
posted on 2009-02-24 16:48
sanmao 閱讀(285)
評論(0) 編輯 收藏