使用c#在word文档中创建表格的方法详解

代码如下:

public string CreateWordFile()
        {
            string message = "";
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                string name = "xiehuan.doc";
                object filename = @"C:\Users\xiehuan\xxx\" + name;  //文件保存路径
                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                //添加页眉
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距
                //移动焦点并换行
                object count = 14;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                 WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                 WordApp.Selection.TypeParagraph();//插入段落
                 //文档中创建表格
                 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);
                 //设置表格样式
                 newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                 newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                 newTable.Columns[1].Width = 100f;
                 newTable.Columns[2].Width = 220f;
                 newTable.Columns[3].Width = 105f;
                 //填充表格内容
                 newTable.Cell(1, 1).Range.Text = "产品详细信息表";
                 newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                 //合并单元格
                 newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

//填充表格内容
                 newTable.Cell(2, 1).Range.Text = "产品基本信息";
                 newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
                 //合并单元格
                 newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                  //填充表格内容
                  newTable.Cell(3, 1).Range.Text = "品牌名称:";
                  newTable.Cell(3, 2).Range.Text = "BrandName";
                  //纵向合并单元格
                  newTable.Cell(3, 3).Select();//选中一行
                  object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                  object moveCount = 5;
                  object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                   WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                   WordApp.Selection.Cells.Merge();
                   //插入图片
                   string FileName = Picture;//图片所在路径
                   object LinkToFile = false;
                   object SaveWithDocument = true;
                   object Anchor = WordDoc.Application.Selection.Range;
                   WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                    WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
                    WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
                    //将图片设置为四周环绕型
                    Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                    s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

newTable.Cell(12, 1).Range.Text = "产品特殊属性";
                    newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                     //在表格中增加行
                     WordDoc.Content.Tables[1].Rows.Add(ref Nothing);

WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                     WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
                    //文件保存
                    WordDoc.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);
                    WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                    WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                    message=name+"文档生成成功,以保存到C:CNSI下";
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return message;
        }

(0)

相关推荐

  • C#根据Word模版生成Word文件

    本文实例为大家分享了C#根据Word模版生成Word文的具体代码,供大家参考,具体内容如下 1.指定的word模版 2.生成word类 添加com Microsoft word 11.0 Object Library 引用 using System; using System.Collections.Generic; using System.Data; using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word;

  • C#实现通过模板自动创建Word文档的方法

    本文实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧.分享给大家供大家参考.具体实现方法如下: 引言:前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! 现通过具体的示例演示具体的步骤:   第一步,制作模板   1.新建一个文档,设置文档内容. 2.在相应位置插入书签:将鼠标定位到要插入书签的位置,点击"插入">"书签&quo

  • C# WORD操作实现代码

    1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC. 2.定制WORD文档的模板文件,在C#中操作WORD模板,生成新的WORD文档. 第一方案简单,只需要改动文件的扩展名就行了,但是也存在了一些问题,譬如生成的WORD文档样式的丢失.这样对于客户来说可能是一个无法通过的方案.第二方案比较复杂,需要调用OFFICE的WORD组件通过C#来操作WORD,进而生成WORD.此方法类似于我们在c#中的后台拼接数据.虽然麻烦,但是能够灵活定制,只不过是操作WORD对象而已.

  • c#开发word批量转pdf源码分享

    微软Office Word本身已经提供了另存为PDF文档功能,对于少量文档,手工使用该方式进行Word转换为PDF尚可,一旦需要处理大量的文档,可能就显得有些捉襟见肘了.不过对于已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF了. 源码: 复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.D

  • word ppt excel文档转换成pdf的C#实现代码

    复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; using Word = Microsoft.Office.Interop.Word;using Excel = Micro

  • 比较全的一个C#操作word文档示例

    最近两天研究了一下如何使用VS2008(C#语言)输出Word文档.以下是几点总结: 1.非常简单. 2.开发及运行环境要求.操作系统为:WindowsXP(安装.net framework2.0)/Vista/Win7:在操作系统必须安装Word2003完全安装版.这里必须要强调是Word2003完全安装版,因为软件开发及运行都需要一个com组件:Microsoft word 11.0 Object Library.如果不是Word2003完全安装版,可以下载这个com组件,并手动的安装这个c

  • C# 利用Aspose.Words.dll将 Word 转成PDF

    只要把aspose.words.dll 在bin中添加引用即可. 复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Aspose.Words;using Aspose.Words.Saving;usi

  • C# Word 类库的深入理解

    代码如下所示: 复制代码 代码如下: using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.IO;using System.Web;using System.Data;using System.Reflection;using Microsoft.Win32;using System.Text.RegularExpressio

  • JavaScript打开word文档的实现代码(c#)

    在C#中打开word文档其实不算太难,方法也比较多. 一.C#中打开word文档方法 复制代码 代码如下: //在项目引用里添加上对Microsoft Word 11.0 object library的引用 private void button1_Click(object sender, System.EventArgs e) { //调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称 OpenFileDialog opd = new OpenFileDialog()

  • asp.net(c#)下读取word文档的方法小结

    第一种方法: 复制代码 代码如下: Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "Application/msword"; string s=Server.MapPath("C#语言参考.doc"); Response.WriteFile("C#语言参考.doc"); Response.Write(s); Response.Flush(); Re

随机推荐