9

我需要加载一个 Excel 文件并在上面写字。我已经将该文件添加到资源并将其构建操作设置为嵌入式资源。我的问题是我似乎无法从资源/程序集中加载它。我目前有这个代码:

 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;

我究竟做错了什么?如何访问该文件?任何帮助,将不胜感激。谢谢。

4

3 回答 3

16

您需要从程序集中提取资源(在本例中为 excel 电子表格)并将其作为流写入文件,例如:

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);
     }
}
于 2013-04-25T02:26:01.917 回答
1
//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;
于 2016-02-27T19:53:11.197 回答
1

我想在这里添加我的代码片段,它在 Visual Studio 2015 中运行良好。(只是改进了Jeremy Thompson答案。)

(不要忘记在属性窗口中将 Excel 文件资源的Build Action属性设置为。)Embedded Resource

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

希望这能帮助你解决你的麻烦。

此致。

于 2017-01-14T07:12:57.117 回答