...
2. Instantiate the ExcelTemplate
object.
Csharp |
---|
|
ExcelTemplate oXLTXLT = new ExcelTemplate();
|
Vbnet |
---|
|
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 |
---|
|
oXLTXLT.Open(Page.MapPath("templates\\Hello World.xlsx"));
|
Vbnet |
---|
|
oXLTXLT.Open(Page.MapPath("templates\Hello World.xlsx")) |
...
Csharp |
---|
|
DataBindingProperties oDataPropsDataProps = oXLTXLT.CreateDataBindingProperties();
|
Vbnet |
---|
|
Dim oDataProps DataProps As DataBindingProperties = oXLTXLT.CreateDataBindingProperties() |
...
Csharp |
---|
|
string value = DataValueBox.Text.Trim();
oXLTXLT.BindCellData(value, "DataValue", oDataPropsDataProps);
|
Vbnet |
---|
|
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 |
---|
|
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 |
---|
|
oXLTXLT.Save(Page.Response, "Output.xlsx", false);
|
Vbnet |
---|
|
oXLTXLT.Save(Page.Response, "Output.xlsx", False) |
...
Congratulations, you have completed Hello World for ExcelTemplate!
Final Code
Csharp |
---|
|
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 |
---|
|
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) |