網(wǎng)站模板jsp設(shè)計(jì)公司排名前十強(qiáng)
目錄
功能需求
Office 數(shù)據(jù)源的一些映射關(guān)系
范例運(yùn)行環(huán)境
配置Office DCOM
關(guān)鍵代碼
組件庫引入
?核心代碼
殺掉進(jìn)程
總結(jié)
功能需求
在應(yīng)用項(xiàng)目里,多數(shù)情況下我們會(huì)遇到導(dǎo)入 Excel 文件數(shù)據(jù)到數(shù)據(jù)庫的功能需求,但某些情況下,也存在使用 Word 進(jìn)行表格數(shù)據(jù)編輯的情況。Word 和 Excel 其實(shí)各有特點(diǎn),用戶的習(xí)慣不同,即使同一數(shù)據(jù)源,可能提供的數(shù)據(jù)源文件類型也不同,這其中也包括導(dǎo)入Word內(nèi)容的功能,比如表格數(shù)據(jù)導(dǎo)出到DataSet數(shù)據(jù)集。
Office 數(shù)據(jù)源的一些映射關(guān)系
下圖是一個(gè)簡單的 Office 數(shù)據(jù)源的映射關(guān)系:
1、第一層級(jí)比如 WORD / EXCEL 為應(yīng)用層級(jí)(Application)、?DATASET / DATABASE 為數(shù)據(jù)容器
2、第二層級(jí),比如WORD 包含一個(gè)文檔對(duì)象(Docment)、Excel 包含一個(gè)工作簿對(duì)象(WorkBook)、DataSet / DataBase 包括一組數(shù)據(jù)表對(duì)象(Tables)
3、第三層級(jí),比如Word里的表格對(duì)象(Table)、Excel里的工作表對(duì)象(Sheet)
最實(shí)際的工作任務(wù),是要將Table或Sheet對(duì)象的二維數(shù)據(jù)對(duì)應(yīng)導(dǎo)出生成到 DataSet 里的 Table 對(duì)象,如果有多個(gè)則生成對(duì)應(yīng)的集合。最后我們可能會(huì)再次導(dǎo)出到 DataBase 的數(shù)據(jù)表集合里(Tables)。
范例運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
操作系統(tǒng)上安裝 Office Word 2016
.net版本:?.netFramework4.7.1 或以上
開發(fā)工具:VS2019 ?C#
配置Office DCOM
對(duì)于安裝原生Office應(yīng)用,我們需要對(duì)DCOM進(jìn)行進(jìn)一步的配置方可使用其API。
打開控制面板、管理工具、組件服務(wù):
點(diǎn)擊組件服務(wù)、計(jì)算機(jī)、我的電腦、DCOM配置?
?找到 Microsoft Word97-2003 文檔應(yīng)用程序
?選擇屬性、打開標(biāo)識(shí)選項(xiàng)卡、選擇下列用戶選項(xiàng),設(shè)置啟動(dòng)Word應(yīng)用的用戶,點(diǎn)確定即可。
?理論上設(shè)置到這里就可以了,但以防萬一,可以繼續(xù)設(shè)置啟動(dòng)權(quán)限,選擇安全選項(xiàng)卡、啟動(dòng)和激活權(quán)限,如下圖:
關(guān)鍵代碼
組件庫引入
核心代碼
public DataSet WordAsDataSet(string _filename) 方法,傳入要讀取的 WORD 文件路徑即可,方法會(huì)遍歷該WORD里的TABLES對(duì)象集合,如果找到TABLE對(duì)象,則按列的順序創(chuàng)建字段列,比如F1、F2...Fn,以些類推,從第二行起為記錄行,則根據(jù)創(chuàng)建的結(jié)構(gòu)寫入到 DataTable中。
public DataSet WordAsDataSet(string _filename){DataSet ds = new DataSet();Object Nothing = System.Reflection.Missing.Value;object filename = _filename;//創(chuàng)建一個(gè)名為WordApp的組件對(duì)象DateTime beforetime = DateTime.Now;Word.Application WordApp = new Word.Application();//創(chuàng)建一個(gè)名為WordDoc的文檔對(duì)象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;//關(guān)閉拼寫檢查WordDoc.ShowSpellingErrors = false;//關(guān)閉顯示拼寫錯(cuò)誤提示框DateTime aftertime = DateTime.Now;
//遍歷所有的Word里的表格,并寫到數(shù)據(jù)集的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);//關(guān)閉WordApp組件對(duì)象WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);KillProcessByStartTime("WINWORD", beforetime, aftertime);return ds;}
殺掉進(jìn)程
這是一個(gè)無奈之舉,嘗試了一些方法,但某些情況下仍然無法釋放掉 Word 應(yīng)用進(jìn)程,因此根據(jù)時(shí)間點(diǎn)范圍寫了一個(gè)強(qiáng)制殺掉進(jìn)程的方法。
示例代碼如下:
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 "";}
總結(jié)
在實(shí)際的應(yīng)用中,無論是導(dǎo)入的文件格式還是導(dǎo)出的數(shù)據(jù)源,都是要結(jié)合客戶的需求進(jìn)行的。
在功能實(shí)現(xiàn)前,需要約定模板文件的格式,字段內(nèi)容的意義、長度等。導(dǎo)入到 DataSet 成功后,再根據(jù)業(yè)務(wù)邏輯進(jìn)行后續(xù)操作再加工,或直接導(dǎo)入到規(guī)范的數(shù)據(jù)表里(如 MS SQL SERVER)。
這些代碼我們提供了一些操作WORD相關(guān)的關(guān)鍵方法,這里僅作參考,歡迎大家評(píng)論指教!