Word控件Spire.Doc 【表单域】教程(二):在 C# 中填写 Word 文档中的表单字段

我们已经演示了如何创建表单域。本文主要向您展示开发人员如何使用独立的.NET Word 组件 Spire.Doc仅通过 4 个简单的步骤在 C# 中填写 Word 文档中的表单字段。

Spire.Doc for.NET 最新下载 

欢迎下载|体验更多E-iceblue产品 技术交流Q群(767755948)

确保 Spire.Doc for .NET 已正确安装,然后在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考,路径如下:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc。 dll”。以下是开发人员如何使用 Spire.Doc 填写表单字段的详细信息:

第一步:打开需要填写数据的表格。

[C#]

//Create word document
Document document = new Document(@"..\..\..\Data\UserForm.doc");

第 2 步:加载将填写表格的数据。

[C#]

//Fill data from XML file
using (Stream stream = File.OpenRead(@"..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");

第 3 步:使用加载的数据填写表格。

[C#]

//fill data
foreach (FormField field in document.Sections[0].Body.FormFields)
{
String path = String.Format("{0}/text()", field.Name);
XPathNavigator propertyNode = user.SelectSingleNode(path);
if (propertyNode != null)
{
switch (field.Type)
{
case FieldType.FieldFormTextInput:
field.Text = propertyNode.Value;
break;

case FieldType.FieldFormDropDown:
DropDownFormField combox = field as DropDownFormField;
for(int i = 0; i < combox.DropDownItems.Count; i++)
{
if (combox.DropDownItems[i].Text == propertyNode.Value)
{
combox.DropDownSelectedIndex = i;
break;
}
if (field.Name == "country" && combox.DropDownItems[i].Text == "Others")
{
combox.DropDownSelectedIndex = i;
}
}
break;

case FieldType.FieldFormCheckBox:
if (Convert.ToBoolean(propertyNode.Value))
{
CheckBoxFormField checkBox = field as CheckBoxFormField;
checkBox.Checked = true;
}
break;
}
}
}
}

第 4 步:将文档保存为 XML 或 Microsoft Word 格式的文件。

[C#]

//Save doc file
document.SaveToFile("Sample.doc",FileFormat.Doc);

有效截图

填写表单域的完整源代码:

[C#]

namespace FillFormField
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//open form
Document document = new Document(@"..\..\..\..\..\..\Data\UserForm.doc");

//load data
using (Stream stream = File.OpenRead(@"..\..\..\..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");

//fill data
foreach (FormField field in document.Sections[0].Body.FormFields)
{
String path = String.Format("{0}/text()", field.Name);
XPathNavigator propertyNode = user.SelectSingleNode(path);
if (propertyNode != null)
{
switch (field.Type)
{
case FieldType.FieldFormTextInput:
field.Text = propertyNode.Value;
break;

case FieldType.FieldFormDropDown:
DropDownFormField combox = field as DropDownFormField;
for(int i = 0; i < combox.DropDownItems.Count; i++)
{
if (combox.DropDownItems[i].Text == propertyNode.Value)
{
combox.DropDownSelectedIndex = i;
break;
}
if (field.Name == "country" && combox.DropDownItems[i].Text == "Others")
{
combox.DropDownSelectedIndex = i;
}
}
break;

case FieldType.FieldFormCheckBox:
if (Convert.ToBoolean(propertyNode.Value))
{
CheckBoxFormField checkBox = field as CheckBoxFormField;
checkBox.Checked = true;
}
break;
}
}
}
}

//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);

//Launching the MS Word file.
WordDocViewer("Sample.doc");
}

private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}

}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
首先机子上安装有office,在COM添加引用Microsoft.Word.11.0.Object.Library(或11.0以上) Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率 myWordApp = new Microsoft.Office.Interop.Word.Application(); doc = myWordApp.Documents.Open(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); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; for (int i = 2; i < 102; i++)//举例100台 { doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行 doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号 doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号 doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数 doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 } doc.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); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印,文档不显示 //doc.PrintOut(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, ref Nothing, ref Nothing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值