Mustafa El-assi
Mustafa El-assi

Reputation: 53

Saving an excel template as part of the resources C# Visual Studio

I am working on my first C# project which has the goal of reading out configuration files, saving the read data in an existing excel template and then saving a copy of the excel file at the desired location.

The parsing part has been successfully created, I also know how to do this task with an actual existing excel template as long I know its location. But I'm trying to overcome the problem of having to keep the excel template as part of the directory, my goal would be to have the excel file as part of my final executable.

I have read other questions similar to mine but I think I'm just not experienced enough to understand what I really have to do.

What I have:

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
Excel excel = new Excel(startupPath + @"\template_hm_mach10x.xlsx", 1);

This is how I create the excel instance that I will be working with, it is an existing template that I have placed in the /debug folder so I could work with it in my prototype.

I then alter the template using

 ws = wb.Worksheets[d.Ws];
 wb.Worksheets[d.Ws].Select();
 ws.Cells[d.Row, d.Col].Value2 = d.Data;

and then save the file using excel.SaveAs(saveFileDialog.FileName.ToString());

What I have tried

I have already tried to add the template to my solution by doing:

Right-Click on my Solution > Add.. > Existing item> Select the item

there I tried to look for the assembly options of the file but I couldn't find them in the properties :

Screenshot of all the available File Properties

I have also tried to add it to the Project and it gave me the option of "Copy to output", but I don't know how that would help me, or what it does.

What I have researched

I have read about people saving the template as a binary string of some sort and then generating the file and saving it on the Computer, that would be a good solution for me, but having never worked with this I just don't know how it should be realized. I think it would be the best solution since the enduser wouldn't have access to the template file anymore and it would just be very interesting for me to learn how to do that.

But if that is no solution for my problem, then I'd be happy to learn how to make the template file part of the Programm and being able to use a relative path to it.

Thank you all for your help ! I have tried to ask this question before but it was my first question ever on this website and I think I just didn't do a good job asking my question precisely.

If there is more information needed on any part of my code or IDE I will gladly Edit it in.

Upvotes: 2

Views: 2338

Answers (1)

Mustafa El-assi
Mustafa El-assi

Reputation: 53

My Answer

I actually got it to work ! I'll reply here so anyone facing a similar problem can benefit

I learned how to implement a File as a embeded resource and this is how:

Rightclick on your progect > Properties > Resources

Add the File by clicking "Add Resource" > "Add Existing File"

the File will now be displayed in your Projectstructure under Resources.

Open the Properties of the File and select the Build Action "Embedded Resource"

The embedded File will be saved in a temporary file in a temporary location and opened from there to be edited and saved aftwerwards. The temporary file will be filled by converting a resource stream into a byte array which will then be written into the file.

string templatefile = System.IO.Path.GetTempFileName();
MemoryStream ms = new MemoryStream(); 
_assembly.GetManifestResourceStream("SwitchConfigReader.Resources.template_hm_mach10x.xlsx").CopyTo(ms);
Byte[] bArray = ms.ToArray();
System.IO.File.WriteAllBytes(templatefile, bArray);
Excel excel = new Excel(templatefile, 1);

The File can then be edited and saved

excel.SaveAs("path_and_name_of_Excel_Doc.xlsx");

this is just my solution for this problem right now and I am fairly new to programming, so take it for what its worth.

I hope I helped someone today.

Upvotes: 2

Related Questions