向Word中输出表格

来源:互联网 发布:网站域名查询 编辑:程序博客网 时间:2024/04/29 11:32

1、首先添加Aspose.Word.dll引用

2、使用using语句包含Aspose.Word

using Aspose.Words;

3、在需要生成表格的地方编写如下代码片段

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 Aspose.Words;namespace AsposeWordTest{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            Document doc = new Document();            DocumentBuilder builder = new DocumentBuilder(doc);            Aspose.Words.Tables.Table table = builder.StartTable();            builder.InsertCell();            builder.Write("1-1");            builder.InsertCell();            builder.Write("1-2");            builder.InsertCell();            builder.Write("1-3");            builder.EndRow();            builder.InsertCell();            builder.Write("2-1");            builder.InsertCell();            builder.Write("2-2");            builder.InsertCell();            builder.Write("2-3");            builder.EndRow();            builder.EndTable();            builder.InsertBreak(BreakType.PageBreak);//插入 分页            table = builder.StartTable();            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;//段落对齐方式            builder.InsertCell();            builder.Write("1-1");            builder.InsertCell();            builder.Write("1-2");            builder.InsertCell();            builder.Write("1-3");            builder.EndRow();            builder.Bold = true;// 字体加粗            builder.InsertCell();            builder.Write("好人");            builder.Font.Size = 17;//字体大小                         builder.InsertCell();            builder.Write("坏人");            builder.Font.Name = "华文隶书";             builder.InsertCell();            //builder.Font = Aspose.Words.Fonts.FontFamily.Swiss            builder.CellFormat.Borders.LineStyle = LineStyle.Single;            builder.CellFormat.Borders.Color = System.Drawing.Color.Black;            builder.Write("坏人");            builder.EndRow();            builder.EndTable();            builder.InsertBreak(BreakType.PageBreak);                        doc.Save("1.doc");        }    }}


在内存中新建Word用参数为空的构造函数,这种方法与XML文档类似。

 Document doc = new Document();
然后构建一个
DocumentBuilder builder = new DocumentBuilder(doc);

DocumentBuilder可以看做真正的程序(程序输入流和控制)。

Document代表文档句柄。


原创粉丝点击