Business Students Sharpen C# Programming Skills with Visual Studio Tools for Microsoft Office

32
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

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 of Business Students Sharpen C# Programming Skills with Visual Studio Tools for Microsoft Office

Page 1: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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

Page 2: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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).

Page 3: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 4: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 5: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

Student Background

Assumptions Students have some background in

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

Page 6: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 7: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 8: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 9: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 10: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

The Marketing Problem

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

Page 11: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 12: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 13: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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)

Page 14: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

The Cleveland OH Center form

Page 15: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

Setting Up the Machinery

Class.cs

Form.cs

Form/Design Database

Excel

ThisworkBook.cs

1

53

4

2

Page 16: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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).

Page 17: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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);

Page 18: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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);}

Page 19: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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

Page 20: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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);

Page 21: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

The Form and its Code

Class.cs

Form.cs

Form/Design Database

Excel

ThisworkBook.cs

1

53

4

2

Page 22: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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. }

Page 23: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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

Page 24: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

Form & Excel Worksheet

Page 25: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

What have we accomplished?

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

Page 26: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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. {

Page 27: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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. }

Page 28: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 29: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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

Page 30: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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

Page 31: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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.

Page 32: Business Students Sharpen C# Programming Skills with  Visual Studio Tools for Microsoft Office

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