




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Drawing;using Word = Microsoft.Office.Interop.Word;using Microsoft.Office.Interop.Word;using System.Windows.Forms;/* * 文件名 : * * 声明: * * 创建者 : 黄聪* * 创建日期 : 2009.10.8* * 修改者 : 黄聪* * 最新修改日期 : 2009.10.8*/namespace Tool/* * 类名 : WordPlayer* * 声明 : * * 创建者 : 黄聪* * 创建日期 : 2009.7.15* * 修改者 : 黄聪* * 最新修改日期 : 2009.7.15*/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 oDocgetif (odoc = null)odoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);return odoc;setif (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)tryoWord = 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)tryoWord.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(DataTable dt, bool haveBorder, double colWidths)tryobject 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.Columnsi + 1.Width = (float)(28.5F * colWidthsi);/设置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.Rowsrowcol.ToString();return true;catch (Exception e)MessageBox.Show(e.ToString(), 错误提示, MessageBoxButtons.OK, MessageBoxIcon.Error);return false;finallypublic static bool InsertTable(DataTable dt, bool haveBorder)return InsertTable(dt, haveBorder, null);public static bool InsertTable(DataTable dt)return InsertTable(dt, false, null);#endregion#region - 插入文本 -public static bool InsertText(string strText, System.Drawing.Font font, Alignment alignment, bool isAftre)tryWord.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 += rn;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()tryobject 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()tryobject 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)tryobject 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)tryoWord.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)tryoWord.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;oWord.Selection.WholeStory();oWord.Selection.ParagraphFormat.BordersWdBorderType.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新建一个窗体工程,拖入一个按钮,复制下面代码进去,按F5运行private void button2_Click(object sender, EventArgs e)if (WordPlayer.CreateWord() = false)MessageBox.Show(文件创造失败., 错误提示, MessageBoxButtons.OK, MessageBoxIcon.Error);return;DataTa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年商务师职业资格考试题库:商务创业与市场分析试题
- 2025年消防行业从业资格考试题库:职业道德与消防安全知识应用能力测试
- 空调安装与维护技术规范
- 城市商业设施运维协议
- 2025年机械安全操作规范试题库(机械自动化工程师认证)试卷
- 中考数学模拟试卷(山西2018版)
- 住宅工程质量问题及解决方案
- 2025年初中地理学业水平考试模拟卷:地理实验探究与难点解析
- 中医合理膳食调养指导手册
- 财务报销申请单填写及审核标准
- 2025年国家能源集团宁夏煤业有限责任公司招聘笔试考试题库+答案
- 父母情+养育恩-2025-2026学年高二上学期感恩教育主题班会
- 2025年物流行业审核合规性提升方案
- 高校实验室安全基础(华东理工大学)学习通网课章节测试答案
- 女生青春期性教育核心知识框架
- 日常膝关节护理
- 初中音标考试题及答案大全人教版
- 新能源汽车火灾事故成因分析及灭火救援措施
- 2024北京陈经纶中学高二10月月考语文试题及答案
- 中兴信息安全管理制度
- 轮胎店转让协议书
评论
0/150
提交评论