2024年8月4日发(作者:)
最近刚完成一个例子,受益不菲,和大家分享一下。
VC的Excel编程操作,总结:(结合网络资源)
利用VC操作Excel的方法至少有两种
1 .利用ODBC把Excel文件当成数据库文件,来进行读、写、修改等操作,网上有
人编写了CSpreadSheet类,提供支持。(不太能理解CSpreadSheet)
2. 利用Automation(OLD Automation)方法。将Excel当成组件服务器,利用VBA。
又分为基于MFC的和SDK两种。(制作报表适用)
主要研究了一下第二种基于MFC的OLE编程方法。
一、Excel的对象模型
Application:代表应用程序本身。即Excel应用程序
Workbooks:是Workbook 的集合,代表了工作薄。
Worksheets:是Worksheet的集合,是Workbook的子对象。
Range:是Worksheet的子对象,可以理解为Sheet中一定范围的单元格。
Shapes:是Worksheet的子对象,用于存储图片等信息的单元格。
二、VC操作Excel的初始化过程
1、导入Excel库文件。
使用VC6.0的同志们可进行一下操作获取Excel库文件:
1. 启动VC 6.0,打开新建对话框,新建一个MFC AppWizard(exe)工程,这里工程明设置
为TestExcel。
2. 进入MFC 应用程序向导,选择 基本对话框,直接点击完成,工程文件结构如下图:
3. 打开MFC ClassWizard窗口(查看—>建立类向导),选择Automation,单击Add Class
按钮,选择From a ,弹出文件选择对话框,之后定位到Microsoft Office
的安装目录(通常为C:Program FilesMicrosoft OfficeOffice),选择(适
用于Excel2003;适用于Excel2000),确定后,弹出Confirm Classes
窗口,(在不知道会使用到哪些类的情况下,)选择列表中的所有类,单击OK按钮。
4.返回编辑器,查看工程文件,可发现多了EXCEL9.H及两个文件。
使用VS2005的同志们可从百度文库直接下载我已上传的Excel库文件(excel.h
和)。
将Excel库文件加入到项目后,进行如下操作:
1. 打开stdafx.h头文件确保包含如下头文件:
#include
#include "excel.h"
2. 打开文件,修改CTestExcelApp::InitInstance(),加入如下代码:
BOOL CTestExcelApp::InitInstance()
{
//
初始化COM组件
if( !AfxOleInit() ){
AfxMessageBox("初始化Ole出错!");
return FALSE;
}
AfxEnableControlContainer();
......
......
return FALSE;
}
为保证编译时不产生重复定义错误,打开excel.h文件,在文件开始位置加入如下代码:
#if !defined _HEAD_FILE_EXCEL_
#define _HEAD_FILE_EXCEL_
相应的,在文件末尾加入:
#endif
2、操作EXCEL文件
1.
选择ResourceView工作区,打开IDD_TESTEXCEL_DIALOG,在对话框中添加一个按钮
控件Button1,双击它,生成一个Button1 Click事件的处理函数:
void CTestExcelDlg::OnButton1()
{
// TODO: Add your control notification handler code here
}
2. 在OnButton1()函数中,添加代码:
void CTestExcelDlg::OnButton1()
{
// 制单
CComboBox *pCmb = (CComboBox *)GetDlgItem(IDC_COMBO1);
int pos = pCmb->GetCurSel();
if (pos == 0)
{
MessageBox(TEXT("请选择销售订单号!"), TEXT("注意"));
return ;
}
else
{//参数定义不再赘述
CEdit *pEdt = (CEdit *)GetDlgItem(IDC_EDIT1);
pCmb->GetLBText(pos,m_oOrderID);//订单号
pEdt->GetWindowTextW(m_oDeliveryDate);//交货日期
pEdt = (CEdit *)GetDlgItem(IDC_EDIT2);
pEdt->GetWindowTextW(m_oOrderTime);//接单日期
pEdt = (CEdit *)GetDlgItem(IDC_EDIT3);
pEdt->GetWindowTextW(m_oOrderCode);//编号
pEdt = (CEdit *)GetDlgItem(IDC_EDIT4);
pEdt->GetWindowTextW(m_oCompanyName);//
pEdt = (CEdit *)GetDlgItem(IDC_EDIT5);
pEdt->GetWindowTextW(m_oDept);//
pEdt = (CEdit *)GetDlgItem(IDC_EDIT6);
pEdt->GetWindowTextW(m_oLinkman);//
pEdt = (CEdit *)GetDlgItem(IDC_EDIT7);
pEdt->GetWindowTextW(m_oRecorder);//
pEdt = (CEdit *)GetDlgItem(IDC_EDIT8);
pEdt->GetWindowTextW(m_oAddress);//
pEdt = (CEdit *)GetDlgItem(IDC_EDIT9);
pEdt->GetWindowTextW(m_oPhone);//
pEdt = (CEdit *)GetDlgItem(IDC_EDIT10);
pEdt->GetWindowTextW(m_oPostCode);//
pEdt = (CEdit *)GetDlgItem(IDC_EDIT11);
pEdt->GetWindowTextW(m_oSalesman);//
////////绝对路径////////////////////////////////////////
//// 文件放在项目生成文件夹(Debug文件夹或Release
////文件夹内),发布时,别忘记添加到发布文件中。GetModuleFileName
////方法获取的是项目生成文件的根目录.
CString fileName(L"");
TCHAR exeFullPath[MAX_PATH];
CString strPath;
GetModuleFileName(NULL,exeFullPath,MAX_PATH);
strPath=(CString)exeFullPath;
int position=eFind('');
strPath=(position+1);
fileName=strPath+fileName;
////////////////////////////////////////////////////////
_Application app;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
Font font;
Range cols;
LPDISPATCH lpDisp; //接口指针
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if( !Dispatch(L"ation") ){
this->MessageBox(L"无法创建Excel应用!");
return;
}
books=kbooks();
lpDisp = (fileName,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional
);
Dispatch(lpDisp);
sheets=ets();
//////生成报表页数///////////////////////////
/// m_list1为Gridview,每页报表之存放十个记录行
int itemCount = m_mCount();
int sheetCount = 0;
if ((itemCount%10) == 0)
{
sheetCount = itemCount/10;
}
else
{
sheetCount = itemCount/10 + 1;
}
/////////////////////////////////////////////
for ( int i = 1; i <= sheetCount; i++ )
{
sheet=m(COleVariant((short)i));//获取工作表页
/////////设置报表表头///////////////////////////////////
range=ge(COleVariant(L"M1"),COleVariant(L"N1"));
umnWidth(_variant_t((long)10));//设置列宽从上往下
range=ge(COleVariant(L"B2"),COleVariant(L"C2"));
ue2(COleVariant(m_oOrderTime));
range=ge(COleVariant(L"G2"),COleVariant(L"G2"));
ue2(COleVariant(m_oOrderID));
range=ge(COleVariant(L"M2"),COleVariant(L"M2"));
ue2(COleVariant(m_oOrderCode));
range=ge(COleVariant(L"B3"),COleVariant(L"B3"));
ue2(COleVariant(m_oCompanyName));
range=ge(COleVariant(L"F3"),COleVariant(L"I3"));
ue2(COleVariant(m_oDept));
range=ge(COleVariant(L"K3"),COleVariant(L"L3"));
ue2(COleVariant(m_oLinkman));
range=ge(COleVariant(L"N3"),COleVariant(L"N3"));
ue2(COleVariant(m_oRecorder));
range=ge(COleVariant(L"B4"),COleVariant(L"B4"));
ue2(COleVariant(m_oAddress));
range=ge(COleVariant(L"F4"),COleVariant(L"I4"));
ue2(COleVariant(m_oPhone));
range=ge(COleVariant(L"K4"),COleVariant(L"L4"));
ue2(COleVariant(m_oPostCode));
range=ge(COleVariant(L"N4"),COleVariant(L"N4"));
ue2(COleVariant(m_oSalesman));
/////对记录行进行分页填充/////////////////////
CString num;
for (int j = (i-1)*10; j < 10*i; j++)
{
(L"%d",(j%10 + 7));
m_oProductName = m_mText(j,0);
if (m_oProductName == _T(""))
{
break;
}
m_oProductType = m_mText(j,1);
m_oNum = m_mText(j,2);
m_oRemark = m_mText(j,3);
range=ge(COleVariant(L"A"+num),COleVariant(L"A"+num));
ue2(COleVariant(m_oProductName));
range=ge(COleVariant(L"C"+num),COleVariant(L"C"+num));
ue2(COleVariant(m_oProductType));
range=ge(COleVariant(L"E"+num),COleVariant(L"E"+num));
ue2(COleVariant(m_oNum));
//range=ge(COleVariant(L"G"+num),COleVariant(L"G"+num));
//ue2(COleVariant());//价格
range=ge(COleVariant(L"I"+num),COleVariant(L"I"+num));
ue2(COleVariant(m_oDeliveryDate));
range=ge(COleVariant(L"L"+num),COleVariant(L"L"+num));
ue2(COleVariant(m_oRemark));
}
cols=ireColumn();
t();
}
}
ible(TRUE);
rControl(TRUE);
}
结果如下图所示:
注:Excel模板需要在Excel中手动制作,且制作完成后制作多个副本,根据情况
修改副本名称。Worksheet从左往右排列1,2,3…
// : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "stdio.h"
#include "string.h"
int main(int argc, char* argv[])
{
struct student
{
char name[10];
int num;
bool dute;
int date;
};
int len=100;
student *a;
a=new student[len];
strcpy(a[0].name,"litongyu");
a[0].num=1;
}
a[0].dute=true;
a[0].date=1;
for(int i=0;i { if(a[i].num==1&&a[i].dute==true) { a[i+1].num=1; a[i+1].dute=false; } else if(a[i].num==1&&a[i].dute==false) { a[i+1].num=2; a[i+1].dute=true; } else if(a[i].num==2&&a[i].dute==true) { a[i+1].num=3; a[i+1].dute=true; } else if(a[i].num==3&&a[i].dute==true) { a[i+1].num=3; a[i+1].dute=false; } else if(a[i].num==3&&a[i].dute==false) { a[i+1].num=1; a[i+1].dute=true; } a[i].date=i; cout< if(a[i].dute==true) cout<<",true"< else if(a[i].dute==false) cout<<",false"< } delete a; return 0;
发布评论