日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#操作Word之2003版處理簡析

C#操作Word之2003版處理簡析,帶制圖功能:

成都創(chuàng)新互聯(lián)2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元項城做網(wǎng)站,已為上家服務(wù),為項城各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

 
 
 
  1. using System;
  2. using System.IO;
  3. using System.Data;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Reflection;
  7. using Microsoft.Office.Core;
  8. using Word = Microsoft.Office.Interop.Word;
  9. using Graph = Microsoft.Office.Interop.Graph;
  10.  //C#操作Word之2003版
  11. namespace NWord
  12. ...{
  13. /**//// 
  14. /// 功能描述:操作word文件
  15. /// 作者:--
  16. /// 使用說明:在工程中添加Word 11.0對象庫的引用。該模塊在office2003基礎(chǔ)上開發(fā)。
  17. /// 
  18. public class C_Word
  19. ...{
  20. Variables#region Variables
  21. private string strwordfilename;//文件名
  22. private string strwordfilepath;//文件路徑
  23. private bool wordvisible = false;//Word文件操作是否可見
  24. Word._Application WordApp = new Word.Application();
  25. Word._Document WordDoc;
  26. object missing = System.Reflection.Missing.Value;
  27. object oEndOfDoc = "\endofdoc";
  28. #endregion
  29. Properties#region Properties
  30. /**//// 
  31. /// word文件名  ,C#操作Word之2003版
  32. /// 
  33. public string WordFileName
  34. ...{
  35. get ...{ return strwordfilename; }
  36. }
  37. /**//// 
  38. /// word文件路徑
  39. /// 
  40. public string WordFilePath
  41. ...{
  42. get ...{ return strwordfilepath; }
  43. }
  44. #endregion
  45. /**//// 
  46. /// 構(gòu)造word對象
  47. /// 
  48. public C_Word() ...{ }
  49. /**//// 
  50. /// 構(gòu)造word對象  ,C#操作Word之2003版
  51. /// 
  52. /// word文件全路徑
  53. public C_Word(string strfullfilepath)
  54. ...{
  55. WordApp.Visible = false;
  56. strwordfilename = Path.GetFileName(strfullfilepath);
  57. strwordfilepath = Path.GetDirectoryName(strfullfilepath);
  58. CreateWordFile(strfullfilepath);
  59. }
  60. /**//// 
  61. /// 構(gòu)造word對象
  62. /// 
  63. /// word文件全路徑
  64. /// 是否覆蓋現(xiàn)有word文件
  65. public C_Word(string strfullfilepath, bool overwrite)
  66. ...{
  67. strwordfilename = Path.GetFileName(strfullfilepath);
  68. strwordfilepath = Path.GetDirectoryName(strfullfilepath);
  69. WordApp.Visible = wordvisible;
  70. if (overwrite || !File.Exists(strfullfilepath))
  71. ...{
  72. CreateWordFile(strfullfilepath);
  73. }
  74. else
  75. ...{
  76. this.Open(strfullfilepath);
  77. }
  78. }
  79. /**//// 
  80. /// 打開word文件
  81. /// 
  82. /// 文件路徑
  83. public void Open(string strfilepath)
  84. ...{
  85. object filepath = strfilepath;
  86. object wrdvisible = wordvisible;
  87. //WordApp.Documents.Add(ref filepath, ref missing,
  88.  ref missing, ref wrdvisible);
  89.  //C#操作Word之2003版
  90. WordApp.Documents.Open(ref filepath, ref missing,
  91.  ref missing, ref missing, ref missing
  92. , ref missing, ref missing, ref missing, ref missing
  93. , ref missing, ref missing, ref wrdvisible, ref missing
  94. , ref missing, ref missing, ref missing);
  95. WordDoc = WordApp.Documents.Open(ref   filepath,
  96. ref   missing, ref   wrdvisible, ref   missing,
  97. ref   missing, ref   missing, ref   missing, ref  missing,
  98. ref   missing, ref   missing, ref   missing, ref  wrdvisible,
  99. ref   missing, ref   missing, ref   missing, ref missing);
  100. }
  101. /**//// 
  102. /// 新建word文件
  103. /// 
  104. /// word文件路徑
  105. private void CreateWordFile(string strfullfilepath)
  106. ...{
  107. object ofilename = strfullfilepath;
  108. WordDoc = WordApp.Documents.Add(ref missing,
  109.  ref missing, ref missing, ref missing);
  110. //驗證路徑,C#操作Word之2003版
  111. if (!Directory.Exists(Path.GetDirectoryName(
  112. strfullfilepath))) return;
  113. if (Path.GetExtension(strfullfilepath).
  114. ToLower() != ".doc") return;
  115. try
  116. ...{
  117. if (File.Exists(strfullfilepath)) File.Delete(strfullfilepath);
  118. WordApp.ActiveDocument.SaveAs(ref ofilename,
  119. ref missing, ref missing, ref missing, 
  120. ref missing, ref missing,
  121. ref missing, ref missing, ref missing,
  122.  ref missing, ref missing,
  123. ref missing, ref missing, ref missing, 
  124. ref missing, ref missing);
  125. return;
  126. }
  127. catch
  128. ...{
  129. return;
  130. }
  131. }
  132. /**//// 
  133. /// 從模版創(chuàng)建word文件 ,C#操作Word之2003版
  134. /// 
  135. /// word模版路徑
  136. /// word文件路徑
  137. public void CreateFileFromDot(string strdotpath,string strdocpath)
  138. ...{
  139. object filepath = strdotpath;
  140. object owordfile = strdocpath;
  141. if (File.Exists(strdocpath))//刪除已存在的文件
  142. ...{
  143. File.Delete(strdocpath);
  144. }
  145. WordApp.Documents.Add(ref filepath, ref missing, ref missing, ref missing);
  146. WordApp.ActiveDocument.SaveAs(ref owordfile,
  147. ref missing, ref missing, ref missing, 
  148. ref missing, ref missing,
  149. ref missing, ref missing, ref missing, 
  150. ref missing, ref missing,
  151. ref missing, ref missing, ref missing, 
  152. ref missing, ref missing);
  153. this.Open(strdocpath);
  154. strwordfilepath = Path.GetDirectoryName(strdotpath);
  155. strwordfilename = Path.GetFileName(strdocpath);
  156. }
  157.  //C#操作Word之2003版
  158. /**//// 
  159. /// 向word結(jié)尾插入1行數(shù)據(jù),不會換行
  160. /// 
  161. /// 插入的字符串
  162. public void WriteLine(string strline)
  163. ...{
  164. object fileName = WordFilePath + WordFileName;
  165. object bvisible = true;
  166. Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
  167. wrdRng.InsertAfter(strline);
  168. this.Save();
  169. }
  170. /**//// 
  171. /// 將書簽更新成字符串
  172. /// 
  173. /// 書簽名
  174. /// 更新字符串
  175. public void UpdateBookmark(string bookmarkName, string newText)
  176. ...{
  177. object name = bookmarkName;
  178. Word.Range rng = WordApp.ActiveDocument.Bookmarks.
  179. get_Item(ref name).Range;
  180. rng.Text = newText;
  181. object range = rng;
  182. WordApp.ActiveDocument.Bookmarks.Add(bookmarkName, ref range);
  183. this.Save();
  184. }
  185. /**//// 
  186. /// 將書簽更新成字符串,C#操作Word之2003版
  187. /// 
  188. /// word書簽
  189. /// 更新字符串
  190. public void UpdateBookmark(Word.Bookmark bookmark, string newText)
  191. ...{
  192. object rng = bookmark.Range;
  193. string bookmarkName = bookmark.Name;
  194. bookmark.Range.Text = newText;
  195. WordApp.ActiveDocument.Bookmarks.Add(bookmarkName, ref rng);
  196. this.Save();
  197. }
  198. /**//// 
  199. /// 更改某表的某個單元格的內(nèi)容
  200. /// 
  201. /// table id
  202. /// 行id
  203. /// 列id
  204. /// 更新字符串
  205. public void UpdateTableContent(int tableID, 
  206. int lineID, int columnID, string context)
  207. ...{
  208. Word.Table tbl = WordApp.ActiveDocument.Tables[tableID];
  209. tbl.Cell(lineID, columnID).Range.Text = context;
  210. this.Save();
  211. }
  212. /**//// 
  213. /// 插入圖表  ,C#操作Word之2003版
  214. /// 
  215. /// 書簽名
  216. /// 圖表數(shù)據(jù)源datatable
  217. /// 圖表格式
  218. public void InsertChart(string strbookmark, 
  219. DataTable dtsheet,Graph.XlChartType xlcharttype)
  220. ...{
  221. int i, j;
  222. Graph.Chart wrdChart;
  223. Graph.Axis axis;
  224. object oClassType = "MSGraph.Chart.8";
  225. object bookmark = strbookmark;
  226. //在指定的書簽位置插入圖表
  227. Word.Range wrdRng = WordDoc.Bookmarks.get_Item(ref bookmark).Range;
  228. //初始化一張圖表
  229. wrdChart = (Graph.Chart)wrdRng.InlineShapes.
  230. AddOLEObject(ref oClassType, ref missing,
  231. ref missing, ref missing, ref missing,
  232. ref missing, ref missing, ref missing).OLEFormat.Object;
  233. //wrdChart.Application.Visible = false;
  234. wrdChart.Application.PlotBy = Graph.XlRowCol.xlColumns;
  235. //根據(jù)Y軸來畫圖表
  236. //改變圖表格式
  237. wrdChart.ChartType = xlcharttype;
  238. axis = (Graph.Axis)wrdChart.Axes(1, 1);//設(shè)置X軸的屬性
  239. wrdChart.Application.DataSheet.Cells.Clear();
  240. //清空表格的初始數(shù)據(jù)
  241. //填充圖表,起始的行號和列號都是1
  242. for (i = 0; i < dtsheet.Columns.Count; i++)//初始化列名
  243. ...{
  244. wrdChart.Application.DataSheet.Cells[1, i + 1] = 
  245. dtsheet.Columns[i].ColumnName;
  246. }
  247. for (i = 0; i < dtsheet.Rows.Count; i++)//填充數(shù)據(jù)
  248. ...{
  249. for (j = 0; j < dtsheet.Columns.Count; j++)
  250. ...{
  251. wrdChart.Application.DataSheet.Cells[i + 2, j + 1] = 
  252. dtsheet.Rows[i][j].ToString().Replace("9999999", "100ys");
  253. }
  254. }
  255. //axis.MaximumScale = 1;//X軸最大刻度
  256. //axis.MajorUnit = 0.1;
  257.  //C#操作Word之2003版
  258. wrdChart.Legend.Delete();
  259. wrdChart.Width = 500;
  260. //wrdChart.Height = 666;
  261. //oShape.Height = oWord.InchesToPoints(3.57f);
  262. //更新圖表并保存退出
  263. wrdChart.Application.Update();
  264. wrdChart.Application.Quit();
  265. this.Save();
  266. }
  267. /**//// 
  268. /// 清空文檔
  269. /// 
  270. public void Clear()
  271. ...{
  272. object Unit = (int)Word.WdUnits.wdCharacter;
  273. object Count = 1;
  274. WordApp.Selection.WholeStory();
  275. WordApp.Selection.Delete(ref Unit, ref Count);
  276. this.Save();
  277. }
  278. /**//// 
  279. /// 另存為
  280. /// 
  281. /// 目標文件路徑
  282. public void SaveAs(string strFileName)
  283. ...{
  284. object fileName = strFileName;
  285. WordApp.ActiveDocument.SaveAs(ref fileName,
  286. ref missing, ref missing, ref missing, ref missing,
  287. ref missing, ref missing, ref missing, ref missing,
  288. ref missing, ref missing, ref missing, ref missing,
  289. ref missing, ref missing, ref missing);
  290. }
  291. /**//// 
  292. /// 轉(zhuǎn)到指定的標簽處
  293. /// 
  294. /// 標簽名
  295. public void GotoBookMark(string strBookMarkName)
  296. ...{
  297. object Bookmark = (int)Word.WdGoToItem.wdGoToBookmark;
  298. object NameBookMark = strBookMarkName;
  299. WordApp.Selection.GoTo(ref Bookmark,
  300.  ref missing, ref missing, ref NameBookMark);
  301. }
  302. /**//// 
  303. /// 刪除指定的文本
  304. /// 
  305. /// 被刪除的文本
  306. public void DeleteText(string text)
  307. ...{
  308. object findText = text;
  309. object replaceWith = "";
  310. object replaceAll = Word.WdReplace.wdReplaceAll;
  311. this.WordApp.Selection.Find.ClearFormatting();
  312. this.WordApp.Selection.Find.Replacement.ClearFormatting();
  313. this.WordApp.Selection.Find.Replacement.Text = "";
  314. this.WordApp.Selection.Find.Execute(ref findText,
  315. ref missing, ref missing, ref missing, 
  316. ref missing, ref missing, ref missing,
  317. ref missing, ref missing, ref replaceWith,
  318.  ref replaceAll, ref missing, ref missing,
  319. ref missing, ref missing);
  320. object unit = (int)Word.WdUnits.wdCharacter;
  321. object count = 1;
  322. this.WordApp.Selection.Delete(ref unit, ref count);
  323. this.Save();
  324. }
  325. /**//// 
  326. /// 保存當前word文檔
  327. /// 
  328. private void Save()
  329. ...{
  330. WordApp.ActiveDocument.Save();
  331. }
  332. /**//// 
  333. /// 關(guān)閉Word文件,釋放對象;C#操作Word之2003版
  334. ///最后一定要調(diào)用此函數(shù),否則會引起異常
  335. /// 
  336. public void Close()
  337. ...{
  338. try
  339. ...{
  340. WordApp.Application.Quit(ref missing, 
  341. ref missing, ref missing);
  342. if (WordApp != null)
  343. ...{
  344. WordApp = null;
  345. }
  346. }
  347. catch
  348. ...{ }
  349. finally
  350. ...{
  351. GC.Collect();
  352. GC.WaitForPendingFinalizers();
  353. GC.Collect();
  354. GC.WaitForPendingFinalizers();
  355. }
  356. }
  357. }
  358. }

C#操作Word之2003版的相關(guān)處理就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作Word有所幫助。


當前題目:C#操作Word之2003版處理簡析
文章網(wǎng)址:http://m.5511xx.com/article/djggeph.html