business students sharpen c# programming skills with visual studio tools for microsoft office

Post on 30-Dec-2015

36 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Business Students Sharpen C# Programming Skills with Visual Studio Tools for Microsoft Office. Jerry Chin Sheryl Brahnam Mary Chin College of Business Administration Southwest Missouri State University. Agenda. - PowerPoint PPT Presentation

TRANSCRIPT

Business Students Sharpen C# Programming Skills with

Visual Studio Tools for Microsoft Office

Jerry ChinSheryl Brahnam

Mary Chin

College of Business AdministrationSouthwest Missouri State University

Agenda

In October of 2003, Microsoft released a new application package to create project templates for document-centric solutions for the host applications Word and Excel.

Using Microsoft Visual Basic or C#, code modules can be created in much the same way as using Visual Basic for Application (VBA).

Agenda

This presentation discusses the development of a C#/Excel student assignment.

This presentation provides a systematic view of the relationships between source modules, internal data structures, and the worksheet.

Student Background

Assumptions Have taken a least a beginning class in a

business school – CIS environment. Students having completed a microcomputer

application course which features word processing, spreadsheets, database, and basic Web development.

Student Background

Assumptions Students have some background in

programming and have made the fundamental jump to object-oriented programming.

Microsoft Visual Studio Tools for Office (MSVSTO)

Reflect Microsoft’s recognition that technology is evolving into other parts of an organization that is not directly related to MIS/Technology department.

VBA is not limited or falling out of favor BUT . . .

They acknowledge that existing programming staff is more familiar with a C-flavored syntax.

Microsoft Visual Studio Tools for Office (MSVSTO)

Students are familiar with VBA/Excel applications.

We use MSVSTO to extend this familiarity into a C# with Excel example.

Microsoft Visual Studio Tools for Office (MSVSTO)

At this time there are few “how-to” books concerning the VSTO software package.

The book, “Using Microsoft Visual Studio Tools for the Microsoft Office System: Msm2052acppb” has a January 2004 print date.

The Marketing Problem

Assume that QueValue managers desire to measure the sales activity in the eight US distribution centers conveniently located at or near hub sites of national ground/air package service companies.

The Marketing Problem

Each regional manager reports the activity of pre-selected items based upon corporate projections and market analysis.

The Marketing Problem

A quick analysis by the Applications Group has determined that one of the forms of the project is a four-part input form for following:Center, Number of Items, Item Number, transaction date.

The Marketing Problem

Suppose that the Marketing department has a number of marketing directives triggered by individual managers using the system. For example, MD123 might require that if the

number of Units exceeds 1000, then the order is automatically increased by 20% and diverted to the center located in Canton, OH.

Component Generation

using Microsoft’s Visual Studio Tools for Office (VSTO), the following components were generated: QueForm.cs QueForm.cs[Design] Center.cs (In C# all functions and data members

must be accessible from a class) dataOhio (dataset, essentially cache or in-memory

copy of data) dataTableOhio (visual gateway to the data) ThisWorkbook.cs (becomes visible at run time)

The Cleveland OH Center form

Setting Up the Machinery

Class.cs

Form.cs

Form/Design Database

Excel

ThisworkBook.cs

1

53

4

2

The Form and its Code

Creating a form called “QueForm”, generates a corresponding C# code module called, “QueForm.cs” as a class with a similar name in a namespace QueForm.

Any data in the form can be accessed by reference to the appropriate textbox.

We use the class called Center and load its class members with form data (arc 4).

The Form and its Code

1. Center c = new Center(); //a new center object

2. c.center = textBox1.Text;

3. c.date = textBox3.Text;

4. c.units = textBox2.Text;

5. c.units_num = textBox4.Text;

6. Center.recordcnt ++; // Bump Record Counter

7. CheckMD123(ref c);

The Form and its Code

We implement MD321 as part of QueValue’s processing requirements. The call CheckMD321 passes the Center object to the following procedure:

1. private void CheckMD123(ref Center x )2. {3. double work1, work2;4. work1 = Convert.ToDouble(x.units_num);5. if (work1 > 1000.00)6. {work2 = work1 * 1.20;7. x.center = "Canton";}8. else work2 = work1;9. 10. x.units_num = Convert.ToString(work2);}

The Form and its Code

The data on the form is ready to be written to the Excel worksheet. We first write to a data table linked to the datagrid on the form:

