Saving a Customized Template to a Stream

Nov 22, 2011 at 12:56 PM

Hi.

Im trying to use your API to open a template and them fill it fill some data.

I use your example code :

public static void CustomizeTemplateToStream()
{
    using (var input = Assembly.GetExecutingAssembly().GetManifestResourceStream(TemplateResource))
    using (var output = new FileStream("MyWorkbook.xlsx", FileMode.Create, FileAccess.Write))
    using (var package = SpreadsheetDocumentWrapper.Open(input, output))
    {
        // Code here to customize the template workbook
    }
}

But the "Input" variable is NULL.

TemplateResource, can that be the location of the template excel file i want to open ?
I have set it to a string right now pointing to the location of the file.
But it is still NULL.
Please help.
Coordinator
Nov 22, 2011 at 1:28 PM

The sample code that you are using is trying to retrieve the template file from an assembly resource using the GetManifestResourceStream() method. It will only work if your template file is stored in the assembly as an embedded resource.

If your template file is accessible on disk, you can simply use the FileStream class to access it in the same way ...

using (var input = new FileStream("path_to_template_file", FileMode.Open, FileAccess.Read))
using (var output = new FileStream("MyWorkbook.xlsx", FileMode.Create, FileAccess.Write))
using (var package = SpreadsheetDocumentWrapper.Open(input, output))
{
    // Code here to customize the template workbook
}

Good luck,

Tim

Nov 22, 2011 at 1:36 PM

Thx for the fast respons!

It works now, or atleast part of it :)

How do I send the file back to the client after it has completed its work?

Coordinator
Nov 22, 2011 at 1:55 PM

Saving the modified package is handled by the output stream that is created in the second line ... 

using (var output = new FileStream("MyWorkbook.xlsx", FileMode.Create, FileAccess.Write))

Of course, you need to give it the correct file path.

 

 

Nov 22, 2011 at 1:56 PM
Edited Nov 22, 2011 at 2:00 PM

Hmm doesnt work for me, Im using ASP.NET...maybe that doesnt matter but nothing is happening..

It creates the file, put puts it on the harddrive, not sending it back to the browser for download...

Nov 22, 2011 at 3:23 PM

Do I have to do something else with the output stream if I want that functionality ?

Jan 12, 2012 at 6:14 PM

I think you have to add the stream to the Response's stream, and also set the type of the Reponse to the header for excel files

Jan 14, 2012 at 3:10 PM
Edited Jan 14, 2012 at 3:13 PM

To send file directly to browser

 

// ASP.NET WebForms
protected void Button1_Click(object sender, EventArgs e) {
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // mime type of xlsx format
    this.Response.AddHeader("Content-Disposition", "attachment; filename=Report.xlsx"); // set file name for download
    using (var inputStream = new FileStream(Server.MapPath("Template1.xlsx"), FileMode.Open, FileAccess.Read)) // template file in web root directory
    using (var package = SpreadsheetDocumentWrapper.Open(inputStream, Response.OutputStream)) {
        // TODO: make changes to template
    }
    Response.End();
}
// APS.NET MVC
public ActionResult ExportExcel() {
    var outputStream = new System.IO.MemoryStream();
    using (var inputStream = new FileStream(Server.MapPath("Template1.xlsx"), FileMode.Open, FileAccess.Read))
    using (var package = SpreadsheetDocumentWrapper.Open(inputStream, outputStream)) {
        // TODO: make changes to template
    }
    outputStream.Position = 0;
    return File(outputStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx");
}