Knowledgebase (2330)
Children categories
We have already demonstrated how to create form field. This article mainly shows you how developers fill form field in word document in C# only with 4 simple steps by using a standalone .NET Word component Spire.Doc.
Make sure Spire.Doc for .NET has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how developers Fill Form Field by using Spire.Doc:
Step 1: Open the form that needs to fill the data.
//Create word document Document document = new Document(@"..\..\..\Data\UserForm.doc");
Step 2: Load data that will fill the form.
//Fill data from XML file
using (Stream stream = File.OpenRead(@"..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
Step 3: Use the loaded data to fill the form.
//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;
}
}
}
}
Step 4: Save the document to file in XML or Microsoft Word format.
//Save doc file
document.SaveToFile("Sample.doc",FileFormat.Doc);
Effective Screenshot:
Full Source Code for Fill FormField:
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 { }
}
}
}
C#/VB.NET: Create, Edit, or Delete Named Ranges in Excel
2023-07-03 02:29:00 Written by AdministratorA named range in Excel is a user-defined name given to a specific cell or range of cells. It allows you to assign a meaningful and descriptive name to a set of data, making it easier to refer to that data in formulas, functions, and other parts of the spreadsheet. In this article, you will learn how to create, edit or delete named ranges in Excel in C# and VB.NET using Spire.XLS for .NET.
- Create a Named Range in Excel in C# and VB.NET
- Edit an Existing Named Range in Excel in C# and VB.NET
- Delete a Named Range from Excel in C# and VB.NET
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Create a Named Range in Excel in C# and VB.NET
You can use the Workbook.NameRanges.Add(string name) method provided by Spire.XLS for .NET to add a named range to an Excel workbook. Once the named range is added, you can define the cell or range of cells it refers to using the INamedRange.RefersToRange property.
The following steps explain how to create a named range in Excel using Spire.XLS for .NET:
- Initialize an instance of the Workbook class.
- Load an Excel workbook using the Workbook.LoadFromFile() method.
- Add a named range to the workbook using the Workbook.NameRanges.Add(string name) method.
- Get a specific worksheet in the workbook using the Workbook.Worksheets[int index] property.
- Set the cell range that the named range refers to using the INamedRange.RefersToRange property.
- Save the result file using the Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using Spire.Xls.Core;
namespace CreateNamedRanges
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load an Excel workbook
workbook.LoadFromFile(@"Sample.xlsx");
//Add a named range to the workbook
INamedRange namedRange = workbook.NameRanges.Add("Amount");
//Get a specific worksheet in the workbook
Worksheet sheet = workbook.Worksheets[0];
//Set the cell range that the named range references
namedRange.RefersToRange = sheet.Range["D2:D5"];
//Save the result file to a specific location
string result = "CreateNamedRange.xlsx";
workbook.SaveToFile(result, ExcelVersion.Version2013);
workbook.Dispose();
}
}
}

Edit an Existing Named Range in Excel in C# and VB.NET
After you've created a named range, you may want to modify its name or adjust the cells it refers to.
The following steps explain how to modify the name and cell references of an existing named range in Excel using Spire.XLS for .NET:
- Initialize an instance of the Workbook class.
- Load an Excel workbook using the Workbook.LoadFromFile() method.
- Get a specific named range in the workbook using the Workbook.NameRanges[int index] property.
- Modify the name of the named range using the INamedRange.Name property.
- Modify the cells that the named range refers to using the INamedRange.RefersToRange property.
- Save the result file using the Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using Spire.Xls.Core;
namespace ModifyNamedRanges
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load an Excel workbook
workbook.LoadFromFile(@"CreateNamedRange.xlsx");
//Get a specific named range in the workbook
INamedRange namedRange = workbook.NameRanges[0];
//Change the name of the named range
namedRange.Name = "MonitorAmount";
//Set the cell range that the named range references
namedRange.RefersToRange = workbook.Worksheets[0].Range["D2"];
//Save the result file to a specific location
string result = "ModifyNamedRange.xlsx";
workbook.SaveToFile(result, ExcelVersion.Version2013);
workbook.Dispose();
}
}
}

Delete a Named Range from Excel in C# and VB.NET
If you have made significant changes to the structure or layout of your spreadsheet, it might be necessary to delete a named range that is no longer relevant or accurate.
The following steps explain how to delete a named range from Excel using Spire.XLS for .NET:
- Initialize an instance of the Workbook class.
- Load an Excel workbook using the Workbook.LoadFromFile() method.
- Remove a specific named range by its index or name using the Workbook.NameRanges.RemoveAt(int index) or Workbook.NameRanges.Remove(string name) method.
- Save the result file using the Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using Spire.Xls.Core;
namespace RemoveNamedRanges
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load an Excel workbook
workbook.LoadFromFile(@"CreateNamedRange.xlsx");
//Remove a specific named range by its index
workbook.NameRanges.RemoveAt(0);
//Remove a specific named range by its name
//workbook.NameRanges.Remove("Amount");
//Save the result file to a specific location
string result = "RemoveNamedRange.xlsx";
workbook.SaveToFile(result, ExcelVersion.Version2013);
workbook.Dispose();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.