This is a small Excel Example that I've created. The user must have Excel installed on their computer. You, the developer, must have the Excel Com object 11.0 included as a reference. The code should be fairly simple to follow.
Enjoy!
using System.Reflection;
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
private void btnProcess_Click(object sender, system.EventArgs e)
{
try
{
oXL = new Excel.Application();
oXL.Visible = true;
oWB = ( Excel._Workbook ) ( oXL.Workbooks.Add( Missing.Value ) );
oSheet = ( Excel._Worksheet ) oWB.ActiveSheet;
oSheet.Cells[1,1] = "Row 1 Column 1";
oSheet.Cells[1,2] = "Row 1 Column 2";
oSheet.Cells[2,1] = "Row 2 Column 1";
oSheet.Cells[2,1] = "Row 2 Column 2";
// Add a worksheet
Excel.Worksheet newWorkSheet;
newWorkSheet = ( Excel.Worksheet ) oWB.Worksheets.Add( Type.Missing, Type.Missing, Type.Missing, Type.Missing );
// Select a worksheet
( ( Excel.Worksheet ) oXL.ActiveWorkbook.Sheets[3] ).Select( Type.Missing ); // The third worksheet will get focus.
oSheet = ( Excel._Worksheet ) oWB.ActiveSheet;
// Name the WorkSheet
oSheet.Name = "This is Worksheet 3";
// Add data to the Worksheet
oSheet.Cells[1,1] = "Worksheet 3 Row 1 Column 1";
oSheet.Cells[1,2] = "Worksheet 3 Row 1 Column 2";
oSheet.Cells[2,1] = "Worksheet 3 Row 2 Column 1";
oSheet.Cells[2,2] = "Worksheet 3 Row 2 Column 2";
// Save file "Will prompt for an overwrite condition" so you should handle this progmaticly
oWB.SaveAs(@"C:\Excel Demo.XLS", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing );
// Close down the workbooks
oWB.Close( false, Type.Missing, Type.Missing );
// Close Down Excel
oXL.Quit();
}
catch ( Exception theException )
{
MessageBox.Show( "Error " + theException );
}
}