Page tree

Versions Compared

Key

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

...

Table

...

of

...

Contents

Table of Contents
maxLevel1

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, 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 {{

 

  1. Include the SoftArtisans.OfficeWriter.ExcelWriter

...

  1. namespace

...

  1. in

...

  1. the

...

  1. code

...

  1. behind.

...

  1. Code Block
    languagec#
    using SoftArtisans.OfficeWriter.ExcelWriter;

...

  1. Code Block
    languagevb
    Imports SoftArtisans.OfficeWriter.ExcelWriter

...



  1. Instantiate the ExcelApplication object

    Code Block
    languagec#
    ExcelApplication XLAPP = new ExcelApplication();

...

  1. Code Block
    languagevb
    Dim XLAPP As New ExcelApplication()

...

  1. 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.

     

     

  2. Create a new workbook with ExcelApplication.Create(FileFormat)

    Code Block
    languagec#
    Workbook WB = XLAPP.Create(ExcelApplication.FileFormat.Xlsx);

...

  1. Code Block
    languagevb
    Dim WB As Workbook = XLAPP.Create(ExcelApplication.FileFormat.Xlsx)

...

  1.  

    ExcelWriter has the ability to create Excel 2003 (XLS)

...

  1. files

...

  1. and

...

  1. Excel

...

  1. 2007

...

  1. (XLSX)

...

  1. files,

...

  1. but

...

  1. cannot

...

  1. convert

...

  1. between

...

  1. formats.

...

  1. The

...

  1. file

...

  1. format

...

  1. must

...

  1. be

...

  1. declared

...

  1. when

...

  1. the

...

  1. workbook

...

  1. is

...

  1. created

...

  1. and

...

  1. the

...

  1. file

...

  1. extension

...

  1. of

...

  1. the

...

  1. output

...

  1. file

...

  1. must

...

  1. match

...

  1. when

...

  1. the

...

  1. file

...

  1. is

...

  1. saved.

...

  1. ExcelApplication.Create(FileFormat)

...

  1. automatically

...

  1. creates

...

  1. the

...

  1. first

...

  1. worksheet

...

  1. in

...

  1. the

...

  1. workbook

...

  1. for

...

  1. you.

...


  1.  

  2. Access the first worksheet through the Workbook.Worksheets collection

    Code Block
    languagec#
    Worksheet WKST = WB.Worksheets[0];

...

  1. Code Block
    languagevb
    Dim WKST As Worksheet = WB.Worksheets(0)

...

  1. You can access worksheets by name (e.g.

...

  1. "Sheet1")

...

  1. or

...

  1. by

...

  1. index

...

  1. (shown

...

  1. above),

...

  1. but

...

  1. ExcelWriter

...

  1. will

...

  1. throw

...

  1. an

...

  1. exception

...

  1. if

...

  1. you

...

  1. attempt

...

  1. to

...

  1. access

...

  1. a

...

  1. worksheet

...

  1. that

...

  1. does

...

  1. not

...

  1. exist.

...

  1.  

     

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

    Code Block
    languagec#
    string value = DataValueBox.Text.Trim();
    WKST.Cells[0,0].Value = value;

...

  1. Code Block
    languagevb
    Dim value As String = DataValueBox.Text.Trim()
    WKST.Cells(0, 0).Value = value

...

  1. It is important to note that ExcelWriter indices are all 0-indexed,

...

  1. unlike

...

  1. Excel

...

  1. indices,

...

  1. which

...

  1. are

...

  1. 1-indexed.

...

  1. This

...

  1. is

...

  1. importing

...

  1. when

...

  1. working

...

  1. with

...

  1. cells,

...

  1. rows,

...

  1. columns,

...

  1. and

...

  1. worksheet

...

  1. positions

...

  1. in

...

  1. ExcelWriter.

...

  1. You

...

  1. can

...

  1. also

...

  1. reference

...

  1. cells

...

  1. by

...

  1. name;

...

  1. in

...

  1. this

...

  1. case,

...

  1. it

...

  1. would

...

  1. be

...

  1. "A1".

...

  1.  

     

  2. Save the workbook

    Code Block
    languagec#
    XLAPP.Save(WB, Page.Response, "Output.xlsx", false);

...

  1. Code Block
    languagevb
    XLAPP.Save(WB, Page.Response, "Output.xlsx", False)

...

  1. ExcelApplication.Save

...

  1. has

...

  1. the

...

  1. same

...

  1. output

...

  1. options

...

  1. as

...

  1. ExcelTemplate

...

  1. :

...

  1. save

...

  1. to

...

  1. disk,

...

  1. save

...

  1. to

...

  1. memory

...

  1. stream,

...

  1. stream

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. inline,

...

  1. and

...

  1. stream

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. as

...

  1. an

...

  1. attachment.

...

  1. In

...

  1. this

...

  1. case,

...

  1. we're

...

  1. streaming

...

  1. the

...

  1. workbook

...

  1. back

...

  1. to

...

  1. the

...

  1. client

...

  1. as

...

  1. an

...

  1. attachment.

...

  1. Remember

...

  1. that

...

  1. the

...

  1. file

...

  1. extension

...

  1. must

...

  1. match

...

  1. the

...

  1. file

...

  1. format

...

  1. specified

...

  1. when

...

  1. the

...

  1. workbook

...

  1. was

...

  1. created.

...

  1.  

     

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

Final Code

Code Block
languagec#
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);

{csharp}

{vbnet:8}
Code Block
languagevb
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)

{vbnet}

h1. Downloads

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

* [Hello World Tutorial^ExcelWriter

Downloads

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

...