Load excel file from resources/assembly in C#

青春壹個敷衍的年華 提交于 2019-11-30 08:33:17

问题


I need to load an Excel file and write on it. I have already added the file to resources and set its build action to Embedded Resource. My problem is I can't seem to load it from the resources/assembly. I currently 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 appreciated. Thanks.


回答1:


You need to extract the resource (in this case an excel spreadsheet) from the assembly and write it as a stream to a File, eg:

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);
     }
}



回答2:


//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;



回答3:


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

(Don't forget to set the Excel file resource's Build Action Property to Embedded Resource in Property 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 will help you with your trouble.

Best regards.



来源:https://stackoverflow.com/questions/16205252/load-excel-file-from-resources-assembly-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!