...
Csharp |
---|
|
ExcelTemplate oXLT = new ExcelTemplate();
|
Vbnet |
---|
Dim oXLT 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 |
---|
|
oXLT.Open(Page.MapPath("templates\\Hello World.xlsx"));
|
Vbnet |
---|
oXLT.Open(Page.MapPath("templates\Hello World.xlsx")) |
4. Create a DataBindingProperties
object
Csharp |
---|
|
DataBindingProperties oDataProps = oXLT.CreateDataBindingProperties();
|
Vbnet |
---|
Dim oDataProps As DataBindingProperties = oXLT.CreateDataBindingProperties() |
The DataBindingProperties
object can be used to change the behavior of how data is imported. For example, if we were importing multiple rows of data, we can use the DataBindingProperties.MaxRows
property to limit the number of rows that are imported. In this sample, we won't be changing any of the import properties, but we still need the DataBindingProperties
object to bind data.
...
Csharp |
---|
|
string value = DataValueBox.Text.Trim();
oXLT.BindCellData(value, "DataValue", oDataProps);
|
Vbnet |
---|
Dim value As String = DataValueBox.Text.Trim() oXLT.BindCellData(value, "DataValue", oDataProps) |
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.
...
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.
...
Csharp |
---|
|
oXLT.Save(Page.Response, "Output.xlsx", false);
|
Vbnet |
---|
oXLT.Save(Page.Response, "Output.xlsx", False) |
There are several options for ExcelTemplate.Save
including: save to disk, save to memory stream, stream back to the client inline, and stream back to the client as an attachment. In this case, we're streaming the workbook back to the client as an attachment.
Also, ExcelWriter does not convert between file formats, so it is important that the file extension on the output file matches the file extension of the original template file.
...
Csharp |
---|
|
using SoftArtisans.OfficeWriter.ExcelWriter;
...
ExcelTemplate oXLT = new ExcelTemplate();
oXLT.Open(Page.MapPath("Hello World.xlsx"));
DataBindingProperties oDataProps = oXLT.CreateDataBindingProperties();
string value = DataValueBox.Text.Trim();
oXLT.BindCellData(value, "DataValue", oDataProps);
oXLT.Process();
oXLT.Save(Page.Response, "Output.xlsx", false);
|
Vbnet |
---|
Imports SoftArtisans.OfficeWriter.ExcelWriter ... Dim oXLT As New ExcelTemplate() oXLT.Open(Page.MapPath("templates\Hello World.xlsx")) Dim oDataProps As DataBindingProperties = oXLT.CreateDataBindingProperties() Dim value As String = DataValueBox.Text.Trim() oXLT.BindCellData(value, "DataValue", oDataProps) oXLT.Process() oXLT.Save(Page.Response, "Output.xlsx", False) |