How-To Develop Excel Applications With C-Sharp and VSTO 2008

Making applications with Excel 2007 and C# isn’t generally clear as large numbers of us have discovered. For instance you can’t utilize a full scale to compose code for you as you would with VB.net (VBA). To add to the difficulty you will track down when programming with C# in Excel 2007 or other Office items you should get to the APIs diversely contrasted with utilizing VBA. So there is somewhat of an expectation to learn and adapt.

All things considered, Microsoft offers full help for C# in the Excel 2007 API, through the Visual Studio 2008 Tools for Office 2007 (or 2003). Both are accessible as independent downloads from the MSDN website. 作业代写

This article is planned as a section point for engineers who, similar to me, either need to utilize C# to automatic partner interface with Excel (2007) or in light of the fact that it is their language of decision. This article will layout which references to incorporate; how-to introduce an exercise manual; access a gathering of worksheets; or only one specifically; how-to access and change a phone. Every one of the models will utilize the VSTO (Visual Studio Tools for Office 2007) project layouts.

Exercise manual Project Example

This model is a straightforward stroll through to show you the fundamentals of programming with Microsoft C# with Excel 2007. When programming with the VSTO 2007 you need to see the Excel 2007 document as a Windows Forms customer. You communicate with this structure as you would with some other Windows Form in.Net.

If you haven’t done as such, you will have to download and introduce VSTO 2008. You will find the tool compartment on MSDN. The establishment is speedy and simple. When you have arrangement the VSTO, make a task in Visual Studio 2008:

– >File

– >New

– >Project

On the other hand click Ctrl+Shift+N.

From the New Project Dashboard, Expand the C# hub and select the Office hub. On the rigth side, you will have a few Project Solution format to browse. For this model, select the Excel 2007 Workbook format. In the fitting fields, give your application, pick an area and a Solution Name. Ensure the “Make Directory for Solution” is checked.

In the following screen acknowledge the defaults which to utilize another exercise manual. You can likewise decide to utilize a current exercise manual assuming you need to add some usefulness to a current application. Keep the default “xlsx” document organization and snap OK to wrap up making the task.

When the task is made you will have an Excel exercise manual rather than the standard Windows Form. In the Solution Explorer there will have a C# (cs) record for each Excel worksheet and the Workbook.cs document. This follows a similar Excel format while making a standard Excel Spreadsheet.

At the highest point of the open ThisWorkbook.cs are the different references that are remembered for the layout. The VSTO Solution layout ought to have added the accompanying two references (see beneath) when the venture was arrangement. Anyway in case it isn’t true, you can reorder the code references into the code manager of the Workbook.cs record.

utilizing Excel = Microsoft.Office.Interop.Excel;

utilizing Office = Microsoft.Office.Core;

A large portion of the code is made from the format when the task is arrangement. For the models in this article you will add code to the “ThisWorkbook_Startup” which will be considered when the application is dispatched.

If you really want to reference the top sheet (Sheet1) when the Application is dispatched, you would utilize the accompanying code:

Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)this.ActiveSheet;

Then again, if you expected to reference another worksheet when the application is dispatched, you would utilize first get the assortment of the worksheets

Microsoft.Office.Interop.Excel.Sheets sheets = (Microsoft.Office.Interop.Excel.Sheets)this.Worksheets;

Then, at that point, you could understand the worksheet in the assortment:

Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(“Sheet2”);

To get to a cell and enter a cell you really want to get to a Range. This can be one cell or a scope of cells:

Microsoft.Office.Interop.Excel.Range away from home = (Microsoft.Office.Interop.Excel.Range)sheet.get_Range(“A1”,System.Reflection.Missing.Value);

afield.set_Value(System.Reflection.Missing.Value, “Hi World”);

The total code for the two models are given beneath:

Leave a Reply

Your email address will not be published. Required fields are marked *