亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

黃聰:C# web word文檔操作,任意指定位置插入

系統 2926 0

?? ? ?最近在做考試系統,說是要將試卷導出到word文檔中,好方便教師打印,其實考試系統這個已經是別人做爛的系統了,我的一個(資深)同事,也說過一個調侃的話,考試系統好做,但是要想做好就不容易。如果你真要做到將一張試卷,(當然是一定的word格式,包含圖片,表格等),導入到數據庫中,并且能夠成功的將其導出到word中來,(樣式基本上不能有太大的出入),就說明你做成功了。這個工作就是我現在要攻克的難關,現在只是說一個導出word文檔的問題。

思路:我原來是想通過段落 (paragraph)的方式來進行操作,但是,總是插入的圖片,不能很好的定位,后來找到問題了,應該是光標的問題。可是我總是不能很好的掌握到光標的問題,可能是我對其了解的還不夠吧,也看了一些對光標移動的一些文章 http://hi.baidu.com/tyszgkxy/blog/item/d22360f39edaec5c352acce9.html 類似的文章,網絡上有很多,我也用

object unit = Microsoft.Office.Interop.Word.WdUnits.wdParagraph
object count = 1 ;
object extend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
WordApp.Selection.MoveDown (
ref unit, ref count, ref extend );

上面的語句應該就是講光標移動到段落的末尾,然后再進行換行操作應該就可以了。但是總是不成功,也沒有出錯。我百思不得其解!

后來我就想直接在文檔操作的開始部分就設置光標,然后,只要有操作的時候,就講光標移動到指定的位置,這樣的話,就不會有光標錯位的問題了,

這樣問題就解決了,但是,接下來的問題,就是牽扯到換行的問題了,類似于光標的移動,我也不能成功的進行換行操作。郁悶!

后來,就用了,插入段落來進行代替。

WordApp.Selection.TypeParagraph();//這樣也能完成回車換行的作用。

這樣基本上問題都解決了,接下來,就將整個操作完整的寫下來。

