hi, i have seen most of the time we require Automation process in .net
programming, Automation is a process that permits applications that are
written in languages like C#, classic asp, vb.net to control other
applications.like creating new Workbook, Store the result into excel file , desing a chart and so many. You can do this manually also and
programetically also by using AUTOMATION
in C# we have an Application object, a Workbook object, and a Worksheet
object, each of which contain the functionality of those pieces of Excel.
but for that we have to set the proper project reference to the Excel type
library in C#.
Steps to Create an Automation Client for Microsoft Excel.



Creating sample automation example:
1. add a Button to form1 and double-click on button1.
2. it will create this code
private void button1_Click(object sender, System.EventArgs e)
{
}
3.include excel directive on you’r page
using Excel = Microsoft.Office.Interop.Excel;
4.now create object for workbook, worksheet and application also.
//this will Start Excel and get Application object.
Excel.Application XL = new Excel.Application();
//Create new workbook
Excel._Workbook WB;
WB = (Excel._Workbook)(XL.Workbooks.Add( Missing.Value ));
Excel._Worksheet WSheet = (Excel._Worksheet)WB.ActiveSheet;
5. Now you can add data into excel file. you can select one cell
and with the help of that add new record with same.
like :
WSheet.Cells[1, 1] = “ID”;
WSheet.Cells[1, 2] = “Password”;
6. if you want to set the field as a bold
WSheet.get_Range(“A1″, “B1″).Font.Bold = true;
7. now u can add records on that.
string[] Result = new string[1,2];
Result [0, 0] = “Kiran”;
Result [0, 1] = “Automation”;
8. it will fill the records.
WSheet.get_Range(“A1″, “B1″).Value2 = Result;
9. now save you’r work and close excel
WB.SaveAs(nameoffile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, null);
WB.Close(false,nameoffile, Missing.Value);
Known Issues with Automation
Because the CLR uses a garbage collector for memory management, and because your RCW is itself a managed object, the lifetime of your Automation object is not guaranteed to end.you can get the actual help from here
317109 : Office Application Does Not Quit After Automation from Visual Studio .NET Client
Hope it will help you.
Thank You,
Leave a reply