Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

2. Instantiate the ExcelTemplate object.

Csharp
2
2
ExcelTemplate oXLTXLT = new ExcelTemplate();
Vbnet
2
2

Dim oXLT XLT As New ExcelTemplate()

3. Open the template file.
The ExcelTemplate object corresponds to a single template file, so a given ExcelTemplate instance can only have one template file open.

Csharp
3
3
oXLTXLT.Open(Page.MapPath("templates\\Hello World.xlsx"));
Vbnet
3
3

oXLTXLT.Open(Page.MapPath("templates\Hello World.xlsx"))

...

Csharp
4
4
DataBindingProperties oDataPropsDataProps = oXLTXLT.CreateDataBindingProperties();
Vbnet
4
4

Dim oDataProps DataProps As DataBindingProperties = oXLTXLT.CreateDataBindingProperties()

...

Csharp
5
5
string value = DataValueBox.Text.Trim();
oXLTXLT.BindCellData(value, "DataValue", oDataPropsDataProps);
Vbnet
5
5

Dim value As String = DataValueBox.Text.Trim()
oXLT XLT.BindCellData(value, "DataValue", oDataPropsDataProps)

In this sample, we're pulling the single value from the text box on the web form.
Since we're binding a single value, we use BindCellData() and specify the data marker ID. Note that we need to pass the DataBindingProperties object, even though none of the DataBindingProperties are active.

6. Call ExcelTemplate.Process() to insert the data into the file

Csharp
6
6
oXLTXLT.Process();
Vbnet
6
6

oXLTXLT.Process()

ExcelTemplate.Process() handles everything relating to inserting the data into the file. If we were importing multiple rows of data, Process() would handle inserting the new physical rows into the Excel worksheet.

7. Save the output

Csharp
7
7
oXLTXLT.Save(Page.Response, "Output.xlsx", false);
Vbnet
7
7

oXLTXLT.Save(Page.Response, "Output.xlsx", False)

...

Congratulations, you have completed Hello World for ExcelTemplate!

Final Code

Csharp
8
8

using SoftArtisans.OfficeWriter.ExcelWriter;
...
ExcelTemplate oXLTXLT = new ExcelTemplate();
oXLTXLT.Open(Page.MapPath("Hello World.xlsx"));
DataBindingProperties oDataPropsDataProps = oXLTXLT.CreateDataBindingProperties();
string value = DataValueBox.Text.Trim();
oXLTXLT.BindCellData(value, "DataValue", oDataPropsDataProps);
oXLTXLT.Process();
oXLTXLT.Save(Page.Response, "Output.xlsx", false);

Vbnet
8
8

Imports SoftArtisans.OfficeWriter.ExcelWriter
...
Dim oXLT XLT As New ExcelTemplate()
oXLT XLT.Open(Page.MapPath("templates\Hello World.xlsx"))
Dim oDataProps DataProps As DataBindingProperties = oXLTXLT.CreateDataBindingProperties()
Dim value As String = DataValueBox.Text.Trim()
oXLT XLT.BindCellData(value, "DataValue", oDataPropsDataProps)
oXLT XLT.Process()
oXLT XLT.Save(Page.Response, "Output.xlsx", False)