2013-04-25 4 views
6

में संसाधन/असेंबली से एक्सेल फ़ाइल लोड करें मुझे एक्सेल फ़ाइल लोड करने और उस पर लिखने की आवश्यकता है। मैंने संसाधनों में फ़ाइल पहले ही जोड़ दी है और एंबेडेड संसाधन में अपनी बिल्ड एक्शन सेट की है। मेरी समस्या यह है कि मैं इसे संसाधन/असेंबली से लोड नहीं कर सकता। मेरे पास वर्तमान में यह कोड है:सी #

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

मैं अपने कोड स्निपेट यहाँ है, जो दृश्य स्टूडियो 2015 में अच्छी तरह से काम जोड़ना चाहते हैं (बस में सुधार Jeremy Thompson के answer।)

(संपत्ति विंडो में एक्सेल फ़ाइल संसाधन की 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); 
    } 
} 

आशा है कि इससे आपको आपकी परेशानी में मदद मिलेगी।

सर्वश्रेष्ठ संबंध।