Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Wiki Markup
|| Table of

...

Table of Contents

Hello World with ExcelApplication

 Contents ||
| {toc} |

h1. _Hello World_ with ExcelApplication

ExcelWriter's ExcelApplication approach provides you with full programmatic control over the Excel file formats (XLS, XLSX, XLSM). This includes the ability to create and modify: charts, formulas, formatting, data validation, conditional formatting, worksheet protection, images and more! This tutorial will show you how to create a new workbook and write a value to a cell.

...

Diving right into the code

Info
In the Hello World sample web application,
 

h1. Diving right into the code

{info} In the _Hello World_ sample web application, ExcelApplication_HelloWorld.aspx has a text box for users to supply a value and ExcelApplication_HelloWorld.aspx.cs/vb contains the code shown below.
{info}

1. Include the {{SoftArtisans.OfficeWriter.ExcelWriter}} namespace in the code behind.

...



{csharp
:1
1
}
using SoftArtisans.OfficeWriter.ExcelWriter;
Vbnet
11Imports
{csharp}

{vbnet:1}
Imports SoftArtisans.OfficeWriter.ExcelWriter

{vbnet}

2. Instantiate the {{[ExcelApplication

...

Csharp
22
]}} object

{csharp:2}
ExcelApplication XLAPP = new ExcelApplication();
Vbnet
22

Dim XLAPP As New ExcelApplication()

Unlike the ExcelTemplate object, which represents a single file, the ExcelApplication works as a file generation engine. The ExcelApplication object can be used to create, open, and save multiple workbooks.

3. Create a new workbook with ExcelApplication.Create()

Csharp
33
{csharp}

{vbnet:2}
Dim XLAPP As New ExcelApplication()
{vbnet}

Unlike the {{[ExcelTemplate]}} object, which represents a single file, the {{ExcelApplication}} works as a file generation engine. The {{ExcelApplication}} object can be used to create, open, and save multiple workbooks. 

3. Create a new workbook with {{[ExcelApplication.Create()]}}

{csharp:3}
Workbook WB = XLAPP.Create(ExcelApplication.FileFormat.Xlsx);
Vbnet
33Dim WB As Workbook =
{csharp}

{vbnet:3}
Dim WB As Workbook = XLAPP.Create(ExcelApplication.FileFormat.Xlsx)

...


{vbnet}

ExcelWriter has the ability to create Excel 2003 (XLS) files and Excel 2007 (XLSX) files, but cannot convert between formats. The file format must be declared when the workbook is created and the file extension of the output file must match when the file is saved.

...

 

{{ExcelApplication.Create()}} automatically creates the first worksheet in the workbook for you.

...

 

4. Access the first worksheet through the {{[Workbook.Worksheets

...

Csharp
44
]}} collection

{csharp:4}
Worksheet WKST = WB.Worksheets[0];
Vbnet
44Dim WKST As Worksheet =
{csharp}

{vbnet:4}
Dim WKST As Worksheet = WB.Worksheets(0)

...


{vbnet}

You can access worksheets by name (e.g. "Sheet1") or by index (shown above), but ExcelWriter will throw an exception if you attempt to access a worksheet that does not exist.

...

 

5. Write the value from the web form into a cell

...



{csharp
:5
5
}
string value = DataValueBox.Text.Trim();
WKST.Cells[0,0].Value = value;
Vbnet
55Dim value As String =
{csharp}

{vbnet:5}
Dim value As String = DataValueBox.Text.Trim()


WKST.Cells(0, 0).Value =
value

...

 value
{vbnet}

It is important to note that ExcelWriter indices are all 0-indexed, unlike Excel indices, which are 1-indexed. This is importing when working with cells, rows, columns, and worksheet positions in ExcelWriter. You can also reference cells by name; in this case, it would be "A1".

...

 

6. Save the workbook

...



{csharp
:6
6
}
XLAPP.Save(WB, Page.Response, "Output.xlsx", false);
Vbnet
66
{csharp}

{vbnet:6}
XLAPP.Save(WB, Page.Response, "Output.xlsx", False)

{vbnet}

{{[ExcelApplication.Save]}} has the same output options as {{ExcelTemplate}}: 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.

...


Remember that the file extension must match the file format specified when the workbook was created.

...

 

7. Run the code. In the output file, you will see that the custom text from the form has been inserted into cell "A1".

...

Image Removed

Final Code

Csharp
77
 

!ExcelApplicationFinal.png!

h1. Final Code

{csharp:7}

using SoftArtisans.OfficeWriter.ExcelWriter;
...
ExcelApplication XLAPP = new ExcelApplication();
Workbook WB = XLAPP.Create(ExcelApplication.FileFormat.Xlsx);
Worksheet WKST = WB.Worksheets[0];
string value = DataValueBox.Text.Trim();
WKST.Cells[0,0].Value = value;
XLAPP.Save(WB, Page.Response, "Output.xlsx", false);

Vbnet
88Include
{csharp}

{vbnet:8}
Include SoftArtisans.OfficeWriter.ExcelWriter


...


Dim XLAPP As New ExcelApplication()


Dim WB As Workbook = XLAPP.Create(ExcelApplication.FileFormat.Xlsx)


Dim WKST As Worksheet = WB.Worksheets(0)


Dim value As String = DataValueBox.Text.Trim()


WKST.Cells(0, 0).Value = value


XLAPP.Save(WB, Page.Response, "Output.xlsx", False)

Downloads

You can download the code for the Hello World tutorial as a Visual Studio solution.

...



{vbnet}

h1. Downloads

You can download the code for the _Hello World_ tutorial as a Visual Studio solution.  

* [Hello World Tutorial^ExcelWriter_HelloWorldC#.zip]
* [Hello World Tutorial^ExcelWriter_HelloWorldVB.zip]