翻譯|使用教程|編輯:胡濤|2022-04-24 11:20:13.273|閱讀 241 次
概述:本文主要向您展示開發人員如何使用獨立的.NET Word 組件 Spire.Doc僅通過 4 個簡單的步驟在 C# 中填寫 Word 文檔中的表單字段。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
我們已經演示了如何創建表單域。本文主要向您展示開發人員如何使用獨立的.NET Word 組件 Spire.Doc僅通過 4 個簡單的步驟在 C# 中填寫 Word 文檔中的表單字段。
確保 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 { } } } }
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn