Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# using excel file from embedded resource

Tags:

c#

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);
like image 922
Boundinashes6 Avatar asked Feb 21 '23 10:02

Boundinashes6


1 Answers

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
}
like image 148
Darien shannon Avatar answered Feb 23 '23 23:02

Darien shannon