1. DataRow myrow = dataTableOhio.NewRow();2. myrow["Center"] = c.center;3. myrow["Item Number"] = c.units;4. myrow["Date"] = c.date;5. myrow["Units"] = c.units_num;6. dataTableOhio.Rows.Add(myrow); 7. // add a row to table

The Form and its Code

To accomplish arc 2, we pass the Center object and the current record count as parameters to the code module called ThisWorkBook.cs :

1. int reccount = Center.recordcnt;2. this.excelCode.GoToExcel(c,

reccount);

The Form and its Code

Class.cs

Form.cs

Form/Design Database

Excel

ThisworkBook.cs

1

53

4

2

The Form and its Code

The procedure GoToExcel in ThisWorkBook.cs has the following structure:

1. public void GoToExcel(Center c, int count ) 2. { 3. Excel.Worksheet s1 = 4. (Excel.Worksheet)this.ThisApplication.5. Sheets.get_Item("Sheet1"); 6. ((Excel.Range)s1.Cells[count+1,1]).Value2 = c.center;7. ((Excel.Range)s1.Cells[count+1,2]).Value2 = c.date;8. ((Excel.Range)s1.Cells[count+1,3]).Value2 = c.units;9. ((Excel.Range)s1.Cells[count+1,4]).Value2 = c.units_num;10. ((Excel.Range)s1.Cells[1,1]).Value2 = count;11. }

The Form and its Code

The “Cells[row,col]” syntax indicates that the data is being assigned to cells in the Excel worksheet. This is arc 3.

Class.cs

Form.cs

Form/Design Database

Excel

ThisworkBook.cs

1

53

4

2

Form & Excel Worksheet

What have we accomplished?

User has entered data on the form. Data has been processed. Data has been entered on a worksheet.

Can we access data from the worksheet?

On the form in Figure 1 there is a button labeled, “Excel To Data Table”. The code connected to that button event is the following:

1. public void ExcelToTable(ref DataTable d)2. {3. Excel.Worksheet s1 =4.

(Excel.Worksheet)this.ThisApplication.Sheets.get_Item("Sheet1");

5. Excel.Range rng2;6. rng2 = (Excel.Range)s1.Cells[1,1]; 7. int MaxRow = Convert.ToInt16(rng2.Value2);8. // add 1 to Max 9. for (int Rindex = 2; Rindex <= MaxRow +1 ; Rindex++) 10. {

Can we access data from the worksheet?

Code Continued . . . 1. DataRow r = d.NewRow();2. rng2 = (Excel.Range)s1.Cells[Rindex,1];3. r["Center"] = rng2.Value2;4. rng2 = (Excel.Range)s1.Cells[Rindex,2];5. r["Item Number"] = rng2.Value2;6. rng2 = (Excel.Range)s1.Cells[Rindex,3];7. r["Date"] = rng2.Value2;8. rng2 = (Excel.Range)s1.Cells[Rindex,4];9. r["Units"] = rng2.Value2;10. d.Rows.Add(r);11. }12. }

Can we access data from the worksheet?

Any data in the data table can be accessed and sent back to the appropriate textboxes in QueForm.

Using code very much like code associated with arc 3, any information in the datatable can be inserted back into the Excel worksheet.

In the following code “d” can be interpreted as a reference to datatableOhio passed to a procedure in ThisWorkBook.cs.

1. ((Excel.Range)s1.Cells[ExcelIndex,1]).Value2 = d["Center"];2. ((Excel.Range)s1.Cells[ExcelIndex,2]).Value2 = d["Item

Number"];3. ((Excel.Range)s1.Cells[ExcelIndex,3]).Value2 = d["Date"];4. ((Excel.Range)s1.Cells[ExcelIndex,4]).Value2 = d["Units”];

From dataset to worksheet

Arc 5

The process flow designated by arc 5 is now established.

Class.cs

Form.cs

Form/Design Database

Excel

ThisworkBook.cs

1

53

4

2

CONCLUSION

The new software Microsoft Visual Studio Tools for Office is an excellent way for students, not necessarily CIS majors, to combine their familiarity of Word or Excel with their C# programming / analysis development.

Business Students Sharpen C# Programming Skills with

Visual Studio Tools for Microsoft Office

Jerry ChinSheryl Brahnam

Mary Chin

College of Business AdministrationSouthwest Missouri State University

top related