Solved

OPEN XML (ExtremeML) How can I Copy a Workbook

Posted on 2010-09-08
5
1,077 Views
Last Modified: 2012-05-10
I am trying to copy a workbook in openxml and create a new workbook.

Tried to follow the documentation, but I can't seem to make this work.


Can anyone tell me how to do this... in the code below "TemplateResource" is not recognized and I can't figure out what to use. I tried a file name, and the code runs, but the "input" var is null after its execution.
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

    }

}

Open in new window

0
Comment
Question by:dantheanswerman
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:existenz2
Comment Utility
TemplateResource is a constant which holds the fully qualified name of the embedded template in the assembly.

So for example: MyAssembly.Templates.MyFirsTemplate.xslx
0
 
LVL 1

Expert Comment

by:Tim85
Comment Utility
You don't need to use Excel templates that are embedded in the assembly as resources. You could equally assign any valid System.IO.Stream object to the "input" variable, for example a FileStream that is constructed using the path to file containing your template.
0
 
LVL 2

Author Comment

by:dantheanswerman
Comment Utility
OK.. I kind of understand where you're going with this, but am struggling to put it into C# code.

Could you give me some sample code on how to get this done?

Thanks so much... struggling with this.
0
 
LVL 1

Accepted Solution

by:
Tim85 earned 500 total points
Comment Utility
The attached code snippet is almost identical to your original sample, except that I have modified the first line to assign a FileStream to the variable "input" instead of the original assembly resource stream.
The path specified in the FileStream constructor should be the fully qualified path to the template file. When you run this method it will simply create an exact copy of the template file at the path specified in the FileStream assigned to the variable "output". In order to populate or modify the package retrieved from the template, you need to call the appropriate methods on the package object with the body of the using block.



 

public static void CustomizeTemplateToStream()

        {



            using (var input = new FileStream("the_path_to_my_template_file", FileMode.Open))

            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

            }

        }

Open in new window

0
 
LVL 2

Author Closing Comment

by:dantheanswerman
Comment Utility
Thank you very much.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now