Boundinashes6
Boundinashes6

Reputation: 387

c# using excel file from embedded resource

i'm writing a grading program for my girlfriend and i'm stuck trying to output the data to an excel file I embedded into the program. I have it writing to a blank excel file currently but would like to use a pre-made excel file and just export the data to the appropriate cells. I can't figure out how to tell the program to use the xls file in the resource folder instead of making a blank excel file. Here is the code for saving it so far. I'm using C# 2008 express edition.

Thanks

my rescource reference is: Properties.Resources.gradesheet

Excel.Application xlApp;
        Excel.Workbook xlWorkBook;            
        Excel.Worksheet xlWorkSheet;

        object misValue = System.Reflection.Missing.Value;


        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Add(misValue);

        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        //add data to excel
        xlWorkSheet.Cells[1, 1] = firstName;
        xlWorkSheet.Cells[2, 1] = lastName;




        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

Upvotes: 3

Views: 14266

Answers (5)

Darien shannon
Darien shannon

Reputation: 61

This should help you out. (tested in .NET 4.5).

public void openExcelTemplateFromResources ()
{
    string tempPath = System.IO.Path.GetTempFileName(); 

    System.IO.File.WriteAllBytes(tempPath, Properties.Resources.excelResource);

    Excel.Application excelApplication = new Excel.Application();
    Excel._Workbook excelWorkbook;
    excelWorkbook = excelApplication.Workbooks.Open(tempPath)

    excelApplication.Visible = true; // at this point its up to the user to save the file
}

Upvotes: 4

Alexander Bell
Alexander Bell

Reputation: 7918

You can create Excel template file (.xlt) and then open new .xls files based on that template. Regards, AB

Upvotes: 0

algreat
algreat

Reputation: 9002

First you should get stream of your resource file using this function:

 public static Stream GetResourceFileStream(string fileName)
            {
                Assembly currentAssembly = Assembly.GetExecutingAssembly();
                // Get all embedded resources
                string[] arrResources = currentAssembly.GetManifestResourceNames();

                foreach (string resourceName in arrResources)
                {
                    if (resourceName.Contains(fileName))
                    {
                        return currentAssembly.GetManifestResourceStream(resourceName);
                    }
                }

                return null;
            }

Then you can work with stream or you can save the stream to the file: How do I save a stream to a file.

Upvotes: 0

MadBoy
MadBoy

Reputation: 11104

As John Koerner says Excel works on files so you need your Excel file to be saved to file first.

However if you can work with .XLSX rather then .XLS you would be better of with EPPlus rather then Interop to create Excel files. It's easier and doesn't have all the problems that Interop does.

Based on this link depending on what you want to do with the Excel file, and how you're going to use it, the scenario differs a little. You can:

  • Pass the stream directly to an object that supports a stream as parameter. For example, Excel Package Plus supports reading an excel file directly from any stream and can load the object into memory. You can then manipulate the object and write it to a file calling the Save method, passing either a filestream or a filename.
  • Save the stream to a (temporary) file by creating a writable Filestream object with the correct target filename (using File.OpenWrite) and then calling yourManifestResourceStream.CopyTo(yourFileStream). You can then pass the filename to another process (such as Excel) and open it from the filesystem.

Upvotes: 1

John Koerner
John Koerner

Reputation: 38079

You will need to take the excel file from your resources and save it off to a file. Then you can do an open on the workbook:

xlApp.Workbooks.Open(fileName);

Upvotes: 2

Related Questions