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);
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With