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-08-19 10:16:15

C#操作Word

2009-09-03 17:10:57

2009-08-13 10:27:28

C#读取Excel数据

2009-08-20 09:58:06

C#操作文本文件

2009-08-12 10:07:51

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-08-19 09:42:52

C#操作Word书签

2009-09-01 11:21:02

C#读取word内容

2009-09-09 18:41:42

C# 加密散列算法

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2010-01-20 14:25:56

函数调用

2010-06-18 14:06:03

AMF协议

2009-08-19 10:46:48

C#操作Word表格
点赞
收藏

51CTO技术栈公众号