1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using Microsoft.Office.Interop.Word;
  8. using System.Reflection;
  9.  
  10. namespace WordAndExcel.Word
  11. {
  12. public partial class makeWord : System.Web.UI.Page
  13. {
  14. Microsoft.Office.Interop.Word.Application appWord = null;
  15. Microsoft.Office.Interop.Word.Document docWord = null;
  16. Range range = null;
  17. object missing = null;
  18.  
  19. protected void Page_Load(object sender, EventArgs e)
  20. {
  21. Document wordDoc = OpenWord(Server.MapPath("./file/law.doc"));
  22. MakeWord(wordDoc, Server.MapPath("./file/laws.doc"));
  23. }
  24. /// <summary>
  25. /// 打开Word
  26. /// </summary>
  27. /// <param name="str_Path">文件路径</param>
  28. /// <returns></returns>
  29. protected Document OpenWord(object str_Path)
  30. {
  31. missing = Missing.Value;
  32. appWord = new Microsoft.Office.Interop.Word.Application();
  33. appWord.Visible = false;
  34. docWord = appWord.Documents.Open(ref str_Path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  35. return docWord;
  36. }
  37. /// <summary>
  38. /// 操作Word
  39. /// </summary>
  40. /// <param name="docWord">文档对象</param>
  41. protected void MakeWord(Document docWord, object str_Path)
  42. {
  43. try
  44. {
  45. object startPostion = (docWord.Characters.Count - ) as object;
  46. Range tableLocation = docWord.Range(ref startPostion, ref startPostion); ///文檔對象 從頭開始
  47. ///
  48. Microsoft.Office.Interop.Word.Table newTable = docWord.Tables.Add(tableLocation, , , ref missing, ref missing);
  49. ///table 樣式
  50. newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
  51. newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
  52. for (int i = ; i < ; i++)
  53. {
  54. for (int j = ; j < ; j++)
  55. {
  56. newTable.Rows[i + ].Cells[j + ].Width = (float)(255F);
  57. newTable.Rows[i + ].Cells[j + ].Height = (float)(155F);
  58. TableAddText(newTable.Rows[i + ].Cells[j + ].Range);
  59. }
  60.  
  61. }
  62. }
  63. catch (Exception ee)
  64. {
  65. Response.Write(ee.ToString());
  66. }
  67. finally
  68. {
  69.  
  70. object saveChange = true;
  71. docWord.SaveAs(ref str_Path, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
  72. docWord.Close(ref saveChange, ref missing, ref missing);
  73. appWord.Quit(ref saveChange, ref missing, ref missing);
  74. }
  75. }
  76.  
  77. public void TableAddText(Range range)
  78. {
  79. range.Text = "John:(寄)";
  80. // appWord.Selection.Text = "童新:(寄)";
  81. // appWord.Selection.TypeParagraph();
  82. // appWord.Selection.Text = "童新:(收)";
  83. range.Text += "Jerry:(收)";
  84. object Anchor = range;
  85.  
  86. // 插入圖片
  87. string FileName = Server.MapPath("./file/law.jpg");//图片所在路径
  88. object LinkToFile = false;
  89. object SaveWithDocument = true;
  90. // object Anchor = docWord.Application.Selection.Range;
  91. docWord.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
  92. docWord.Application.ActiveDocument.InlineShapes[].Width = 100f;//图片宽度
  93. docWord.Application.ActiveDocument.InlineShapes[].Height = 20f;//图片高度
  94.  
  95. //插入Hyperlink
  96. Microsoft.Office.Interop.Word.Selection mySelection = appWord.ActiveWindow.Selection;
  97. mySelection.Start = ;
  98. mySelection.End = ;
  99. Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;
  100. Microsoft.Office.Interop.Word.Hyperlinks myLinks = docWord.Hyperlinks;
  101. object linkAddr = @"http://www.cnblogs.com/tx720/";
  102. Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,ref missing);
  103. appWord.ActiveWindow.Selection.InsertAfter("\n");
  104.  
  105. //添加舉行形狀 并且添加文字
  106. Shape s = range.Document.Shapes.AddShape(, , , , , ref Anchor);
  107. s.TextFrame.TextRange.Text = "dds";
  108.  
  109. }
  110. }
  111. }

c# 操作word的更多相关文章

  1. python操作word入门

    1.安装pywin32 http://sourceforge.net/projects/pywin32 在files里去找适合你的python版本.截止此文,最新版本是pywin32-219快捷路径: ...

  2. C#中操作Word(1)—— word对象模型介绍

    一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...

  3. C#操作Word的超详细总结

    本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...

  4. C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  5. C#操作Word的辅助类(word2003) 修改完善版

    转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...

  6. 黄聪:C#操作Word表格的常见操作(转)

    几种常见C#操作Word表格操作有哪些呢?让我们来看看具体的实例演示: bool saveChange = false; //C#操作Word表格操作 object missing = System. ...

  7. c#操作word表格

    http://www.webshu.net/jiaocheng/programme/ASPNET/200804/6499.html <% if request("infoid" ...

  8. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  9. OpenXML操作word

    OpenXML概述 项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错.现在开始采用OpenXML.OpenXML(OOXML)是微软在Office 2007中提出的一 ...

随机推荐

  1. Emmet:HTML/CSS代码快速编写神器(转)

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  2. 二分+叉积判断方向 poj 2318 2398

    // 题意:问你每个区域有多少个点 // 思路:数据小可以直接暴力 // 也可以二分区间 #include <cstdio> #include <cstring> #inclu ...

  3. Language Basics:语言基础

    Java包含多种变量类型:Instance Variables (Non-Static Fields)(实例变量):是每个对象特有的,可以用来区分各个实例Class Variables (Static ...

  4. struts2框架开发的第一个应用

    写这篇博文,主要是帮助那些刚接触struts2框架开发而不知所措的人,希望批评指正 一.先建立一个web project,命名为struts2 二.在webroot/WEB-INF/lib目录下添加如 ...

  5. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  6. java volatile进阶(一)

    本篇文章继续学习volatile.上篇文章简单的介绍了volatile和synchonized,这篇文章讲一下什么时候可以用volatile. 先看一段代码. package com.chzhao.v ...

  7. C#枚举数值与名称的转换

    在应用枚举的时候,时常需要将枚举和数值相互转换的情况.有时候还需要转换成相应的中文.下面介绍一种方法. 首先建立一个枚举: /// <summary> /// 颜色 /// </su ...

  8. Oracle & Sun

    2010s January 27, 2010: Oracle acquires Sun Microsystems.

  9. ASP.NET MVC- 使用PageList.Mvc分页

    ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList.PagedList.Mvc进行分页. 1. 通过NuGet引用PagedList.Mvc 在安装引用Paged ...

  10. 下载Xml文件方法

    #region 下载Xml文件方法 //定义委托 private delegate void DownLoadDelegate(string url, string filename); privat ...