It is possible to do.
You would need to utilize the Excel Interop namespace, specifically the Microsoft.Office.Interop.Excel namespace.
Documentation is here:
https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.aspx
It gives the capabilities to write to Excel files, update, alter and so on.
In short, you would assign each cell the specific text or label information that you would want.
For example, you would have in a method something along the lines of:
// Put this in the namespace references sections
using Excel = Microsoft.Office.Interop.Excel;
//inside your class(es) that will be writing to Excel
Excel.Application xlApp; //instance of the Excel application
Excel._Workbook xlWB; //will become the workbook object of the Excel application
Excel._WorkSheet xlWS; //will become the worksheet object of the Excel application
object misval = System.Reflection.Missing.Value; //used to cover certain arguements for saving (as far as I gather)
public void ExcelWriter()
{
xlApp = new Excel.Application; //starts an instance of Excel
xlApp.Visible = false; //hides the application from appearing on screen
xlApp.Dialog = false; //keeps the "Save As" dialogs from appearing when it goes to save
//adds a new workbook to the Excel application and puts a new sheet in it
xlWB = (Excel._Workbook)(xlApp.Workbooks.Add("Name of Book Goes Here"));//your workbook instance
xlWS = (Excel._WorkSheet)xlWS.ActiveSheet;//your worksheet instance
//each cell is referenced along an array
//for example, cell A1 is 1,1
xlWS.Cells[1,1] = label1.Text;
xlWS.Cells[1,2] = label2.Text;
xlWS.Cells[1,3] = label3.Text;
xlWS.Cells[1,4] = label4.Text;
.....
//whatever executable code you'd like here
//formatting of cells, fonts, etc.
xlWB.SaveAs("C:\\Path\Goes\Here", misval, misval, misval,
misval, misval, misval, misval, misval,
misval, misval, misval, misval);
xlWB.Close();
xlApp.Quit();
}
But that leads into a great deal of question such as:
- Do you need to open an existing file, create a new one each time, etc?
- Do you want the user to verify the Excel page is correct before saving?
- Is the Excel formatting extremely important?
- Are you making a new worksheet each time? A new workbook?
- Is this coming from a template worksheet or building a new one from scratch each time?
I will also say this: the Interops can be VERY finicky with closing applications correctly. If anywhere you are generating errors that cause your app to close or not quit Excel properly, it will leave the Excel instance running. Be sure to make check your Task Manager, check for exceptions, and make sure that Excel exits when its supposed to.
Some other posts to reference:
quitting excel with C# (while using excel automation)
Writing to Excel using C#