Reputation: 387
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
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
Reputation: 7918
You can create Excel template file (.xlt
) and then open new .xls
files based on that template. Regards, AB
Upvotes: 0
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
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:
filestream
or a filename
.filename
to another process (such as Excel) and open it from the filesystem. Upvotes: 1
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