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中提出的一 ...
随机推荐
- Emmet:HTML/CSS代码快速编写神器(转)
Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...
- 二分+叉积判断方向 poj 2318 2398
// 题意:问你每个区域有多少个点 // 思路:数据小可以直接暴力 // 也可以二分区间 #include <cstdio> #include <cstring> #inclu ...
- Language Basics:语言基础
Java包含多种变量类型:Instance Variables (Non-Static Fields)(实例变量):是每个对象特有的,可以用来区分各个实例Class Variables (Static ...
- struts2框架开发的第一个应用
写这篇博文,主要是帮助那些刚接触struts2框架开发而不知所措的人,希望批评指正 一.先建立一个web project,命名为struts2 二.在webroot/WEB-INF/lib目录下添加如 ...
- HDU 5510 Bazinga (2015沈阳现场赛,子串判断)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- java volatile进阶(一)
本篇文章继续学习volatile.上篇文章简单的介绍了volatile和synchonized,这篇文章讲一下什么时候可以用volatile. 先看一段代码. package com.chzhao.v ...
- C#枚举数值与名称的转换
在应用枚举的时候,时常需要将枚举和数值相互转换的情况.有时候还需要转换成相应的中文.下面介绍一种方法. 首先建立一个枚举: /// <summary> /// 颜色 /// </su ...
- Oracle & Sun
2010s January 27, 2010: Oracle acquires Sun Microsystems.
- ASP.NET MVC- 使用PageList.Mvc分页
ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList.PagedList.Mvc进行分页. 1. 通过NuGet引用PagedList.Mvc 在安装引用Paged ...
- 下载Xml文件方法
#region 下载Xml文件方法 //定义委托 private delegate void DownLoadDelegate(string url, string filename); privat ...