How to create an excel sheet or chart in vc?

xiaoxiao2021-03-06  42

1. Require Files

When You Start to Program In Vc, You Must Have The Lib Files of Excel. The next Table Will show That:

Office Version Requirement files Other files Default Dir Office 97 Excel8.olb Excel.exe C: / program files / MS Office / office Office 2000 Excel9.olb Excel.exe C: / program files / MS Office / office Office 2002 Excel.exe

C: / Program Files / MS Office / Office10 Office XP Excel.exe

C: / Program Files / MS Office / Office10

Before You Start Work, You Must Be Sure That Corresponding Files Are There.

2. The Excel Object Model

BEFORE You Start To Work, Another Thing Must Be Known, It Is The Excel Object Model. It Tells US That What Are InCluded in Excel? Let us.

2.1. Basic Model

Excel program consists of eight main parts; these are application object, workbooks object, work book object, work sheets object, work sheet object, range object, charts, chart Next picture describes the relationship of main objects in Excel:.

NOTHER OBJECTS, but offten.com.

2.2. Application Object

Application Object Is Defined in Excel Type Library AS _Application Class. Application is The Excel Itself, The Main functions are:

l the setting and options of excel application level

L Some methods which return to top object

We can Get Workbooks / Workbook Object from _Application Object Like this:

_Application app;

app.get_workbooks ();

2.3. Workbooks Object

Workbooks is the aggregate of all Open books. It is container object, The element is workbook object.

The main functions are:

l retturn the workbook object by index.

l Add a new empty workbook.

l Open a file, and create a new workbook for this file.example:

Workbooks books = app.get_workbooks ();

Workbook newbook = books.add (votp);

Newbook = books.open (".// 1.xls", ...);

