[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1104
  • Last Modified:

OPEN XML (ExtremeML) How can I Copy a Workbook

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
dantheanswerman
Asked:
dantheanswerman
  • 2
  • 2
1 Solution
 
existenz2Commented:
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
 
Tim85Commented:
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
 
dantheanswermanAuthor Commented:
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
 
Tim85Commented:
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
 
dantheanswermanAuthor Commented:
Thank you very much.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now