public class WordUtility
{
private _Application wordApp = null; private _Document wordDoc = null; public _Application Application
{
get
{
return wordApp;
}
set
{
wordApp = value;
}
} public _Document Document
{
get
{ return wordDoc; }
set
{
wordDoc = value;
}
} //通过模板创建新文档 public void CreateNewDocument(string filePath)
{ killWinWordProcess(); wordApp = new Application(); wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone; wordApp.Visible = false; object missing = System.Reflection.Missing.Value; object templateName = filePath; wordDoc = wordApp.Documents.Add(ref templateName, 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); } private void HideFile(String path)
{
try
{
FileAttributes fa = FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System; File.SetAttributes(path, fa);
}
catch
{
}
} //保存新文件 public void SaveDocument(string filePath)
{ object fileName = filePath; object format = WdSaveFormat.wdFormatDocument;//保存格式 object miss = System.Reflection.Missing.Value; wordDoc.SaveAs(ref fileName, ref format, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); //关闭wordDoc,wordApp对象 object SaveChanges = WdSaveOptions.wdSaveChanges; object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat; object RouteDocument = false; wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument); } /// <summary>
/// 获取所有的书签
/// </summary>
/// <returns></returns>
public Bookmarks GetDocBookmarks()
{
return wordApp.ActiveDocument.Bookmarks;
} public void SetText(object markName, String text)
{
try
{
Bookmark mark = wordDoc.Bookmarks.get_Item(ref markName);
if (mark != null)
{
mark.Range.Text = text;
//wordApp.Selection.TypeText(text);
}//wordDoc.Bookmarks.get_Item(ref markName).Range.Text = text;
}
catch
{
}
} //在书签处插入值 public bool InsertValue(string bookmark, string value)
{
object bkObj = bookmark; if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
{ wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select(); wordApp.Selection.TypeText(value); return true; } return false;
} //插入表格,bookmark书签 public Table InsertTable(string bookmark, int rows, int columns, float width)
{ object miss = System.Reflection.Missing.Value; object oStart = bookmark; Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//表格插入位置 Table newTable = wordDoc.Tables.Add(range, rows, columns, ref miss, ref miss); //设置表的格式 newTable.Borders.Enable = 1; //允许有边框,默认没有边框(为0时报错,1为实线边框,2、3为虚线边框,以后的数字没试过) newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;//边框宽度 if (width != 0)
{ newTable.PreferredWidth = width;//表格宽度 } newTable.AllowPageBreaks = false; return newTable; } //合并单元格 表名,开始行号,开始列号,结束行号,结束列号 public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2)
{ table.Cell(row1, column1).Merge(table.Cell(row2, column2)); } //设置表格内容对齐方式Align水平方向,Vertical垂直方向(左对齐,居中对齐,右对齐分别对应Align和Vertical的值为-1,0,1) public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, int Align, int Vertical)
{ switch (Align)
{ case -1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; break;//左对齐 case 0: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; break;//水平居中 case 1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break;//右对齐 } switch (Vertical)
{ case -1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop; break;//顶端对齐 case 0: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; break;//垂直居中 case 1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom; break;//底端对齐 } } //设置表格字体 public void SetFont_Table(Microsoft.Office.Interop.Word.Table table, string fontName, double size)
{ if (size != 0)
{ table.Range.Font.Size = Convert.ToSingle(size); } if (fontName != "")
{ table.Range.Font.Name = fontName; } } //是否使用边框,n表格的序号,use是或否 public void UseBorder(int n, bool use)
{ if (use)
{ wordDoc.Content.Tables[n].Borders.Enable = 1; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过) } else
{ wordDoc.Content.Tables[n].Borders.Enable = 2; //允许有边框,默认没有边框(为0时无边框,1为实线边框,2、3为虚线边框,以后的数字没试过) } } //给表格插入一行,n表格的序号从1开始记 public void AddRow(int n)
{ object miss = System.Reflection.Missing.Value; wordDoc.Content.Tables[n].Rows.Add(ref miss); } //给表格添加一行 public void AddRow(Microsoft.Office.Interop.Word.Table table)
{ object miss = System.Reflection.Missing.Value; table.Rows.Add(ref miss); } //给表格插入rows行,n为表格的序号 public void AddRow(int n, int rows)
{ object miss = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n]; for (int i = 0; i < rows; i++)
{ table.Rows.Add(ref miss); } } //给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素 public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column, string value)
{ table.Cell(row, column).Range.Text = value; } //给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素 public void InsertCell(int n, int row, int column, string value)
{ wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value; } //给表格插入一行数据,n为表格的序号,row行号,columns列数,values插入的值 public void InsertCell(int n, int row, int columns, string[] values)
{ Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n]; for (int i = 0; i < columns; i++)
{ table.Cell(row, i + 1).Range.Text = values[i]; } } //插入图片 public void InsertPicture(string bookmark, string picturePath, float width, float hight)
{ object miss = System.Reflection.Missing.Value; object oStart = bookmark; Object linkToFile = false; //图片是否为外部链接 Object saveWithDocument = true; //图片是否随文档一起保存 object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;//图片插入位置 wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range); wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width; //设置图片宽度 wordDoc.Application.ActiveDocument.InlineShapes[1].Height = hight; //设置图片高度 } public void InsertPicture(string bookmark, string picturePath)
{ object miss = System.Reflection.Missing.Value; object oStart = bookmark; Object linkToFile = false; //图片是否为外部链接 Object saveWithDocument = true; //图片是否随文档一起保存 object range = wordDoc.Bookmarks.get_Item(ref oStart).Range; wordDoc.Bookmarks.get_Item(ref oStart).Select();//图片插入位置 InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture(picturePath,
ref linkToFile, ref saveWithDocument, ref range);
//点位信息表格式调整,局部图像及位置略图调整大小为245*245,实地照片大小调整为245*185 if (bookmark == "局部图像")
{
inlineShape.Width = 180;// 180;
inlineShape.Height = 180; //210;
}
else if (bookmark == "位置略图")
{
inlineShape.Width = 180;// 120;
inlineShape.Height = 180; //150;
}
else
{
inlineShape.Width = 180;
inlineShape.Height = 120;// 210;
}
} //插入一段文字,text为文字内容 public void InsertText(string bookmark, string text)
{ object oStart = bookmark; object range = wordDoc.Bookmarks.get_Item(ref oStart).Range; Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range); wp.Format.SpaceBefore = 6; wp.Range.Text = text; wp.Format.SpaceAfter = 24; wp.Range.InsertParagraphAfter(); wordDoc.Paragraphs.Last.Range.Text = "\n"; } //杀掉winword.exe进程 public void killWinWordProcess()
{
try
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD"); foreach (System.Diagnostics.Process process in processes)
{ bool b = process.MainWindowTitle == ""; if (process.MainWindowTitle == "")
{ process.Kill(); } }
}
catch (Exception ex)
{
}
} }

word操作类的更多相关文章

  1. 【小丸类库系列】Word操作类

    using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.Dr ...

  2. C# 字符串操作类

    using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...

  3. C# 文件操作类大全

      C# 文件操作类大全 时间:2015-01-31 16:04:20      阅读:1724      评论:0      收藏:0      [点我收藏+] 标签: 1.创建文件夹 //usin ...

  4. Aspose.Words 自定义文档模版生成操作类

    /// <summary> /// 操作word通用类 LIYOUMING add 2017-12-27 /// </summary> public class DocHelp ...

  5. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  6. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~

    最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...

  7. [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc

    开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...

  8. JQuery操作类数组的工具方法

    JQuery学习之操作类数组的工具方法 在很多时候,JQuery的$()函数都返回一个类似数据的JQuery对象,例如$('div')将返回div里面的所有div元素包装的JQuery对象.在这中情况 ...

  9. Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)

    今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...

  10. Util应用程序框架公共操作类(九):Lambda表达式扩展

    上一篇对Lambda表达式公共操作类进行了一些增强,本篇使用扩展方法对Lambda表达式进行扩展. 修改Util项目的Extensions.Expression.cs文件,代码如下. using Sy ...

随机推荐

  1. mysql 批量有则修改,无则新增

    需要为表添加唯一索引 alter table tb_*** add unique index(aa,bb); -- 此条为联合唯一索引INSERT INTO<include refid=&quo ...

  2. LeetCode 650. 2 Keys Keyboard(只有两个键的键盘)(DP/质因数分解)

    最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). Paste (粘贴) : ...

  3. SMMU中stage1 和stage2 的意思

    ARM SMMU(System Memory Management Unit)是一种用于ARM架构的内存管理单元,它支持两阶段的地址转换机制,即Stage 1和Stage 2.这种机制允许操作系统和虚 ...

  4. Java日期时间API系列23-----Jdk8中java.time包中的新的日期时间API类,获取准确开始时间00:00:00,获取准确结束时间23:59:59等

    有时候,往往需要统计某个时间区间的销量等问题,这就需要准确的起始时间,获取准确开始时间00:00:00,获取准确结束时间23:59:59.下面增加了一一些方法,获取当天起始时间,昨天起始时间,当前月第 ...

  5. 探索 PCI 转 PMC 载板转接卡:连接不同接口的桥梁

    探索 PCI 转 PMC 载板转接卡:连接不同接口的桥梁 在计算机硬件领域,各种接口和总线标准不断演进,以满足日益增长的性能和功能需求.在这个过程中,不同接口之间的转换设备应运而生,其中 PCI 转 ...

  6. KubeSphere 社区双周报 | OpenFunction v1.0.0 发布 | 2023.03.03-03.16

    KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...

  7. Minecraft小说

    小说标题:<方块与冒险:勇者的传说> 持续更新中 故事简介: 主角艾伦(Alan)是一个普通的玩家,偶然之间被传送到Minecraft的世界中.这个世界充满了各种各样的奇迹.冒险.危险和谜 ...

  8. 网页设计中常用的Web英文安全字体

    原文地址:https://www.openkee.com/post-176.html 在 Web 编码中,CSS 默认应用的 Web 字体是有限的,你能看到的字体别人未必看得到.虽然在新版本的CSS3 ...

  9. vue搜索历史记录缓存实现

    思路: 1.浏览器缓存永久保存搜索历史数据. 2.页面初始化将数据保存到页面变量中. 3.对搜索历史记录的怎加和删除,要同步到缓存中. ----------------直接看代码----------- ...

  10. npm安装html2canvas依赖报错 npm ERR! Unexpected token < in JSON at position 0 while parsing near '<!DOCTYPE html> npm ERR! <htm...'

    今天安装某个依赖时发现npm ERR! 可我是正常操作啊,也没有升级啥的,咋就安装不了了? npm install --save html2canvas 报错信息如下: npm ERR! Unexpe ...