Solved

OPEN XML (ExtremeML) How can I Copy a Workbook

Posted on 2010-09-08
5
1,088 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
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

832 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