- 浏览: 325106 次
- 性别:
- 来自: 南昌
-
文章分类
最新评论
-
j_bird:
你好,想探讨下滑动窗口是怎么计算的,一条群发短信发出去,滑动窗 ...
协议研发 中移动CMPP2.0协议API -
andyliulin:
楼主,现在的magicode 生成器工具的 官网,http: ...
Mgicode 生成器正式发布 -
huazai_wow:
楼主 你只是分析了 在jquery 中 有使用到 jQu ...
jquery event trigger 分析 -
dengkanghua:
CMPP2.0中出现流量控制错误是什么引起的。有什么解决办法吗 ...
协议研发 中移动CMPP2.0协议API -
JohnHust:
[flash=200,200][/flash][url][/u ...
Jquery源码分析(一)
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 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(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(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) { 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 } }
新建一个窗体工程,拖入一个按钮,复制下面代码进去,按F5运行
private void button2_Click(object sender, EventArgs e) { if (WordPlayer.CreateWord() == false) { MessageBox.Show("文件创造失败.", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DataTable storedt = new DataTable(); storedt.Columns.Add("Book_ISBN"); storedt.Columns.Add("Book_Name"); storedt.Columns.Add("Store_Num"); storedt.Columns.Add("CanBorrow_Num"); storedt.Columns.Add("InShop_Num"); storedt.Columns.Add("OutShop_Num"); storedt.Rows.Add("1", "1", "1", "1", "1", "1"); storedt.Rows.Add("2", "2", "2", "2", "2", "2"); storedt.Rows.Add("3", "3", "3", "3", "3", "3"); storedt.Rows.Add("4", "4", "4", "4", "4", "4"); storedt.Rows.Add("5", "5", "5", "5", "5", "5"); storedt.Rows.Add("6", "6", "6", "6", "6", "6"); WordPlayer.SetPage(WordPlayer.Orientation.横板, 18.4, 26, 3, 2.4, 1.87, 2.1); WordPlayer.InsertText("工 资 变 动 情 况 审 批 表", new Font("微软雅黑", 14, FontStyle.Bold), WordPlayer.Alignment.居中, true); WordPlayer.InsertText("", new Font("微软雅黑", 14, FontStyle.Bold), WordPlayer.Alignment.居中, true); WordPlayer.InsertText("姓名:A 审批单位:广西师范大学", new Font("宋体", 12, FontStyle.Regular), WordPlayer.Alignment.左对齐, false); WordPlayer.InsertTable(storedt, true); WordPlayer.InsertText("制表时间:2007年1月15日", new Font("宋体", 12, FontStyle.Regular), WordPlayer.Alignment.右对齐, false); WordPlayer.Save(Application.StartupPath + "\\test.doc",true); }
相关推荐
根据提供的文件信息,我们可以归纳出两个主要的知识点:一是如何使用C#操作Word文档并插入图片;二是如何利用C#批量替换Word文档中的书签文本。下面将对这两个知识点进行详细解析。 ### 一、使用C#操作Word文档并...
本主题将深入探讨如何使用C#来处理Word文档,包括添加文本、图片和表格,绘制表格,管理书签,删除空白页,以及对文档内容进行格式调整。 1. **添加文本、图片和表格**: 在C#中,可以使用`Microsoft.Office....
在IT领域,C#是一种广泛使用的编程语言,尤其...综上所述,C#操作Word的编辑控件涉及多个层面,从基本的文档创建和文本编辑到更复杂的格式设置和自动化流程。选择合适的方法取决于具体需求,如性能、兼容性和开发效率。
"C# ACCESS 数据库操作类"是一个自定义的C#类,设计用来简化对Access数据库的增、删、改、查等操作。这个类通常封装了ADO.NET的核心组件,如Connection、Command、DataAdapter和DataSet,使得代码更加模块化和易于...
本文将详细介绍如何使用C#进行Word文件的基本操作,包括创建Word文档、添加文本、设置格式、插入表格等。 #### 二、环境搭建与命名空间引入 在开始之前,我们需要确保项目环境中已经安装了Microsoft Office,并且...
本主题聚焦于如何使用C#从Word文档中提取文本,这在数据处理、文档分析或自动化任务中非常常见。以下是关于这个话题的详细说明。 首先,要处理Word文档,你需要使用Microsoft Office Interop库,它允许C#代码与...
本文将深入探讨如何使用C#操作Word,特别是通过通用类实现Word的常见功能,如搜索和替换文本、插入表格、页眉和页脚以及设置单元格样式。 首先,我们需要引入Microsoft.Office.Interop.Word命名空间,这是C#与Word...
在C#编程中,生成Word文件...总的来说,这个C#代码段展示了如何利用Microsoft Office Interop库在C#中根据Word模板动态生成新的Word文件,同时处理表格数据和静态文本替换,适用于批量生成类似格式但内容不同的文档。
在本场景中,我们将探讨如何使用C#和NPOI库来读取和操作Word .docx文件。 首先,NPOI库主要支持两种文件格式:HSSF(用于处理旧版的.xls文件)和XSSF(用于处理.xlsx文件)。对于.docx文件,NPOI提供了一个名为`...
WordOperate(c#word操作类) 对OFFICE WORD进行全面的操作源代码
安装此库后,可以在C#项目中直接引用它,从而调用Word的相关功能。 以下是一个简单的C#代码示例,演示如何打开Word文档并读取其内容: ```csharp using System; using Microsoft.Office.Interop.Word; public ...
它提供了对Word对象模型的访问,使开发者可以创建、修改、格式化Word文档,插入文本、图片,执行宏,以及处理其他复杂的Word功能。在这个场景中,我们将使用它来读取Word模板并生成新的文档。 以下是实现这个功能的...
在实际操作中,我们可能需要处理更多的细节,例如CSS样式、链接、表格等。CSS样式可以通过解析并应用到Word文档的样式表中来保留。链接如果无法本地化,可以考虑将其转换为纯文本或者在Word文档中创建超链接。 批量...
C#的`System.IO`命名空间提供了所有必要的类和方法,如`File`和`Directory`,用于文件和目录的读写操作。 7. **异常处理**:在实际的代码实现中,需要对可能出现的错误情况进行处理,例如文件不存在、权限问题、...
在IT行业中,C#是一种广泛使用的...综上所述,创建一个类word编辑器C#项目涉及多个方面的技术,包括UI设计、文本处理、文件操作、事件驱动编程等。开发过程中需要不断优化和调试,以确保编辑器稳定、高效且功能丰富。
使用C#来操作Word文档不仅可以提高工作效率,还能使应用程序的功能更加完善。本文将详细介绍如何使用C#来调用Word并进行相关操作,包括创建文档、插入表格、设置样式等。 #### 前置准备 要使用C#操作Word文档,...
假设我们有一个预先设计好的Word文档,其中包含一些占位符文本,如"{Name}"、"{Address}"等。我们的目标是将这些占位符替换为实际的数据。以下是基本步骤: 1. **创建Word应用程序对象**:首先,我们需要实例化一个...
总的来说,使用C#和Spire库在WinForm应用中导出Word文件是一项常见任务,通过理解Spire库的基本用法和掌握文档操作技巧,可以轻松实现这一功能。同时,不断学习和实践,你将能够更好地掌握文档处理的高级特性,满足...
c# Excel 操作类,可实现EXCEL的大部分操作,并且可以把数据库查询出的结果datatable直接导到EXCEL里面,注释详细! /// /// 将内存中数据表格插入到Excel指定工作表的指定位置 /// /// 数据表 /// 工作表...
C#版日志操作类,能够自定义日志文件类型,将业务日志、数据日志、异常日志等各类型日志分开记录,自动以日期和日志类型创建日志文本文件,开发时调用 WriteLog 一个方法即可完成日志记录,支持自定义日志存放路径等...