2013-04-25 8 views
6

Excelファイルを読み込んで書き込む必要があります。私はすでにファイルにリソースを追加し、ビルドアクションを組み込みリソースに設定しました。私の問題は、リソース/アセンブリからロードできないようです。私は現在このコードを持っています:C#のリソース/アセンブリから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; 

私は間違っていますか?ファイルにアクセスするにはどうしたらいいですか?どんな助けもありがとう。ありがとう。

+0

filestreamを使用していません....ファイルが埋め込みリソース名であるところで開くことはできません。 –

+0

どうすればよいですか?私はこれに新しいです、そして、私は現在どのように進むべきかについて全く知らないです。私はこれについてhttp://support.microsoft.com/kb/319292で読んでみました。しかし、私はどのように理解するのが苦労しています.. – jaqui

+0

ファイルストリームを場所に保存してからロードしてください! –

答えて

13
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); 
    } 
} 
+0

ありがとう!これはうまくいった。 – jaqui

+0

歓迎ですが、回答の横にチェックボックスがあります。これを確認すると、いくつかのポイントが得られ、問題が解決されたことを皆に知らせることができます。がんばろう! –

0
//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; 
+1

答えを教えていただけますか? – jaibatrik

+0

ファイル(UTReportTemplate)をリソースからディスクに保存する – Sergey1380528

1
私は、Visual Studio 2015でうまく動作しますここに私のコードスニペットを、追加したい

が(ただ、改善Jeremy Thompsonanswer。)

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

これはあなたのお手伝いをします。

よろしくお願いいたします。

関連する問題