WordPlayer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using System.Drawing;
/************************************************************************************************************************************
* * 文件名 :
* * 声明 :
* * 创建者 : Ward
* * 修改者 : Ward
************************************************************************************************************************************/
namespace Common
{
/********************************************************************************************************************************
* * 类名 : WordPlayer
* * 声明 :
* * 创建者 : Ward
* * 修改者 : Ward
********************************************************************************************************************************/
public class WordPlayer
{
#region - 属性 -
private static Microsoft.Office.Interop.Word._Application oWord = null;
private static Microsoft.Office.Interop.Word._Document odoc = null;
private static Microsoft.Office.Interop.Word._Document oDoc
{
get
{
if (odoc == null)
{
odoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
} return odoc;
}
set
{
if (value != null)
{
odoc = value;
}
}
}
private static object Nothing = System.Reflection.Missing.Value;
public enum Orientation
{
横板,
竖板
}
public enum Alignment
{
左对齐,
居中,
右对齐
}
#endregion #region - 添加文档 - #region - 创建并打开一个空的word文档进行编辑 -
public static void OpenNewWordFileToEdit()
{
oDoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
}
#endregion #endregion #region - 创建新Word -
public static bool CreateWord(bool isVisible)
{
try
{
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = isVisible; return true;
}
catch (Exception)
{
return false;
}
}
public static bool CreateWord()
{
return CreateWord(false);
}
#endregion #region - 打开文档 -
public static bool Open(string filePath, bool isVisible)
{
try
{
oWord.Visible = isVisible; object path = filePath;
oDoc = oWord.Documents.Open(ref path,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); return true;
}
catch (Exception)
{
return false;
}
}
#endregion #region - 插入表格 -
public static bool InsertTable(System.Data.DataTable dt, bool haveBorder, double[] colWidths)
{
try
{
object Nothing = System.Reflection.Missing.Value; int lenght = oDoc.Characters.Count - 1;
object start = lenght;
object end = lenght; //表格起始坐标
Microsoft.Office.Interop.Word.Range tableLocation = oDoc.Range(ref start, ref end); //添加Word表格
Microsoft.Office.Interop.Word.Table table = oDoc.Tables.Add(tableLocation, dt.Rows.Count, dt.Columns.Count, ref Nothing, ref Nothing); if (colWidths != null)
{
for (int i = 0; i < colWidths.Length; i++)
{
table.Columns[i + 1].Width = (float)(28.5F * colWidths[i]);
}
} ///设置TABLE的样式
table.Rows.HeightRule = Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast;
table.Rows.Height = oWord.CentimetersToPoints(float.Parse("0.8"));
table.Range.Font.Size = 10.5F;
table.Range.Font.Name = "宋体";
table.Range.Font.Bold = 0;
table.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
table.Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; if (haveBorder == true)
{
//设置外框样式
table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
table.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
//样式设置结束
} for (int row = 0; row < dt.Rows.Count; row++)
{
for (int col = 0; col < dt.Columns.Count; col++)
{
table.Cell(row + 1, col + 1).Range.Text = dt.Rows[row][col].ToString();
}
} return true;
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
finally
{ }
}
public static bool InsertTable(System.Data.DataTable dt, bool haveBorder)
{
return InsertTable(dt, haveBorder, null);
}
public static bool InsertTable(System.Data.DataTable dt)
{
return InsertTable(dt, false, null);
}
#endregion #region - 插入文本 -
public static bool InsertText(string strText, System.Drawing.Font font, Alignment alignment, bool isAftre)
{
try
{
Word.Range rng = oDoc.Content; int lenght = oDoc.Characters.Count - 1;
object start = lenght;
object end = lenght; rng = oDoc.Range(ref start, ref end); if (isAftre == true)
{
strText += "\r\n";
} rng.Text = strText; rng.Font.Name = font.Name;
rng.Font.Size = font.Size;
if (font.Style == FontStyle.Bold) { rng.Font.Bold = 1; } //设置单元格中字体为粗体 SetAlignment(rng, alignment); return true;
}
catch (Exception)
{
return false;
}
} public static bool InsertText(string strText)
{
return InsertText(strText, new System.Drawing.Font("宋体", 10.5F, FontStyle.Bold), Alignment.左对齐, false);
}
#endregion #region - 设置对齐方式 -
private static Microsoft.Office.Interop.Word.WdParagraphAlignment SetAlignment(Range rng, Alignment alignment)
{
rng.ParagraphFormat.Alignment = SetAlignment(alignment);
return SetAlignment(alignment);
}
private static Microsoft.Office.Interop.Word.WdParagraphAlignment SetAlignment(Alignment alignment)
{
if (alignment == Alignment.居中)
{
return Word.WdParagraphAlignment.wdAlignParagraphCenter;
}
else if (alignment == Alignment.左对齐)
{
return Word.WdParagraphAlignment.wdAlignParagraphLeft;
}
else
{ return Word.WdParagraphAlignment.wdAlignParagraphRight; }
}
#endregion #region - 页面设置 -
public static void SetPage(Orientation orientation, double width, double height, double topMargin, double leftMargin, double rightMargin, double bottomMargin)
{
oDoc.PageSetup.PageWidth = oWord.CentimetersToPoints((float)width);
oDoc.PageSetup.PageHeight = oWord.CentimetersToPoints((float)height); if (orientation == Orientation.横板)
{
oDoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape;
} oDoc.PageSetup.TopMargin = (float)(topMargin * 25);//上边距
oDoc.PageSetup.LeftMargin = (float)(leftMargin * 25);//左边距
oDoc.PageSetup.RightMargin = (float)(rightMargin * 25);//右边距
oDoc.PageSetup.BottomMargin = (float)(bottomMargin * 25);//下边距
}
public static void SetPage(Orientation orientation, double topMargin, double leftMargin, double rightMargin, double bottomMargin)
{
SetPage(orientation, 21, 29.7, topMargin, leftMargin, rightMargin, bottomMargin);
}
public static void SetPage(double topMargin, double leftMargin, double rightMargin, double bottomMargin)
{
SetPage(Orientation.竖板, 21, 29.7, topMargin, leftMargin, rightMargin, bottomMargin);
}
#endregion #region - 插入分页符 -
public static void InsertBreak()
{
Word.Paragraph para;
para = oDoc.Content.Paragraphs.Add(ref Nothing);
object pBreak = (int)WdBreakType.wdSectionBreakNextPage;
para.Range.InsertBreak(ref pBreak);
}
#endregion #region - 关闭当前文档 -
public static bool CloseDocument()
{
try
{
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
oDoc.Close(ref doNotSaveChanges, ref Nothing, ref Nothing);
oDoc = null;
return true;
}
catch (Exception)
{
return false;
}
}
#endregion #region - 关闭程序 -
public static bool Quit()
{
try
{
object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
oWord.Quit(ref saveOption, ref Nothing, ref Nothing); return true;
}
catch (Exception)
{
return false;
}
}
#endregion #region - 保存文档 -
public static bool Save(string savePath)
{
return Save(savePath, false);
}
public static bool Save(string savePath, bool isClose)
{
try
{
object fileName = savePath;
oDoc.SaveAs(ref fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); if (isClose)
{
return CloseDocument();
}
return true;
}
catch (Exception)
{
return false;
}
}
#endregion #region - 插入页脚 -
public static bool InsertPageFooter(string text, System.Drawing.Font font, WordPlayer.Alignment alignment)
{
try
{
oWord.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;//页脚
oWord.Selection.InsertAfter(text);
GetWordFont(oWord.Selection.Font, font); SetAlignment(oWord.Selection.Range, alignment); return true;
}
catch (Exception)
{
return false;
}
}
public static bool InsertPageFooterNumber(System.Drawing.Font font, WordPlayer.Alignment alignment)
{
try
{
oWord.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
oWord.Selection.WholeStory();
oWord.Selection.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;
oWord.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument; oWord.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;//页脚
oWord.Selection.TypeText("第"); object page = WdFieldType.wdFieldPage;
oWord.Selection.Fields.Add(oWord.Selection.Range, ref page, ref Nothing, ref Nothing); oWord.Selection.TypeText("页/共");
object pages = WdFieldType.wdFieldNumPages; oWord.Selection.Fields.Add(oWord.Selection.Range, ref pages, ref Nothing, ref Nothing);
oWord.Selection.TypeText("页"); GetWordFont(oWord.Selection.Font, font);
SetAlignment(oWord.Selection.Range, alignment);
oWord.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
return true;
}
catch (Exception)
{
return false;
}
}
#endregion #region - 字体格式设定 -
public static void GetWordFont(Microsoft.Office.Interop.Word.Font wordFont, System.Drawing.Font font)
{
wordFont.Name = font.Name;
wordFont.Size = font.Size;
if (font.Bold) { wordFont.Bold = 1; }
if (font.Italic) { wordFont.Italic = 1; }
if (font.Underline == true)
{
wordFont.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
}
wordFont.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic; if (font.Strikeout)
{
wordFont.StrikeThrough = 1;//删除线
}
}
#endregion #region - 获取Word中的颜色 -
public static WdColor GetWordColor(Color c)
{
UInt32 R = 0x1, G = 0x100, B = 0x10000;
return (Microsoft.Office.Interop.Word.WdColor)(R * c.R + G * c.G + B * c.B);
}
#endregion
}
}
WordPlayer的更多相关文章
- C# /windowForm/WPF/SilverLight里面操作Word帮助类提供给大家
很多的程序都需要用到对word的操作,数据库里面的表需要一书面的形式展示出来,最近在的一个项 using System; using System.Collections.Generic; using ...
随机推荐
- RHEL下修改市区
针对中国时区,修改操作如下 1. 修改文件 /etc/sysconfig/clock内容: ZONE=Asia/ShanghaiUTC=falseARC=false 2. rm /etc/ ...
- Mac MySQL 数据库配置(关系型数据库管理系统)
前言 MySQL 关系型数据库管理系统. 1.配置准备工作 1)配置数据库准备工作 下载相关软件 mysql-5.7.21-1-macos10.13-x86_64.dmg mysql-workbenc ...
- [AaronYang]那天有个小孩跟我说Js正则
按照自己的思路学习Node.Js 随心出发.突破正则冷门知识点,巧妙复习正则常用知识点 标签:AaronYang 茗洋 Node.Js 正则 Javascript 本篇博客地址:http://ww ...
- 提取aar 包中的jar包,反编译再替换成新的aar
参考了 http://blog.csdn.net/hekewangzi/article/details/44676797 针对aar包,增加一些说明 aar包本质应该是zip文件.可以用360解压 ...
- SNF快速开发平台--规则引擎在程序当中如何调用
规则定义完如何在程序当中进行使用呢? 其时很简单,只需要如下代码就可以调用程序: 规则定义: 调用代码: #region 演示2:生成左表数据(规则) POST: /api/DEMO/DemoSing ...
- Fiddler插件开发 - 实现网站离线浏览功能
有这么一种应用场景: 你是做前端或APP开发的,需要调用服务端提供的接口,接口只能在公司内网访问:在公司外就无法调试代码了. 想在公司外访问怎么办呢? 如果在公司的时候将所有接口的响应内容都保存起来, ...
- 【Java】forward & redirect 的差异
1.从地址栏显示来说 forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从哪里来的,所以它的地 ...
- 模仿Semaphore自定义自己的 信号量
简介 这里模仿Semaphore,自定义自己的信号量,利用AQS共享模式 1.MySemaphore.java package com.jacky; import java.util.concurre ...
- 【iCore1S 双核心板_ARM】例程十一:RTC实时时钟实验——显示时间和日期
实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ RTC_DateTypeDef sDate; RTC_TimeTypeDef sTime; u ...
- 稍稍解读下JDK8的HashMap
首先,源码中上来就有一大段注释,但最重要的就是第一句. 大意如下: 本map经常用作一个 binned (bucketed) hash table (下面有解释),但是,当bins很大的时候,它们会被 ...