using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using System.Reflection; namespace WordAndExcel.Word
{
public partial class makeWord : System.Web.UI.Page
{
Microsoft.Office.Interop.Word.Application appWord = null;
Microsoft.Office.Interop.Word.Document docWord = null;
Range range = null;
object missing = null; protected void Page_Load(object sender, EventArgs e)
{
Document wordDoc = OpenWord(Server.MapPath("./file/law.doc"));
MakeWord(wordDoc, Server.MapPath("./file/laws.doc"));
}
/// <summary>
/// 打开Word
/// </summary>
/// <param name="str_Path">文件路径</param>
/// <returns></returns>
protected Document OpenWord(object str_Path)
{
missing = Missing.Value;
appWord = new Microsoft.Office.Interop.Word.Application();
appWord.Visible = false;
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);
return docWord;
}
/// <summary>
/// 操作Word
/// </summary>
/// <param name="docWord">文档对象</param>
protected void MakeWord(Document docWord, object str_Path)
{
try
{
object startPostion = (docWord.Characters.Count - ) as object;
Range tableLocation = docWord.Range(ref startPostion, ref startPostion); ///文檔對象 從頭開始
///
Microsoft.Office.Interop.Word.Table newTable = docWord.Tables.Add(tableLocation, , , ref missing, ref missing);
///table 樣式
newTable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
newTable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleDashDotDot;
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
newTable.Rows[i + ].Cells[j + ].Width = (float)(255F);
newTable.Rows[i + ].Cells[j + ].Height = (float)(155F);
TableAddText(newTable.Rows[i + ].Cells[j + ].Range);
} }
}
catch (Exception ee)
{
Response.Write(ee.ToString());
}
finally
{ object saveChange = true;
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);
docWord.Close(ref saveChange, ref missing, ref missing);
appWord.Quit(ref saveChange, ref missing, ref missing);
}
} public void TableAddText(Range range)
{
range.Text = "John:(寄)";
// appWord.Selection.Text = "童新:(寄)";
// appWord.Selection.TypeParagraph();
// appWord.Selection.Text = "童新:(收)";
range.Text += "Jerry:(收)";
object Anchor = range; // 插入圖片
string FileName = Server.MapPath("./file/law.jpg");//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
// object Anchor = docWord.Application.Selection.Range;
docWord.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
docWord.Application.ActiveDocument.InlineShapes[].Width = 100f;//图片宽度
docWord.Application.ActiveDocument.InlineShapes[].Height = 20f;//图片高度 //插入Hyperlink
Microsoft.Office.Interop.Word.Selection mySelection = appWord.ActiveWindow.Selection;
mySelection.Start = ;
mySelection.End = ;
Microsoft.Office.Interop.Word.Range myRange = mySelection.Range;
Microsoft.Office.Interop.Word.Hyperlinks myLinks = docWord.Hyperlinks;
object linkAddr = @"http://www.cnblogs.com/tx720/";
Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr,ref missing);
appWord.ActiveWindow.Selection.InsertAfter("\n"); //添加舉行形狀 并且添加文字
Shape s = range.Document.Shapes.AddShape(, , , , , ref Anchor);
s.TextFrame.TextRange.Text = "dds"; }
}
}

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. WordPress的SEO技术

    原文:http://blog.wpjam.com/article/wordpress-seo/ 文章目录[隐藏] 内容为王 页面优化 标题 链接(URL) Meta 标签 语义化 H1 H2 H3 等 ...

  2. 【暑假】[基本数据结构]根据BFS与DFS确定树

    UVa10410 Tree Reconstruction 算法:根据BFS构造pos数组以区分关系,在此基础上对DFS序列操作.注:栈中存父结点,栈顶是最优先的父结点. 代码如下: #include& ...

  3. 多校1005 HDU5785 Interesting (manacher)

    // 多校1005 HDU5785 Interesting // 题意:给你一个串,求相邻两个回文串左边端点*右边端点的和 // 思路:马拉车算出最长回文半径,求一个前缀和,既得到每个点对答案的贡献. ...

  4. C++ 我想这样用(三)

    话接前篇,继续谈在C++环境下使用C风格编程时的注意点: 6.关于原型的声明 在C里,调用一个未声明的函数是允许的,但是在C++里,必须先声明才能调用函数.另外,如果函数的参数是空的,那么在c里面是未 ...

  5. Window下开发React-Native Android步骤

    1.安装Android开发环境 下载并安装JDK 下载并安装Android SDK, Android NDK 启动SDK下面的SDK Manager.exe,安装相关SDK Platform-tool ...

  6. POJ 1511 Invitation Cards (最短路spfa)

    Invitation Cards 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description In the age ...

  7. javascript中字符串的trim功能表达式

    string.replace(/(^\s*)|(\s*$)/g, "") 用来删除行首行尾的空白字符(包括空格.制表符.换页符等等)

  8. ASP.NET MVC中Session以及处理方式

    转载原地址 http://www.cnblogs.com/darrenji/p/3951065.html

  9. TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView

    先上一张图: 这是使用UITextView时用到的iOS7新增加的类:NSTextContainer.NSLayoutManager.NSTextStorage及其相互关系: 这三个新出的类还没有在官 ...

  10. 201. Segment Tree Build

    最后更新 二刷 08-Jan-2017 一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方. 其实重要的3个东西题目已经提供了: 1) ...