成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#操作Word之2003版處理簡析

開發 后端
C#操作Word之2003版處理簡析主要向你介紹了C#操作Word對于2003版的處理實例應用,希望對你了解和學習C#操作Word有所幫助。

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

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

C#操作Word之2003版的相關處理就向你介紹到這里,希望對你了解和學習C#操作Word有所幫助。

【編輯推薦】

  1. C#操作Word實用實例淺析
  2. C#操作Word的一點認識
  3. C#操作Word學習實例淺析
  4. C#操作Word實際應用實例淺析
  5. C#操作Word學習實例淺析
責任編輯:仲衡 來源: CSDN博客
相關推薦

2009-08-13 15:48:57

C#指針

2009-09-03 17:10:57

2009-08-13 10:27:28

C#讀取Excel數據

2009-08-19 10:16:15

C#操作Word

2009-08-12 10:07:51

C#運算符

2009-08-20 09:58:06

C#操作文本文件

2009-09-03 14:20:21

C#日期格式化

2009-09-03 10:52:41

C#遞歸樹

2009-08-13 14:36:40

C#結構體構造函數

2009-08-12 16:38:35

C#讀取XML節點

2009-08-20 16:02:15

C#正則表達式

2009-08-19 10:42:08

C#操作Word表格

2009-09-09 18:41:42

C# 加密散列算法

2024-12-23 09:09:54

2009-08-19 09:42:52

C#操作Word書簽

2009-09-01 11:21:02

C#讀取word內容

2009-08-19 11:13:49

C#操作Word

2009-08-19 11:34:06

C#操作Word

2010-01-20 14:25:56

函數調用

2010-06-18 14:06:03

AMF協議
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99久久久久| 中国人pornoxxx麻豆 | 日韩中文在线观看 | 国产我和子的乱视频网站 | 超碰97人人人人人蜜桃 | 久久久精品视频一区二区三区 | 国产成人在线一区 | 欧美一区二区三区视频在线播放 | 97人人澡人人爽91综合色 | 欧美一区视频 | 粉嫩一区二区三区国产精品 | 欧美中文字幕在线 | 国产专区视频 | 欧美精品一二三区 | 中文av在线播放 | 97人人澡人人爽91综合色 | 国精产品一区一区三区免费完 | 久久i| 欧美精品二区 | 天天干视频网 | 成人av电影天堂 | 国产成人一区二区三区久久久 | 国产剧情一区二区三区 | 欧美精品第三页 | 亚洲香蕉 | 日日夜夜视频 | 玖玖玖在线观看 | 99精品久久久久久中文字幕 | www.日日干| 91久久精品 | 久久久高清 | 天堂成人国产精品一区 | 美女久久视频 | 国产精品亚洲成在人线 | 国产一区二区欧美 | 一区二区三区在线 | 日本精品久久 | 美女视频一区二区三区 | 国产三级电影网站 | 欧美国产一区二区 | 久久国内 |