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. bzoj 3505 [Cqoi2014]数三角形(组合计数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3505 [题意] 在n个格子中任选3点构成三角形的方案数. [思路] 任选3点-3点共线 ...

  2. SQL Server 2000的并发连接数是多少

    开始->管理工具->性能(或者是运行里面输入 mmc)然后通过 添加计数器添加 SQL 的常用统计(MSSQL General Statistics) 然后在下面列出的项目里面选择 用户连 ...

  3. 优雅地对泛型List 进行深拷贝

    public class People { public string Name; public int Age; public People(string name, int age) { this ...

  4. Longest Increasing Sequence

    public class Longest_Increasing_Subsequence { /** * O(N^2) * DP * 思路: * 示例:[1,0,2,4,10,5] * 找出以上数组的L ...

  5. 《Java数据结构与算法》笔记-CH4-1栈的实现

    class StackX{ private int maxSize; private long[] stackArray; private int top; public StackX(int siz ...

  6. AHOI2013 Round2 Day2 简要题解

    第一题: 第一问可以用划分树或主席树在O(nlog2n)内做出来. 第二问可以用树状数组套主席树在O(nlog2n)内做出来. 我的代码太挫了,空间刚刚卡过...(在bzoj上) 第二题: 分治,将询 ...

  7. 【转】Maven实战(五)---两个war包的调用

    原博文出自于: http://blog.csdn.net/liutengteng130/article/details/42879803    感谢! 开篇前提   1.为什么要用两个war包的调用? ...

  8. ocp 1Z0-042 61-120题解析

    61. View the Exhibit.Which statement regarding the dept and emp tables is true?A) When you delete a ...

  9. HDU 3072 Intelligence System (强连通分量)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  10. HTTP协议状态码详解

    HTTP状态码,我都是现查现用. 我以前记得几个常用的状态码,比如200,302,304,404, 503. 一般来说我也只需要了解这些常用的状态码就可以了.  如果是做AJAX,REST,网络爬虫, ...