Word控件Spire.Doc 【Table】教程(11): 通过 Spire.Doc 将表格在 Word 文档中的位置设置为外部

Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。

Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=N176https://www.evget.com/product/3368/download

word文档中的表格可以让你的数据更有条理,更整洁,这篇文章讲的是通过Spire.Doc在word文档中设置表格的绝对位置。这里尝试实现在header上的图片右侧放置一个table。

以下是步骤:

第 1 步:创建一个新的 word 文档并添加新的部分。

Document doc = new Document();
Section sec = doc.AddSection();

第 2 步:在 Section[0] 上创建标题。

HeaderFooter header = doc.Sections[0].HeadersFooters.Header;

第 3 步:在页眉上添加新段落,并将段落的HorizontalAlignment设置为左边。

Paragraph paragraph = header.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;

第 4 步:为段落加载图像。

DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png"));

第 5 步:添加一个 4 行 2 列的表格。

Table table = header.AddTable();
table.ResetCells(4, 2);

第 6 步:将表格的位置设置在图像的右侧。设置WrapTextAround为true,HorizPositionAbs为外,VertRelationTo为margin,VertPosition为43以适应图片高度。

table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin; table.TableFormat.Positioning.VertPosition = 43;

第 7 步:然后为表格添加内容,第一列对齐设置为左对齐,第二列对齐设置为右对齐。

String[][] data = {
new string[] {"Spire.Doc.left","Spire XLS.right"},
new string[] {"Spire.Presentatio.left","Spire.PDF.right"},
new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"},
new string []{"Spire.DocViewer.left","Spire.BarCode.right"}
};

for (int r = 0; r < 4; r++)
{
TableRow dataRow = table.Rows[r];
for (int c = 0; c < 2; c++)
{
if (c == 0)
{
Paragraph par = dataRow.Cells[c].AddParagraph();
par.AppendText(data[r][c]);
par.Format.HorizontalAlignment = HorizontalAlignment.Left;
dataRow.Cells[c].Width = 180;
}
else
{
Paragraph par = dataRow.Cells[c].AddParagraph();
par.AppendText(data[r][c]);
par.Format.HorizontalAlignment = HorizontalAlignment.Right;
dataRow.Cells[c].Width = 180;
}
}
}

第 8 步:保存文件并查看。

table.TableFormat.WrapTextAround = true;
table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
table.TableFormat.Positioning.VertPosition = 43;
doc.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");

这是屏幕截图

完整代码

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing;
namespace SetPosition
{
class Program
{

static void Main(string[] args)
{

Document doc = new Document();
Section sec = doc.AddSection();

HeaderFooter header = doc.Sections[0].HeadersFooters.Header;

Paragraph paragraph = header.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png"));

Table table = header.AddTable();
table.ResetCells(4, 2);

table.TableFormat.WrapTextAround = true;
table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
table.TableFormat.Positioning.VertPosition = 43;

String[][] data = {
new string[] {"Spire.Doc.left","Spire XLS.right"},
new string[] {"Spire.Presentatio.left","Spire.PDF.right"},
new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"},
new string []{"Spire.DocViewer.left","Spire.BarCode.right"}
};

for (int r = 0; r < 4; r++)
{
TableRow dataRow = table.Rows[r];
for (int c = 0; c < 2; c++)
{
if (c == 0)
{
Paragraph par = dataRow.Cells[c].AddParagraph();
par.AppendText(data[r][c]);
par.Format.HorizontalAlignment = HorizontalAlignment.Left;
dataRow.Cells[c].Width = 180;
}
else
{
Paragraph par = dataRow.Cells[c].AddParagraph();
par.AppendText(data[r][c]);
par.Format.HorizontalAlignment = HorizontalAlignment.Right;
dataRow.Cells[c].Width = 180;
}
}
}
doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");
}
}
}

以上便是如何通过 Spire.Doc 将表格在 Word 文档中的位置设置为外部,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
免费Spire.Doc for .NET是e-iceblue公司推出的一款专门对Microsoft Word 文档进行操作的.NET控件。这款控件的主要功能在于帮助开发人员轻松快捷地生成、编辑和查看Word文档。同时,开发人员还可以通过使用Spire.Doc for .NET 设置Word文档的格式,插入图片,表格,超链接等。Spire.Doc for .NET 最大的便利之处在于它不依赖于Microsoft Word以及任何其他第三方软件。只需将此款控件安装在您的电脑上,您就可以对word文档进行操作。此款控件支持所有的Word格式,即Word97,Word2003,Word2007, Word2010以及Word2013。 主要功能: • 可生成、打开、查看、编辑以及保存Word文档。 • 可对word文档进行加密、解密、设置权限已达到保护Word文档的效果。 • 可对页面进行设置,包括页眉、页脚的设计,插入分页符等。 • 可对文档格式进行设置,包括字体(大小、颜色等)和段落(行间距、项目符号等)的格式设置。 • 可在word文档插入外部资源,如图片、超链接、水印、表格、批注等。 • 支持Word文档和其他文件格式间的转换。如Word to HTML/XML/PDF/EPub/RTF等。同时,其他格式也可以转换为Word,如HTML/XML/RTF/Text to WordWord不同版本间的格式也可相互转换,即Docx to Doc 或者Doc to Docx. • 支持邮件合并功能。开发人员可使用Spire.Doc for .NET提供的邮件合并功能轻松地完成对报表的批量设计及打印。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值