1 . /// <summary>
2 . /// 將試卷導出到word文檔中
3 . /// </summary>
4 . /// <param name="table"> 試卷中的大題,小題等內容
5 . /// table中包含"ItemIndex", "quContent", "quMark", "quType", "para_space"幾列
6 . /// ItemIndex 相當于是 小題或者大題的題號后面有如 一、1、等
7 . /// quContent 相當如是 題干部分(不包含選項部分)選項部分為下一行的內容
8 . /// quMark 為試題的分數部分
9 . /// quType 為試題的類型,如大題:title; 選項 optiion ;題干:content
10 . /// para_space 為應該為試題留出多大的空間去讓答題人答題。
11 . /// </param>
12 . /// <param name="paperName"> 試卷的名稱,如:2009-2010上學年期末考試試卷 </param>
13 . /// <param name="totalScore"> 試卷總分 </param>
14 . /// <param name="time_length"> 時間:60分鐘 </param>
15 . /// <param name="file"> 要保存的文件的相對路徑 </param>
16 . /// <param name="fileTemplate"> 文檔模板的相對路徑 </param>
17 . private void writeDocument(DataTable table, string paperName, string totalScore, string time_length, string file, string fileTemplate)
18 . {
19 . Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
20 . Document WordDoc;
21 . string strContent = "" ;
22 . object strFileName = System.Web.HttpContext.Current.Server.MapPath(file);
23 . object fileName = System.Web.HttpContext.Current.Server.MapPath(fileTemplate);
24 . Object oMissing = System.Reflection.Missing.Value;
25 . WordDoc = WordApp.Documents.Add( ref fileName, ref oMissing, ref oMissing, ref oMissing);
26 . WordDoc.Activate();
27 . /// 試卷模板中包含三個標簽,"header"、"totalScore"、"content"
28 . /// “header” 用來存儲試卷的題目 如:2009-2010上學年期末考試試卷
29 . /// "totalScore"試卷總分+考試時間 如:滿分:100分 時間:60分鐘
30 . /// "content" 試卷的內容部分
31 . foreach (Bookmark bm in WordDoc.Bookmarks)
32 . {
33 . if (bm.Name == " header " )
34 . {
35 . bm.Select();
36 . bm.Range.Text = paperName;
37 . }
38 . if (bm.Name == " totalScore " )
39 . {
40 . bm.Select();
41 . bm.Range.Text = " 總分: " + totalScore + " 分\t時間: " + time_length + " 分鐘 " ;
42 . }
43 . if (bm.Name == " content " )
44 . {
45 . // 講光標定位到書簽內。
46 . bm.Select();
47 .
48 . // 得到光標的位置。
49 . Selection currentSelection = WordApp.Selection;
50 . #region test
51 . foreach (DataRow dr in table.Rows)
52 . {
53 . strContent = dr[ " itemIndex " ].ToString() + dr[ " quContent " ].ToString() + dr[ " quMark " ].ToString();
54 . currentSelection.Range.Font.Name = " 新宋體 " ;
55 . if (dr[ " quType " ].ToString() == " title " )
56 . {
57 . currentSelection.Range.Font.Bold = 1 ;
58 . currentSelection.Range.Font.Size = 16 ;
59 . }
60 . else if (dr[ " quType " ].ToString() == " option " )
61 . {
62 . currentSelection.Range.Font.Bold = 0 ;
63 . currentSelection.Range.Font.Size = 14 ;
64 .
65 . }
66 . else
67 . {
68 . currentSelection.Range.Font.Bold = 0 ;
69 . currentSelection.Range.Font.Size = 14 ;
70 . }
71 . for ( int i = 0 ; i < int .Parse(dr[ " para_space " ].ToString()); i ++ )
72 . {
73 . currentSelection.TypeParagraph();
74 . }
75 . if (Regex.IsMatch(strContent, @" <img[^>]*> " ))
76 . {
77 . string [] matches = Regex.Split(strContent, @" (<img[^>]*>) " );
78 . foreach ( string ma in matches)
79 . {
80 . if (Regex.IsMatch(ma, @" <img[^>]*> " ))
81 . {
82 .
83 . Match match = Regex.Match(ma, @" src=.* " );
84 . string ss = match.Value;
85 . // ss=ma.Replace("\\","");
86 . ss = ss.Replace( " src=\ "" , "" );
87 . ss = ss.Replace( " \ " /> " , "" );//這時的ss就是img的相對路徑了
88 . string imgPath = System.Web.HttpContext.Current
89 . .Server.MapPath(ss.Contains( " .. " ) ? ( " ~ " + ss.Substring( 2 )) : ( " ~ " + ss)); // 圖片所在路徑
90 . object LinkToFile = false ;
91 . object SaveWithDocument = true ;
92 . object Anchor = WordDoc.Application.Selection.Range;
93 . InlineShape li = currentSelection.InlineShapes.AddPicture(imgPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
94 . Shape s = li.ConvertToShape();
95 . // WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f; // 圖片寬度
96 . // 將圖片設置為四周環繞型
97 . s.WrapFormat.Type = WdWrapType.wdWrapSquare;
98 . }
99 . else
100 . {
101 . currentSelection.TypeText(ma);
102 . }
103 . }
104 . }
105 . else if (Regex.IsMatch(strContent, @" <table.*</table> " ))
106 . {
107 . string [] matches = Regex.Split(strContent, @" (<table.*</table>) " );
108 . foreach ( string ma in matches)
109 . {
110 . if (Regex.IsMatch(ma, @" <table.*</table> " ))
111 . {
112 . MatchCollection matchTR = Regex.Matches(ma, @" <tr[^>]*>[\s\S]*?<\/tr> " );
113 . int rowCount = matchTR.Count;
114 . MatchCollection matchTd = Regex.Matches(matchTR[ 0 ].Value, @" <td.*?>.*?</td> " );
115 . int ColumnCount = matchTd.Count;
116 . Table newTable = WordDoc.Tables.Add(
117 . currentSelection.Range, rowCount, ColumnCount, ref oMissing, ref oMissing);
118 . int i = 1 ;
119 . for (; i <= matchTR.Count; i ++ )
120 . {
121 . matchTd = Regex.Matches(matchTR[i - 1 ].Value, @" <td.*?>.*?</td> " );
122 . for ( int j = 1 ; j <= matchTd.Count; j ++ )
123 . {
124 . /// 講語句中的 <td> </td> 刪除掉
125 . string tdString = matchTd[j - 1 ].Value;
126 . tdString = Regex.Replace(tdString, @" <td.*?> " , "" );
127 . tdString = Regex.Replace(tdString, @" </td> " , "" );
128 . newTable.Cell(i, j).Range.Text = tdString;
129 . currentSelection.Tables[ 1 ].Cell(i, j).Select();
130 . }
131 . }
132 . currentSelection.TypeParagraph();
133 . }
134 . else
135 . {
136 . currentSelection.TypeText(ma);
137 . }
138 . }
139 . }
140 . else
141 . {
142 . currentSelection.TypeText(strContent);
143 . }
144 . currentSelection.TypeParagraph();
145 . }
146 . #endregion
147 . }
148 . }
149 . // 將WordDoc文檔對象的內容保存為DOC文檔
150 . WordDoc.SaveAs( ref strFileName, ref oMissing, ref oMissing, ref oMissing,
151 . ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
152 . ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
153 . ref oMissing, ref oMissing);
154 . // 關閉WordDoc文檔對象
155 . WordDoc.Close( ref oMissing, ref oMissing, ref oMissing);
156 . // 關閉WordApp組件對象
157 . WordApp.Quit( ref oMissing, ref oMissing, ref oMissing);
158 . WordOperate.KillWordProcess();
159 . }

其他的就不在此說明

至此節本上講功能給完成了,能將圖片導入到word中了,簡單的表格也可以,但是表格的定位要在斟酌一下,要記得移動光標。

圖片的位置,仍然不能進入人意,但是大體上已經完成了。

????? 效果為下圖

黃聰:C# web word文檔操作,任意指定位置插入圖片,表格簡單操作


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产精品柳州莫菁身材四 | 免费一看一级毛片 | 日韩一区二区三区四区 | 国产欧美一区二区精品性色 | 久久频这里精品香蕉久久 | 四房激情网 | 福利影院在线看 | 狠狠色丁香婷婷综合欧美 | 国产欧美日韩精品高清二区综合区 | 久久夜靖品| 香蕉国产人午夜视频在线 | 国产高清日韩 | 日本xxxwww在线观看免费 | 日本一区二区三区久久 | 精品久久久久久影院免费 | 亚洲 欧洲 另类 综合 自拍 | 伊人色婷婷 | 日日摸夜夜添夜夜添97 | 亚洲精品乱码久久久久久蜜桃 | 亚洲国产第一区二区三区 | 国产精品欧美日韩一区二区 | 97久久精品国产成人影院 | 国产日韩在线 | 欧美性一区二区三区 | 毛片大全高清免费 | 全免费一级午夜毛片 | 成年人黄色在线观看 | 日韩成人在线网站 | 日韩国产中文字幕 | 欧美三级美国一级 | 国产精品a区 | 日韩一及片 | 国产三级观看久久 | 青草视频在线观看免费资源 | 久久综合久久伊人 | 国产69精品久久久久99不卡 | 一区二区午夜 | 综合亚洲欧美 | 97黄色网| 久操精品视频 | 日日操夜夜操免费视频 |