Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
After creating a workbook with ExcelTemplate, you can: * [Save the workbook to disk.|#disk] * [Write the workbook to a stream.|#stream] * [Stream the workbook to the client and open it in Excel.|#response1] * [Stream the workbook to the client and open it in the browser.|#response2] * [Pass the workbook to ExcelApplication.|#excelapp] {anchor:disk} h2. Save(outputFileName) This method saves the generated Excel file on the server. outputFileName specifies a complete path and file name for the generated file. ExcelWriter will save the file to this location. If a file with the same name exists, it will be overwritten by the new Excel file. h3. Example {csharp}
Wiki Markup
Excerpt

ExcelTemplate has several different options for saving an ExcelTemplate generated file.

After creating a workbook with ExcelTemplate, you can:

Table of Contents
maxLevel2

Save the output to disk

Save(outputFileName)

This method saves the generated Excel file on the server.

outputFileName specifies a complete path and file name for the generated file. ExcelWriter will save the file to this location. If a file with the same name exists, it will be overwritten by the new Excel file.

Example

Code Block
languagec#
excelTmpl.Save(@"C:\Reports\Orders.xls");

{csharp}
{vbnet}

Code Block
languagevb
excelTmpl.Save(@"cC:\Reports\Orders.xls")

{vbnet}

{anchor:stream}
h2. 

 

Write the workbook to a stream

Save(outputStream)

...

This

...

method

...

writes

...

the

...

generated

...

Excel

...

file

...

to

...

the

...

specified

...

System.IO.Stream,

...

or

...

a

...

class

...

derived

...

from

...

System.IO.Stream

...

(for

...

example,

...

System.IO.FileStream).

...

If

...

you

...

pass

...

Save

...

a

...

System.IO.FileStream,

...

ExcelWriter

...

will

...

save

...

the

...

generated

...

file

...

on

...

the

...

server.

...

If

...

you

...

pass

...

Save

...

Response.OutputStream,

...

ExcelWriter

...

will

...

stream

...

the

...

the

...

generated

...

file

...

to

...

the

...

client.

...

Example

Code Block
languagec#
//-- FileStream and FileMode are in the System.IO namespace
FileStream fstream = new FileStream(@"C:\temp\outfile.xls", FileMode.Create);
 
//-- Pass the FileStream to ExcelTemplate
excelTmpl.Save(fstream);

//-- Close the FileStream (could be in a finally block)
fstream.Close();
{csharp}
{vbnet}
Code Block
languagevb
'-- FileStream and FileMode are in the System.IO namespace
Dim fstream As New FileStream("C:\temp\outfile.xls", FileMode.Create)

'-- Pass the FileStream to ExcelTemplate
excelTmpl.Save(fstream)

'-- Close the FileStream (could be in a finally block)
fstream.Close()
{vbnet}

{anchor:response1}
h2. 

Stream the workbook to the client and open it in Excel

Save(response)

...

This

...

method

...

streams

...

the

...

generated

...

Excel

...

file

...

to

...

the

...

client.

...

If

...

you

...

pass

...

Save

...

an

...

HttpResponse

...

object,

...

ExcelWriter

...

will

...

stream

...

the

...

generated

...

file

...

to

...

the

...

client.

...

If

...

the

...

user

...

chooses

...

to

...

open

...

(rather

...

than

...

save)

...

the

...

file,

...

it

...

will

...

open

...

in

...

the

...

browser

...

window.

...

The

...

browser

...

will

...

use

...

the

...

ExcelWriter

...

script

...

name

...

as

...

the

...

default

...

file

...

name

...

displayed

...

in

...

the

...

File

...

Download

...

dialog.

...

To

...

set

...

a

...

different

...

file

...

name

...

and/or

...

to

...

open

...

the

...

file

...

in

...

Microsoft

...

Excel,

...

use

...

the

...

signature

...

Save(response,

...

attachmentName,

...

OpenInBrowser).

...

Example

Code Block
languagec#
excelTmpl.Save(Page.Response);
{csharp}
{vbnet}
Code Block
languagevb
excelTmpl.Save(Page.Response)
{vbnet}

{anchor:response2}
h2. 

Stream the workbook to the client and open it in the browser

Save(response,

...

attachmentName,

...

OpenInBrowser)

...

This

...

method

...

streams

...

the

...

generated

...

Excel

...

file

...

to

...

the

...

client.

...

If

...

you

...

pass

...

Save

...

an

...

HttpResponse

...

object,

...

ExcelWriter

...

will

...

stream

...

the

...

generated

...

file

...

to

...

the

...

client.

...

This

...

method

...

allows

...

you

...

to

...

specify

...

a

...

default

...

client-side

...

file

...

name,

...

and

...

whether

...

the

...

file

...

should

...

be

...

opened

...

in

...

the

...

browser

...

window

...

or

...

in

...

Microsoft

...

Excel.

...

The

...

parameter

...

attachmentName

...

specifies

...

a

...

name

...

for

...

the

...

generated

...

Excel

...

file;

...

this

...

name

...

will

...

be

...

displayed

...

in

...

the

...

download

...

dialog

...

when

...

the

...

file

...

is

...

streamed

...

to

...

the

...

browser.

...

If

...

the

...

parameter

...

openInBrowser

...

is

...

set

...

to

...

true,

...

and

...

the

...

user

...

chooses

...

to

...

open

...

the

...

file,

...

the

...

file

...

will

...

open

...

in

...

the

...

browser

...

window.

...

If

...

openInBrowser

...

is

...

set

...

to

...

false,

...

and

...

the

...

user

...

chooses

...

to

...

open

...

the

...

file,

...

the

...

file

...

will

...

open

...

in

...

Microsoft

...

Excel.

...

By

...

default,

...

the

...

file

...

will

...

open

...

in

...

the

...

browser

...

window.

...

Note:

...

not

...

all

...

browsers

...

can

...

embed

...

an

...

Excel

...

file

...

in

...

the

...

browser

...

window.

...

Example

Code Block
languagec#
excelTmpl.Save(Page.Response, "Output.xls", false);
{csharp}
{vbnet}
Code Block
languagevb
excelTmpl.Save(Page.Response, "Output.xls", False)
{vbnet}

{anchor:excelapp}
h2. Pass the Template to ExcelApplication


{note} Though the [ExcelTemplate] object supports                 Excel's BIFF8 (Excel 97/2000/XP/2003) and Office Open XML (Excel 2007) formats, the [ExcelApplication] object does not yet                  support Office Open XML (Excel 2007) formats.  Templates based on Office Open XML files                  (.xlsx and .xlsm) should not be passed to the ExcelApplication object. {note} 
You can use ExcelTemplate to open and populate an ExcelWriter template,     then pass the populated workbook to ExcelApplication for additional     processing. In this case, do not call Save. Instead, pass the ExcelTemplate object to ExcelApplication's open method:
h3.
{code:c#|title=C#}
ExampleExcelTemplate 

Pass the Template to ExcelApplication

You can use ExcelTemplate to open and populate an ExcelWriter template, then pass the populated workbook to ExcelApplication for additional processing. In this case, do not call Save. Instead, pass the ExcelTemplate object to ExcelApplication's open method:

Example

Code Block
languagec#
 ExampleExcelTemplate xlt = new ExcelTemplate();

xlt.Process();

ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Open(xlt);
{code}
{scrollbar}