Load excel file from resources / assembly in C #

I need to download an Excel file and write on it. I already added the file to the resources and set its build action for the Embedded Resource. My problem is that I cannot load it from resources / assembly. I have this code:

Assembly assembly = Assembly.GetExecutingAssembly(); Assembly asm = Assembly.GetExecutingAssembly(); string file = string.Format("{0}.UTReportTemplate.xls", asm.GetName().Name); var ms = new MemoryStream(); Stream fileStream = asm.GetManifestResourceStream(file); xlWorkBook = xlApp.Workbooks.Open(file); if (xlApp == null) { MessageBox.Show("Error: Unable to create Excel file."); return; } xlApp.Visible = false; 

What am I doing wrong? How can I access the file? Any help would be greatly appreciated. Thanks.

+6
source share
3 answers
 Assembly asm = Assembly.GetExecutingAssembly(); string file = string.Format("{0}.UTReportTemplate.xls", asm.GetName().Name); Stream fileStream = asm.GetManifestResourceStream(file); SaveStreamToFile(@"c:\Temp\Temp.xls",fileStream); //<--here is where to save to disk Excel.Application xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(@"c:\Temp\Temp.xls"); if (xlWorkBook == null) { MessageBox.Show("Error: Unable to open Excel file."); return; } xlApp.Visible = false; 

...

 public void SaveStreamToFile(string fileFullPath, Stream stream) { if (stream.Length == 0) return; // Create a FileStream object to write a stream to a file using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length)) { // Fill the bytes[] array with the stream data byte[] bytesInStream = new byte[stream.Length]; stream.Read(bytesInStream, 0, (int)bytesInStream.Length); // Use FileStream object to write to the specified file fileStream.Write(bytesInStream, 0, bytesInStream.Length); } } 
+13
source

I would like to add here my code snippet that works well in Visual Studio 2015. (Just improved Jeremy Thompson's answer .)

(Remember to set the Excel Build Action Embedded Resource Property file in the properties window.)

 public void launchExcel() { String resourceName = "Sample.xls"; String path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); Assembly asm = Assembly.GetExecutingAssembly(); string res = string.Format("{0}.Resources." + resourceName, asm.GetName().Name); Stream stream = asm.GetManifestResourceStream(res); try { using (Stream file = File.Create(path + @"\" + resourceName)) { CopyStream(stream, file); } Process.Start(path + @"\" + resourceName); }catch (IOException ex) { MessageBox.Show(ex.Message); } } public void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[8 * 1024]; int len; while ((len = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, len); } } 

Hope this helps you with your problems.

Sincerely.

+1
source
 //save resource to disk string strPathToResource = @"c:\UTReportTemplate.xls"; using (FileStream cFileStream = new FileStream(strPathToResource, FileMode.Create)) { cFileStream.Write(Resources.UTReportTemplate, 0, Resources.UTReportTemplate.Length); } //open workbook Excel.Application xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(strPathToResource); if (xlWorkBook == null) { MessageBox.Show("Error: Unable to open Excel file."); return; } xlApp.Visible = false; 
0
source

Source: https://habr.com/ru/post/943587/


All Articles