Newbook = books.get_item (Colvariant ((Short) 1);

2.4. Workbook Object

Workbook Object Is A Work Book. It includes work sheet and chart. The main functions are:

l activate a workbook

l Return a Worksheets or Charts

l retturn the active sheet

l save to file (xls)

EXAMPLE:

Newbook.activate ();

Worksheets sheets = newbook.get_worksheets ();

Newbook.get_Charts ();

Newbook.get_Activechart ();

Newbook.get_Activesheet ();

2.5. Worksheets Object

.

The main functions are:

l add new work sheet

l get Work Sheet by Index

EXAMPLE:

Worksheet Sheet = Sheets.Add (Vopt, Vopt, Vopt, Colevariant ((Short) 1));

Sheet = Sheets.get_Item (Index);

2.6. Worksheet Object

Worksheet Object Is A Work Sheet Of Excel. It Is The Member of Worksheets and Sheets.

The main functions are:

l All Operation on Work Sheet, Like Password.

l Return the Range Object by Cell Area.

l activate itself

EXAMPLE:

..........

Sheet.Put_name ("My Create Sheet");

Ranget orng = Sheet.get_Range (Colevariant ("A1: B3"), VOPT);

Sheet.Activate ();

2.7. Range Object

Range Object Is A Cell, OR A Row, OR A Column, OR A Area (It Maybe A Cell Or Some Continuous Cells, OR A 3D Area.

The main functions are:

l Get and set the cells value

l Get and set the cells formal offset

l

Union

l font, autofit, and so on ...

EXAMPLE:

ORNG.GET_VALUE ();

ORNG.PUT_VALUE (Colevariant ("Date");

Orange = Orange.get_resize (Colevariant ((long),

Colevariant ((long) 1));

Orange.put_formula (Colevariant ("= C2 * 0.07");

2.8. Charts

CHARTS IS A AGGREGATE OBJECT, IT INCLUDES All Charts in Workbook, But It Doesn't Contain Embedded Charts.

The main functions are:

l get chart by index

L Add a new chart to workbook

l Print Chart

EXAMPLE:

Charts charts = newbook.get_charts ();

Charts.get_Item (Index);

Chart Newchart = Charts.Add (Vopt, Vopt, Colevariant ((Short) 1));

2.9. Chart

Chart Represents Chart, IT Can Be A Embedded Chart OR A Single Chart.

The main functions:

l Set the Basic Attributes, E.G., Name, Title, Active.

l set the chart type

l set the chart data source

EXAMPLE:

Newchart.put_name ("my charT");

Newchart.put_charttype ((long) xllinemarkers;

Range orang;

ORANG = newsheet.get_range (Colevariant ("C2: D21"), VOPT);

Newchart.setsourceData (Orang, Colevariant ((Short) 2));

2.10. Chart Type

3. THE Step of create

Now let us start to create a project and write program for Creating An Excel Sheet and an Excel Chart.

We describe that in two steps. The first step is how to import type libraries and what type libraries are imported into project, and the second step is how to code. The second will be described in next section.

3.1. How and what

What Libraries Area Imported Into? Different Office Version Has Different Type Libraries, See Above Form.

There is Some Difference of How To Import Between VC6.0 and VC7.0.

3.1.1. VC6.01. CREATE A MFC EXE PROJECT

2. Select Menu "View-> Class Wizard"

3. SELECT OPTION CARD "Automation-> Add class-> from Type Library"

4. SELECT An Excel9.olb / Excel8.olb / Excel.exe File, Which often Locates Under Dir C: / Program Files / Office /.

5. Select specified classes, e.g. _Application, Workbooks, _Workbook, Worksheets, _Worksheet, Range, then click OK, and a file named excel9.h / excel8.h will be created. That file includes the definition of above classes.

See Next Pictures:

Picture 1:

Picture 2:

Picture 3:

Picture 4:

Picture 5:

3.1.2. VC7.0

1. Create a MFC EXE Project, Single Document, And Container

2. SELECT MENU "Project-> Class Wizard"

3. SELECT "Class in Type Library"

4. Click "Open" Button

5. Set the source of class as "file", and select the file.

6. SELECT The interfaces That You want to add your project from left list and insert The Into Right List

7. Set the import file name (Excel.h), The Click The "Complete" button.

See Next Pictures:

Picture 6:

Picture 7:

Picture 8

Picture 9:

4. Program with create Excel file

WE Suppose That All Classed Are Defined in Excel.h, So We can use those classes by Only Including Excel.h.

WE Add Two Menu Items, One for Creating Specified Excel Sheet Named ID_Newsheet, The Other for Creating Chart name id_newchart.

The Steps:

Add Two Menu Items

2. Add Two Message Map Functions for Above Two Menu Items

3. INCLUDE Excel.h file in the file you define Above Two Message Map Functions.

4. Implement The Two Functions

4.1. Program with Sheet

The next is a way of id_newsheet message map function implemented:

// Excel Object Model

_Application app;

Workbooks books; _Workbook newbook;

Worksheets sheets;

_Worksheet osheet, firstsheet;

Charts charts;

_Chart Chart, Firstchart;

Range Range;

Range ICell;

LPDISPATCH LPDISP;

Colevariant vresult;

Colevariant Vopt ((long) DISP_E_PARAMNOTFOUND, VT_ERROR);

// Create Excel Server (START Excel)

IF (! app.created ")))

{

AfxMessageBox ("Can't Start Excel Server!");

Return;

}

App.put_visible (TRUE); // SET Excel Visible

App.put_userControl (True); // User CAN Operate Excel

// new a book

Books.attachdispatch (app.get_workbooks ());

Newbook = books.add (vopt);

// Get Worksheets and get the first worksheet

SHETS = Newbook.get_worksheets ();

Osheet = Sheets.get_Item (Colevariant ((Short) 1);

// *** Add data to the worksheet

// add headers to rotate 1 of the worksheet

Range orange;

Orange = osheet.get_range (Colevariant ("A1"), VOPT);

Orange.put_Value2 (Colevariant ("Date");

Orange = Osheet.get_Range (Colevariant ("B1"), VOPT);

Orange.put_value2 (Colevariant ("Order #");

Orange = Osheet.get_Range (Colevariant ("C1"), VOPT);

Orange.put_Value2 (Colevariant ("Amount"));

Orange = osheet.get_range (Colevariant ("D1"), VOPT);

Orange.Put_Value2 (Colevariant ("TAX");

// Create a Safe Array That Is Numrows x 3 -

// Column 1 Will Contain Dates Column 2 Will Contain Strings

// and column 3 Will Contain Numbers

ColesafeArray SA;

DWORD DWELEMENTS [2];

DWELEments [0] = 20; // Number of Rows

DWELEments [1] = 3; // Number of Columns

sa.create (VT_VARIANT, 2, DWELEMENTS);

// Populate the Safe Array with the data

Long Index [2];

Long LROW;

Colevariant vTemp;

COLEDATETIME VDATIME;

CString S;

For (LROW = 0; LROW <= 20-1; LROW ) {

Index [0] = LROW;

// Fill The First Column with dates

INDEX [1] = 0;

VDATETIME.SETDATE (1999, RAND ()% 12, RAND ()% 28);

Sa.putelement (Index, Colevariant) vdatetime;

// Fill The Second Column with strings

INDEX [1] = 1;

S.Format ("ORDR% D", LROW 1000);

vTemp = S;

Sa.putelement (index, vtemp);

// Fill The Third Column with Numbers

INDEX [1] = 2;

VTEMP = (long) Rand ();

Sa.putelement (index, vtemp);

}

// Fill A Range, Starting at a2 with the data in

// the Safe Array

Orange = osheet.get_range (Colevariant ("A2"), VOPT);

Orange = Orange.get_resize (Colevariant ((Short) 20),

Colevariant (short));

Orange.put_Value2 (SA);

sa.detach ();

// *** Add formulas to the worksheet

// Fill The Fourth Column with a formula to compute the

// Sales Tax. Note That The Formula Uses A "Relative"

// Cell Reference SO That IT Fills Properly.

Orange = osheet.get_range (Colevariant ("D2"), VOPT);

Orange = Orange.get_resize (Colevariant ((long),

Colevariant ((long) 1));

Orange.put_formula (Colevariant ("= C2 * 0.07");

// *** Format the worksheet

Orange = osheet.get_range (Colevariant ("A1"), Colevariant ("D1"));

/ * Font ofont = orange.get_font ();

Ofont.setBold (Colevariant (short)); // Apply Bold To Headers * /

Orange = orange.get_entirecolumn ();

Orange.autofit (); // Autofit The Column 1: 4

// Make Excel Visible and Give The User Control

Osheet.Put_name ("My New Sheet");

Newbook.saveas (Colevariant ("c: //mynew.xls"), Vopt, Vopt,

VOPT, VOPT, VOPT, 0,

VOPT, VOPT, VOPT, VOPT, VOPT

Newbook.close (VOPT, Colevariant ("C: //mynew.xls" / * outfilename * /), VOPT);

Books.close (); app.quit ();

4.2. Program with chart

The next is a way of id_newchart message Map Function Implementation:

// New Create Excel Object Model

_Application app;

_Workbook newbook;

Workbooks books;

Worksheets sheets;

_Worksheet newsheet;

Charts charts;

_Chart newchart;

Colevariant Vopt ((long) DISP_E_PARAMNOTFOUND, VT_ERROR);

// Create Excel Server (START Excel)

IF (! app.created ")))

{

AfxMessageBox ("Can't Start Excel Server!");

Return;

}

//app.put_visible (true); // set Excel Visible

App.put_userControl (True); // User CAN Operate Excel

Books = app.get_workbooks ();

Newbook = books.open ("c: //mynew.xls", VOPT, VOPT, VOPT, VOPT, VOPT,

VOPT, VOPT, VOPT, VOPT, VOPT, VOPT, VOPT, VOPT, VOPT

// newbook = books.add (vopt);

SHETS = newbook.get_sheets ();

Newsheet = shop.get_item (Colevariant ((Short) 1);

Charts = newbook.get_charts ();

CSTRING TIP;

// for (; M_ChartType <100; M_ChartType )

{

Try

{

IF (charts.get_count ()> 0)

Charts.delete ();

Newchart = charts.add (Vopt, Vopt, Colevariant ((Short) 1);

Newchart.put_name ("my charT");

Newchart.put_charttype ((long) xllinemarkers;

Tip.format ("c: //mynewchart%d.xls", XLLINE);

// MessageBox (TIP);

Range orang;

ORANG = newsheet.get_range (Colevariant ("C2: D21"), VOPT);

Newchart.setsourceData (Orang, Colevariant ((Short) 2));

Newchart.put_haastit (true);

Charttitle Ochartttl = Newchart.get_Charttitle ();

Ochartttl.put_text ("My Sample Xy-Scatter Chart");

Newchart.put_haslegegend (false);

Newbook.saveas (Colevariant (TIP), VOPT, VOPT,

VOPT, VOPT, VOPT, 0,

VOPT, VOPT, VOPT, VOPT, VOPT

}

Catch (...)

{

//newbook.close(VOPT, Colevariant (tip/*outfilename * /), vopt); // app.quit ();

//undinue;

}

}

//newbook.close (Vopt, Colevariant (TIP / * OUTFILENAME * /), VOPT);

Books.close ();

app.quit ();

转载请注明原文地址:https://www.9cbs.com/read-80614.html

New Post(0)