c# 操作word
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的更多相关文章
- python操作word入门
1.安装pywin32 http://sourceforge.net/projects/pywin32 在files里去找适合你的python版本.截止此文,最新版本是pywin32-219快捷路径: ...
- C#中操作Word(1)—— word对象模型介绍
一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...
- C#操作Word的超详细总结
本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置 ...
- C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- C#操作Word的辅助类(word2003) 修改完善版
转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...
- 黄聪:C#操作Word表格的常见操作(转)
几种常见C#操作Word表格操作有哪些呢?让我们来看看具体的实例演示: bool saveChange = false; //C#操作Word表格操作 object missing = System. ...
- c#操作word表格
http://www.webshu.net/jiaocheng/programme/ASPNET/200804/6499.html <% if request("infoid" ...
- Aspose.Words操作word生成PDF文档
Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...
- OpenXML操作word
OpenXML概述 项目中经常需要操作word,之前的方式是采用COM接口,这个接口很不稳定,经常报错.现在开始采用OpenXML.OpenXML(OOXML)是微软在Office 2007中提出的一 ...
随机推荐
- zabbix统一脚本监控方式
几周的zabbix使用之后几点心得,暂时记在这儿 简单命令监控,直接配置Userparameter参数,以应用来分类conf文件,将不同应用的配置写在不同的conf文件里,并将之放到统一的配置引入目录 ...
- 有趣的Node爬虫,数据导出成Excel
最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...
- DB2 VALUES用法详解
都知道Oracle有一个虚表(dual),我们可以用select sysdate from dual获取寄存器中的值.在DB2中,可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄 ...
- UVALive 7281 Saint John Festival (凸包+O(logn)判断点在凸多边形内)
Saint John Festival 题目链接: http://acm.hust.edu.cn/vjudge/contest/127406#problem/J Description Porto's ...
- [C语言 - 7] 结构体struct
A. 基本知识 与数组的对比 数组: 构造类型 只能有多个相同类型的数据构成 结构体: 结构体类型 可以由多个不同类型的数据构成 1. 定义类型 struct Student { int ...
- VC 各种情况下的窗口句柄的获取
动窗口的句柄.否则,返回值为NULL. GetSafeHwnd 函数功能:获取某个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数. 通过下面的 ...
- Eclipse查找类路径快捷方式
直接ctrl+shift+t查找这个类,下面会显示类的路径,包括jar名
- [转]Oracle关于null的处理
转至:http://www.2cto.com/database/201209/157606.html 关于空值null的排序问题 Oracle排序中NULL值处理的五种常用方法: 1.缺省O ...
- baseadapter.getItemId的使用方法:实现listview筛选、动态删除
转载:http://www.lai18.com/content/1631131.html 这里的listview筛选是指listview的adapter实现filter来过滤数据. “动态删除&quo ...
- 山东理工大学ACM平台题答案关于C语言 1580 闰年
闰年 Time Limit: 1000ms Memory limit: 32768K 有疑问?点这里^_^ 题目描述 时间过得真快啊,又要过年了,同时,我们的人生也增长了一年的阅历,又成熟了一些 ...