This article describes how to automate Microsoft Excel 97, Microsoft Excel 2000, Microsoft Excel 2002, or Microsoft Excel 2003 by using the Edraw Office Viewer Component. Specifically, this article illustrates how to get the worksheet dispatch interface.
1. Open Visual Studio .NET
2. Right-click on the toolbox and select “Choose Items…”
3. Select the COM Components Tab
4. Check Edraw Office Viewer Component and click OK Button
5. The control should appear in the toolbox as “Edraw Office Viewer Component”
6. Double-click on the control to add to your form
7. Resize and move the control on the form as desired
8. Add a button to the form
9. Double-click on the button to access code editor and enter the following code within the Click event:
‘Note: Modify code to point to an existing office document file on your web server
EdrawOfficeViewer1.Open “http://www.ocxt.com/demo/sample.xls”
10. Change the anchor property of EdrawOfficeViewer1 to Top, Bottom, Left, Right
11. Run the application and click on the button. The Excel program should appear!
For this automation process we need to follow the below steps
1. Add a referrence to the Microsoft Excel object library COM component.
2. Add the namespace Excel
3. Instantiate the class Excel.ApplicationClass as below
Excel.Application xl=EdrawOfficeViewer1.Application
4. To read cell(s) in the worksheet,
Excel.Sheets xlsheets = wb.Sheets; //Get the sheets from workbook
Excel.Worksheet excelWorksheet = (Excel.Worksheet)xlsheets[1]; //Select the first sheet
Excel.Range excelCell = (Excel.Range)excelWorksheet.get_Range(“B4:FZ4”, Type.Missing); //Select a range of cells
Excel.Range excelCell2 = (Excel.Range)excelWorksheet.get_Range(“A5:A5″, Type.Missing); //Select a single cell
Console.WriteLine(excelCell2.Cells.Value2.ToString()); //Print the value of the cell for a single cell selection
System.Array myvalues = (System.Array)excelCell.Cells.Value2; //Assign it to an array
string[] strArray = ConvertToStringArray(myvalues); //Convert array into String array
foreach (string str in strArray)
Console.WriteLine(” Text in Cell ” + str); //Loop through the array to print the values in the cell
Conclusion
Excel is a great tool to work with. When it comes to automating, we need to consider many things. Always remember to quit the excel application in code before exiting. If not, the memory consumed by the excel application will not be freed up.
Leave a Reply
You must be logged in to post a comment.
Recent Comments