Quick Start
> Create a Spreadsheet with ExcelApplication |
|
Create a Spreadsheet with ExcelApplication
To create generate an Excel workbook from code - without using
an ExcelWriter template
- use ExcelApplication, which is
the main class for pure code-based workbook generation. This class
is an engine used to open, create, and write (save or stream to a browser)
workbooks. A single instance of ExcelApplication can
generate multiple workbooks.
Step 1: Import ExcelApplication

The ExcelApplication class is in the
SoftArtisans.OfficeWriter.ExcelWriter namespace.
The class can be referenced as SoftArtisans.OfficeWriter.ExcelWriter.ExcelApplication.
To minimize typing and errors, import the namespace to the aspx page, and
reference the class as ExcelApplication, without
the namespace prefix. If you are coding directly in the .aspx
page, following the Page directive, include:
<%@ Import Namespace="SoftArtisans.OfficeWriter.ExcelWriter" %>
If you are coding in the code-behind page (.aspx.vb or .aspx.cs),
include an Imports or using statement at
the top of the code-behind page:
| In VB.NET: | | Imports SoftArtisans.OfficeWriter.ExcelWriter |
| In C#: | | using SoftArtisans.OfficeWriter.ExcelWriter; |
Step 2: Create an Instance of ExcelApplication

To create an instance of ExcelApplication, use:
| In VB.NET: | | Dim xla As New ExcelApplication() |
| In C#: | | ExcelApplication xla = new ExcelApplication(); |
Step 3: Create a Workbook

A Workbook object represents an Excel workbook. To return a
Workbook object:
- Call
ExcelApplication.Open
to open an existing Excel workbook, for example:
| In VB.NET: |
|
Dim wb As Workbook = xla.Open("C:\Reports\Report.xls")
|
| In C#: |
|
Workbook wb = xla.Open(@"C:\Reports\Report.xls"); |
Or,
- Call
ExcelApplication.Create
to create a new Excel workbook, for example:
| In VB.NET: |
|
xla = New ExcelApplication
Dim wb As Workbook = xla.Create()
|
| In C#: |
|
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Create(); |
Step 4: Get a Worksheet and Set Cell Values

A Worksheet object represents a single Excel worksheet.
To return a Worksheet object
use the Workbook.Worksheets property, specifying the sheet by
index or name:
| In VB.NET: |
|
Dim ws As Worksheet = wb.Worksheets(0)
|
| In C#: |
|
Worksheet ws = wb.Worksheets[0]; |
A Cell object represents a single cell
in a worksheet. To return a Cell object, use
Worksheet.Cells, specifying the cell by 0-based
row and column indexes or by Excel-style reference. Use
Cell.Value to enter a cell value. The first
parameter of the Cells property is the row index, the
second is the column index.
| In VB.NET: |
|
ws.Cells(0, 0).Value = "Welcome to SoftArtisans OfficeWriter"
ws.Cells("A3").Value = "Jan"
ws.Cells("B3").Value = "Feb"
ws.Cells("C3").Value = "Mar"
|
| In C#: |
|
ws.Cells[0,0].Value = "Welcome to SoftArtisans OfficeWriter";
ws.Cells["A3"].Value = "Jan";
ws.Cells["B3"].Value = "Feb";
ws.Cells["C3"].Value = "Mar";
|
Step 5: Generate the Workbook

When the workbook is complete, you can save it to the server's hard disk, return it
in memory, stream it to the browser, or return it as an
ExcelTemplate object (for more information, see
Output Options. For example, to stream
the workbook to the browser, use:
| In VB.NET: |
|
xla.Save(wb, Page.Response, "myfile.xls", True)
|
| In C#: |
|
xla.Save(wb, Page.Response, "myfile.xls", True); |

Copyright 2007 © SoftArtisans, Inc. All Rights Reserved.
|