C#實現(xiàn)讀取二維數(shù)組集合并輸出到Word預設表格
應用場景
存儲或導出個人WORD版簡歷是招聘應用系統(tǒng)中的常用功能,我們通常會通過應用系統(tǒng)采集用戶的個人簡歷信息到數(shù)據(jù)庫,許多情況下我們會讀取數(shù)據(jù)記錄導出到WORD文件中,以便匯總、打印。其中,WORD表格元素是經(jīng)常會被用到的,比如問卷調查表格、教育經(jīng)歷表格、工作經(jīng)歷表格等等。
設計約定
1、設計WORD模板,在需要輸出值的地方繪制表格,表格的數(shù)量對應輸出的數(shù)據(jù)表數(shù)量。
2、根據(jù)條件查詢數(shù)據(jù)表,生成 DataSet ,遍歷 Tables ,生成對應的二維數(shù)組集合,將數(shù)據(jù)輸出到對應的WORD表格中。
3、按約定,WORD表格的數(shù)量應該大于等于 DataSet 里的 Tables 數(shù)量。
4、按參數(shù)設定對應的WORD表格ID序號,如參數(shù)設定為2,則表示輸出到WORD文件中的第二個表格中。(注意:WORD表格序號以1為開始)
在輸出寫入的方式中,我們約定有兩種方式:
一為覆蓋式寫入(即已預知數(shù)據(jù)輸出模式)舉例如下圖:
二為動態(tài)式寫入(即未知數(shù)據(jù)行數(shù)量)舉例如下圖:
范例運行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
操作系統(tǒng)上安裝 Office Word 2016
數(shù)據(jù)庫:Microsoft SQL Server 2016
.net版本: .netFramework4.7.1 或以上
開發(fā)工具:VS2019 C#
配置Office DCOM
配置方法可參照我的文章《C# 讀取Word表格到DataSet》進行處理和配置。
實現(xiàn)代碼
組件庫引入
核心代碼
DataSet轉二維數(shù)組
public object[,] DataSetToObjectData(DataSet ds,int TableId,bool hastitle),該方法提供3個參數(shù),DataSet數(shù)據(jù)集、指定表索引、導出是否包含標題列名稱。
public object[,] DataSetToObjectData(DataSet ds,int TableId,bool hastitle) { ArrayList rowdata = new ArrayList(); int _fieldcount=ds.Tables[TableId].Columns.Count; Object[] colvalues = new Object[_fieldcount]; for(int i=0;i<_fieldcount;i++) { colvalues[i]=ds.Tables[TableId].Columns[i].ColumnName; } if(hastitle) rowdata.Add(colvalues); for(int i=0;i<ds.Tables[TableId].Rows.Count;i++) { Object[] values = new Object[_fieldcount]; values=ds.Tables[TableId].Rows[i].ItemArray; rowdata.Add(values); RowsCount++; } object[,] rv=new object[rowdata.Count,_fieldcount]; for(int i=0;i<rowdata.Count;i++) { for(int j=0;j<_fieldcount;j++) { rv[i,j]=((object[])rowdata[i])[j]; } } return rv; }
導出寫入WORD表格
public string DataSetToWordTables(string _filename,int[,] _drawtype,object[] _datas),該方法提供3個參數(shù),使用的WORD模板文件路徑、寫入配置(包括要寫入的WORD表格ID,寫入方式:0為覆蓋;1為從第二行動態(tài)追加;3為從第一行動態(tài)追加)、對應的二維數(shù)組集合。
public string DataSetToWordTables(string _filename,int[,] _drawtype,object[] _datas) { Object Nothing = System.Reflection.Missing.Value; object filename = _filename; //創(chuàng)建一個名為WordApp的組件對象 Word.Application WordApp = new Word.Application(); //創(chuàng)建一個名為WordDoc的文檔對象 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;//關閉拼寫檢查 WordDoc.ShowSpellingErrors = false;//關閉顯示拼寫錯誤提示框 for(int i=_drawtype.GetLength(0)-1;i>=0;i--) { int _tableid=_drawtype[i,0]; int _draw=_drawtype[i,1]; object[,] r_datas=(object[,])_datas[i]; int _rowscount=r_datas.GetLength(0); if (_rowscount == 0) { WordDoc.Tables[_tableid].Delete(); continue; } int startrowid = 2; if (_draw == 3) { startrowid = 1; } if(_draw==1||_draw==3) { object _row=(object)WordDoc.Tables[_tableid].Rows[startrowid]; for(int j=0;j<_rowscount-1;j++) { WordDoc.Tables[_tableid].Rows.Add(ref _row); } } for (int k = 0; k < r_datas.GetLength(0); k++) { for(int m=0;m<r_datas.GetLength(1);m++) { WordDoc.Tables[_tableid].Cell(k + startrowid, m + 1).Range.Text = r_datas[k, m].ToString(); } } } WordDoc.Save(); WordDoc.Close(ref Nothing, ref Nothing, ref Nothing); //關閉WordApp組件對象 WordApp.Quit(ref Nothing, ref Nothing, ref Nothing); return ""; }
調用舉例
如下為示例代碼:
string _filename=@"x:\test.docx"; int[,] _drawtype=new int[2,2]; _drawtype[0,0]=1; //寫入WORD第一個表格 _drawtype[0,1]=0; //覆蓋式寫入 _drawtype[1,0]=2; //寫入WORD第二個表格 _drawtype[1,1]=1; //動態(tài)追加行寫入 object[] _datas=new object[2]; DataSet ds=getDataSet(); //獲取DataSet對象 _datas[0]=DataSetToObjectData(ds,0,false); //獲取DataSet Tables[0]的二維數(shù)組數(shù)據(jù) _datas[1]=DataSetToObjectData(ds,1,false); //獲取DataSet Tables[1]的二維數(shù)組數(shù)據(jù) string result=DataSetToWordTables(_filename,_drawtype,_datas); //寫入Word表格
小結
1、核心代碼中對無數(shù)據(jù)的表格采取了刪除WORD表格的操作。
2、預設表格的優(yōu)點在于所見即所得,樣式設計簡單化,便于理解輸出,我們還可以采用關鍵字定位并動態(tài)添加WORD表格的形式進行輸出,來實現(xiàn)更加復雜的樣式等。
3、核心代碼中輸出的起始行為第2行,根據(jù)設置也可以為第1行。如果我們設計的表格比較復雜(如包括合并單元格等),則可以對 _drawtype進行改造,增加一個參數(shù),用于指定從第幾行開始進行輸出。
到此這篇關于C#實現(xiàn)讀取二維數(shù)組集合并輸出到Word預設表格的文章就介紹到這了,更多相關C#讀取二維數(shù)組并輸出到Word內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于C#?實現(xiàn)?OPC?DA?Server的問題小結
這篇文章主要介紹了基于C#?實現(xiàn)?OPC?DA?Server的相關知識,關于C#怎么編寫一個進程外的DCOM組件,這里先不做介紹了,這里主要介紹下OPC?DA?Server?的第一個接口,感興趣的朋友跟隨小編一起看看吧2024-04-04WPF自定義控件實現(xiàn)ItemsControl魚眼效果
這篇文章主要為大家詳細介紹了WPF如何通過自定義控件實現(xiàn)ItemsControl魚眼效果,文中的示例代碼講解詳細,需要的可以參考一下2024-01-01