2023年11月30日发(作者:)
C++读写Excel⽂件
在处理仪表数据时需要对Excel⽂件进⾏读写操作,本⽂对C++操作Excel的⽅法做⼀个⼩结。
环境:
1. Visual Studio 2010
2. 安装office2007
3. Win7
Excel应⽤程序。
更改后的导⼊类型库的代码如下:
/*导⼊Office的类型库*/
#import "C:Program FilesCommon FilesMicrosoft "
rename("RGB", "MSORGB")
rename("DocumentProperties", "MSODocumentProperties")
using namespace Office;
/*导⼊VB的类型库*/
#import "C:Program FilesCommon FilesMicrosoft "
using namespace VBIDE;
主要代码如下:
///启动Excel服务器
if(!AfxOleInit() ){
AfxMessageBox(_T("初始化Ole出错!"));
return FALSE;
}
//声明被操作的对象
CApplication m_appA;
CWorkbooks m_booksA;
CWorkbook m_bookA;
CWorksheets m_sheetsA;
//对应LTE的sheet
CWorksheet m_sheetA;
//释放对象
m_();
m_eDispatch();
m_eDispatch();
m_eDispatch();
m_eDispatch();
m_();
///Function: IndexToString
///Description: 得到的单元格在EXCEL中的定位名称字符串
///Input: int row 单元格所在⾏
/// int col 单元格所在列
///Return: CString 单元格在EXCEL中的定位名称字符串
CString CMeterDataExportedToolDlg::IndexToString( int row, int col )
{
CString strResult;
if( col > 26 )
{
(_T("%c%c%d"),'A' + (col-1)/26-1,'A' + (col-1)%26,row);
}
else
{
(_T("%c%d"), 'A' + (col-1)%26,row);
}
return strResult;
}
///Function: GetCellValue


发布评论