自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#操作Word之2003版處理簡(jiǎn)析

開(kāi)發(fā) 后端
C#操作Word之2003版處理簡(jiǎn)析主要向你介紹了C#操作Word對(duì)于2003版的處理實(shí)例應(yīng)用,希望對(duì)你了解和學(xué)習(xí)C#操作Word有所幫助。

C#操作Word之2003版處理簡(jiǎn)析,帶制圖功能:

  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. /// 使用說(shuō)明:在工程中添加Word 11.0對(duì)象庫(kù)的引用。該模塊在office2003基礎(chǔ)上開(kāi)發(fā)。  
  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文件操作是否可見(jiàn)  
  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. /// 構(gòu)造word對(duì)象  
  50. /// </summary>  
  51. public C_Word() ...{ }  
  52.  
  53. /**//// <summary>  
  54. /// 構(gòu)造word對(duì)象  ,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. /// 構(gòu)造word對(duì)象  
  67. /// </summary>  
  68. /// <param name="strfullfilepath">word文件全路徑</param>  
  69. /// <param name="overwrite">是否覆蓋現(xiàn)有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. /// 打開(kāi)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. //驗(yàn)證路徑,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. /// 從模版創(chuàng)建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結(jié)尾插入1行數(shù)據(jù),不會(huì)換行  
  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. /// 將書(shū)簽更新成字符串  
  189. /// </summary>  
  190. /// <param name="bookmarkName">書(shū)簽名</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. /// 將書(shū)簽更新成字符串,C#操作Word之2003版  
  205. /// </summary>  
  206. /// <param name="bookmark">word書(shū)簽</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. /// 更改某表的某個(gè)單元格的內(nèi)容  
  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">書(shū)簽名</param>  
  235. /// <param name="dtsheet">圖表數(shù)據(jù)源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. //在指定的書(shū)簽位置插入圖表  
  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. //根據(jù)Y軸來(lái)畫(huà)圖表  
  259.  
  260.  
  261. //改變圖表格式  
  262. wrdChart.ChartType = xlcharttype;  
  263.  
  264. axis = (Graph.Axis)wrdChart.Axes(1, 1);//設(shè)置X軸的屬性  
  265. wrdChart.Application.DataSheet.Cells.Clear();  
  266. //清空表格的初始數(shù)據(jù)  
  267.  
  268. //填充圖表,起始的行號(hào)和列號(hào)都是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++)//填充數(shù)據(jù)  
  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">目標(biāo)文件路徑</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. /// 轉(zhuǎn)到指定的標(biāo)簽處  
  327. /// </summary>  
  328. /// <param name="strBookMarkName">標(biāo)簽名</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. /// 保存當(dāng)前word文檔  
  365. /// </summary>  
  366. private void Save()  
  367. ...{  
  368. WordApp.ActiveDocument.Save();  
  369. }  
  370.  
  371. /**//// <summary>  
  372. /// 關(guān)閉Word文件,釋放對(duì)象;C#操作Word之2003版  
  373. ///最后一定要調(diào)用此函數(shù),否則會(huì)引起異常  
  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版的相關(guān)處理就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C#操作Word有所幫助。

【編輯推薦】

  1. C#操作Word實(shí)用實(shí)例淺析
  2. C#操作Word的一點(diǎn)認(rèn)識(shí)
  3. C#操作Word學(xué)習(xí)實(shí)例淺析
  4. C#操作Word實(shí)際應(yīng)用實(shí)例淺析
  5. C#操作Word學(xué)習(xí)實(shí)例淺析
責(zé)任編輯:仲衡 來(lái)源: CSDN博客
相關(guān)推薦

2009-08-13 15:48:57

C#指針

2009-08-19 10:16:15

C#操作Word

2009-08-13 10:27:28

C#讀取Excel數(shù)據(jù)

2009-09-03 17:10:57

2009-08-12 10:07:51

C#運(yùn)算符

2009-08-20 09:58:06

C#操作文本文件

2009-09-03 14:20:21

C#日期格式化

2009-09-03 10:52:41

C#遞歸樹(shù)

2009-08-13 14:36:40

C#結(jié)構(gòu)體構(gòu)造函數(shù)

2009-08-12 16:38:35

C#讀取XML節(jié)點(diǎn)

2009-08-20 16:02:15

C#正則表達(dá)式

2009-08-19 10:42:08

C#操作Word表格

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2009-08-19 09:42:52

C#操作Word書(shū)簽

2009-09-01 11:21:02

C#讀取word內(nèi)容

2009-09-09 18:41:42

C# 加密散列算法

2024-12-23 09:09:54

2010-01-20 14:25:56

函數(shù)調(diào)用

2009-08-19 11:28:41

C#操作Word
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)