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:
1.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 implementation:
//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.CreateDispatch("Excel.Application"))
{
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
sheets = newBook.get_Worksheets();
oSheet = sheets.get_Item(COleVariant((short)1));
//*** ADD DATA TO THE WORKSHEET
//Add Headers to Row 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
longindex[2];
longlRow;
COleVariant vTemp;
COleDateTime vDateTime;
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)3));
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)20),
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)TRUE));//Apply Bold to Headers*/
oRange = oRange.get_EntireColumn();
oRange.AutoFit();//AutoFit the columns 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;
Chartscharts;
_Chart newChart;
COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//Create Excel server (start Excel)
if(!app.CreateDispatch("Excel.Application"))
{
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);
sheets = newBook.get_Sheets();
newSheet = sheets.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_HasTitle(true);
ChartTitle oChartTtl = newChart.get_ChartTitle();
oChartTtl.put_Text("My sample xy-scatter chart");
newChart.put_HasLegend(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();
//continue;
}
}
//newBook.Close (vOpt,COleVariant(tip/*OutFilename*/),vOpt);
books.Close();
app.Quit();
相关推荐
经典VC操作Excel表格实例程序,读写Excel表格数据,Excel与CSV格式转换。
可扩展类vc操作excel表格,可以很方便实现一些基本操作.建表,插入,查询 行 列 删除 插入单元格元素
VC操作EXCEL,用最简单的自带函数实现对sheet的定位写入。还实现了对EXCEL进程的资源释放。
VC通过ODBC操作EXCEL表格的代码,包括读入,输出等,
VC操作excel,简化使用vc来生成excel表格的步骤
收集的vc中如何和excel交互,读取和导出到Excel,实用
在vc中将数据写到word和excel中
VC建立excel文件并操作表格内容的例子工程 分别演示了: 打开一个excel文件 写入各项内容 建立一个表格内容 发表博客为:https://thanklife.blog.csdn.net/article/details/119383499
实现了VC++6.0平台从窗口选择EXCEL读取其内容的功能
VC对Excel添加超链接
VC创建Excel表格的基本介绍。一些基本函数和操作。
vc实现excel表格的写入 生成到D盘 目录下 代码我验证过的
vc调用excel,导入数据,显示打印页面。
利用vc创建excel2003表格,字体、背景、边框、合并单元格
利用vc创建excel表格,字体、背景、边框、合并单元格(源程序)
VC 2008 MFC 操作Excel表格实例,使用VS2008开发的操作Excel的演示程序,增加了界面显示效果,部分类库直接使用了微软官方的类库。
支持气泡提示的VC Excel表格程序,VC 对Excel文件的操作,有两个重要文件,这里简要说下功能: ExcelAccessDlg.cpp文件: 对Excel文件进行新建、读写、内部数据行、列、单元格进行查询,插入,替换等操作,对...
该代码完成了用vc读写excel的操作,里面用到了excel里常用的类库.对于初学者,可以参考一下。
VC+将数据写入Excel表格 开发实例,希望对大家有帮助。
详细介绍了VC调用excel接口进行创建表格,画图等操作的方法。