// MFC_textDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "CApplication.h"
#include "CRange.h"
#include "CWorkbook.h"
#include "CWorkbooks.h"
#include "CWorksheet.h"
#include "CWorksheets.h"
#include "CChart.h"
#include "CChartObjects.h"
#include "CChartObject.h"
#include "CCharts.h"
#include "MFC_text.h"
#include "MFC_textDlg.h"
#include "DlgProxy.h"
#include "afxdialogex.h"
#include
#include
#include
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
///////////////////自动生成部分省略//////////////////////////
void CMFC_textDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CApplication objApp;
CWorkbooks objBooks;
CWorkbook objBook;
CWorksheets objSheets;
CWorksheet objSheet;
CChart chart;
CChartObjects chartobjects;
CCharts charts;
CRange objRange, usedRange;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CString strBookPath = _T("Mamamama.xls"); //创建在我的文档中
std::string strFileName = "Mamamama.xls";
if (!objApp.CreateDispatch(_T("Excel.Application"), NULL))
{
AfxMessageBox(_T("创建Excel服务失败!"));
return;
}
objApp.put_Visible(TRUE);
objApp.put_UserControl(FALSE);
/*得到工作簿容器*/
objBooks.AttachDispatch(objApp.get_Workbooks());
/*打开一个工作簿,如不存在,则新增一个工作簿*/
/*打开一个工作簿
lpDisp = objBooks.Open(strBookPath,
vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing, vtMissing, vtMissing);
objBook.AttachDispatch(lpDisp);
/*增加一个新的工作簿*/
lpDisp = objBooks.Add(vtMissing);
objBook.AttachDispatch(lpDisp);
/*得到工作簿中的Sheet的容器*/
objSheets.AttachDispatch(objBook.get_Sheets());
//添加新的Sheet页面
lpDisp = objSheets.Add(vtMissing, vtMissing, _variant_t((long)1), vtMissing);
//得到刚刚新建的Sheet页面,并且命名为MFC0001
objSheet.AttachDispatch(lpDisp);
objSheet.put_Name(_T("MFC0001"));
objRange.AttachDispatch(objSheet.get_Cells(), TRUE);//加载所有单元格
objRange.put_Item(_variant_t((long)1), _variant_t((long)1), _variant_t("学号"));
objRange.put_Item(_variant_t((long)1), _variant_t((long)2), _variant_t("姓名"));
objRange.put_Item(_variant_t((long)1), _variant_t((long)3), _variant_t("性别"));
objRange.put_Item(_variant_t((long)1), _variant_t((long)4), _variant_t("年级"));
objRange.put_Item(_variant_t((long)3), _variant_t((long)6), _variant_t("班级"));
//添加新的Sheet页面
lpDisp = objSheets.Add(vtMissing, vtMissing, _variant_t((long)1), vtMissing);
//得到刚刚新建的Sheet页面,并且命名为MFC0001
objSheet.AttachDispatch(lpDisp);
objSheet.put_Name(_T("MFC0002"));
//把第一个Sheet页面的名字改变为TestSheet
objSheet.AttachDispatch(objSheets.get_Item(_variant_t((long)1)), TRUE);
objSheet.put_Name(_T("MFC0003"));
int a = 2;
objRange.AttachDispatch(objSheet.get_Cells(), TRUE);//加载所有单元格
objRange.put_Item(_variant_t((long)1), _variant_t((long)1), _variant_t("学号"));
objRange.put_Item(_variant_t((long)1), _variant_t((long)2), _variant_t("姓名"));
objRange.put_Item(_variant_t((long)1), _variant_t((long)3), _variant_t("性别"));
objRange.put_Item(_variant_t((long)2), _variant_t((long)1), _variant_t("140814100"));
objRange.put_Item(_variant_t((long)2), _variant_t((long)2), _variant_t(a));
objRange.put_Item(_variant_t((long)2), _variant_t((long)3), _variant_t(a));
objRange.put_Item(_variant_t((long)3), _variant_t((long)1), _variant_t("140814199"));
objRange.put_Item(_variant_t((long)3), _variant_t((long)2), _variant_t(a));
objRange.put_Item(_variant_t((long)3), _variant_t((long)3), _variant_t(a));
long left, top, width, height;
left = 100;
top = 10;
width = 350;
height = 250;
lpDisp = objSheet.ChartObjects(covOptional);
ASSERT(lpDisp);
chartobjects.AttachDispatch(lpDisp); // Attach the lpDisp pointer
// for ChartObjects to the chartobjects
// object.
CChartObject chartobject = chartobjects.Add(left, top, width, height);
//defines the rectangle,
// adds a new chart at that rectangle and
// assigns its object reference to a
// ChartObject variable named chartobject
chart.AttachDispatch(chartobject.get_Chart()); // GetChart() returns
// LPDISPATCH, and this attaches
// it to your chart object.
lpDisp = objSheet.get_Range(_variant_t("A1"), _variant_t("C3"));
// The range containing the data to be charted. 范围
ASSERT(lpDisp);
objRange.AttachDispatch(lpDisp);
VARIANT var; // ChartWizard needs a Variant for the Source range.
var.vt = VT_DISPATCH; // .vt is the usable member of the tagVARIANT
// Struct. Its value is a union of options.
var.pdispVal = lpDisp; // Assign IDispatch pointer
// of the Source range to var.
chart.ChartWizard(var, // Source.
COleVariant((short)11), // Gallery: 3d Column.
covOptional, // Format, use default.
COleVariant((short)2), // PlotBy: xlRows. 每个里面几条?乱...
COleVariant((short)1), // CategoryLabels. 数值从第几列开始
COleVariant((short)1), // SeriesLabels. 类别从第几列开始
COleVariant((short)TRUE), // HasLegend.
_variant_t("Use by Month"), // Title.
_variant_t("Month"), // CategoryTitle. 横
_variant_t("Usage in Thousands"), // ValueTitles. 纵
covOptional // ExtraTitle.
);
//保存
objBook.SaveAs(_variant_t(strBookPath), vtMissing, vtMissing, vtMissing, vtMissing,
vtMissing, 0, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing);
//释放对象(相当重要!)
objRange.ReleaseDispatch();
objSheet.ReleaseDispatch();
objSheets.ReleaseDispatch();
objBook.ReleaseDispatch();
objBooks.ReleaseDispatch();
//m_ExlApp一定要释放,否则程序结束后还会有一个Excel进程驻留在内存中,而且程序重复运行的时候会出错
objApp.ReleaseDispatch();
//退出程序
}