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
Solved

OPEN XML (ExtremeML) How can I Copy a Workbook

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reference issues in Visual Studio 3 41
VB: Convert 2 dates to specific format 24 52
insert value of checklistbox checked 4 32
linq, c# 8 23
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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