Solved

OPEN XML (ExtremeML) How can I Copy a Workbook

Posted on 2010-09-08
5
1,095 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:existenz2
ID: 33643964
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
ID: 33648523
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
ID: 33653350
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
ID: 33653538
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
ID: 33686467
Thank you very much.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…

730 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