我用的是excel2000
首先利用Visual C++ 6.0,建立一个MFC基于对话框的工程项目,共享DLL,Win32平台。工程名称ExcelTest。在主对话框中加入一个按钮,
ID IDC_EXCELTEST
Caption Test Excel
双击该按钮,增加成员函数void CExcelTestDlg::OnExceltest()。
在BOOL CExcelTestApp::InitInstance()中,dlg.DoModal();之前增加代码:
if (CoInitialize(NULL)!=0)
{
AfxMessageBox("初始化COM支持库失败!");
exit(1);
}
在return FALSE; 语句前,加入:
CoUninitialize();
选择Menu->View->ClassWizade,打开ClassWizade窗口,选择Add Class->From a type library,选择D:\Program Files\Microsoft Office\office\Excel9.OLB(D:\Program Files\Microsoft Office\是本机上Microsoft Office 2000的安装目录,可根据个人机器上的实际安装目录修改)。选择_Application、Workbooks、_Workbook、Worksheets、_Worksheet、Range,加入新类,分别为_Application、Workbooks、_Workbook、Worksheets、_Worksheet、Range,头文件Excel9.h,源文件Excel9.cpp。
在ExcelTestDlg.cpp文件的头部,#include "ExcelTestDlg.h"语句之下,增加 :
#include "comdef.h"
#include "Excel9.h"
在void CExcelTestDlg::OnExceltest() 函数中增加如下代码:
void CExcelTestDlg::OnExceltest()
{
_Application ExcelApp;
Workbooks wbsMyBooks;
_Workbook wbMyBook;
Worksheets wssMysheets;
_Worksheet wsMysheet;
Range rgMyRge;
//创建Excel 2000服务器(启动Excel)
if (!ExcelApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("创建Excel服务失败!");
exit(1);
}
//利用模板文件建立新文档
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),true);
wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t("g:\\exceltest\\MyTemplate.xlt")));
//得到Worksheets
wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(),true);
//得到sheet1
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true);
//得到全部Cells,此时,rgMyRge是cells的集合
rgMyRge.AttachDispatch(wsMysheet.GetCells(),true);
//设置1行1列的单元的值
rgMyRge.SetItem(_variant_t((long)1),_variant_t((long)1),_variant_t("This Is A Excel Test Program!"));
//得到所有的列
rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true);
//得到第一列
rgMyRge.AttachDispatch(rgMyRge.GetItem(_variant_t((long)1),vtMissing).pdispVal,true);
//设置列宽
rgMyRge.SetColumnWidth(_variant_t((long)200));
//调用模板中预先存放的宏
ExcelApp.Run(_variant_t("CopyRow"),_variant_t((long)10),vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing);
//打印预览
wbMyBook.SetSaved(true);
ExcelApp.SetVisible(true);
wbMyBook.PrintPreview(_variant_t(false));
//释放对象
rgMyRge.ReleaseDispatch();
wsMysheet.ReleaseDispatch();
wssMysheets.ReleaseDispatch();
wbMyBook.ReleaseDispatch();
wbsMyBooks.ReleaseDispatch();
ExcelApp.ReleaseDispatch();
}
以上内容引自网络。
一下是我自己写的类,将对excel的操作封装了一下:
.h文件:
#include "comdef.h"
#include "excel.h"
class ExcelFile
{
public:
void ShowInExcel(bool bShow);
CString GetCell(int iRow, int iColumn);
int GetCellInt(int iRow, int iColumn);
int GetRowCount();
int GetColumnCount();
bool LoadSheet(int iIndex);
CString GetSheetName(int iIndex);
static void InitExcel();
static void ReleaseExcel();
int GetSheetCount();
bool Open(CString FileName);
ExcelFile();
virtual ~ExcelFile();
protected:
private:
static _Application m_ExcelApp;
Workbooks m_Books;
_Workbook m_Book;
Worksheets m_sheets;
_Worksheet m_sheet;
Range m_Rge;
};
.cpp文件:
ExcelFile::ExcelFile()
{
}
ExcelFile::~ExcelFile()
{
m_Rge.ReleaseDispatch();
m_sheet.ReleaseDispatch();
m_sheets.ReleaseDispatch();
m_Book.ReleaseDispatch();
m_Books.ReleaseDispatch();
}
void ExcelFile::InitExcel()
{
//创建Excel 2000服务器(启动Excel)
if (!m_ExcelApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("创建Excel服务失败!");
exit(1);
}
}
void ExcelFile::ReleaseExcel()
{
m_ExcelApp.ReleaseDispatch();
}
bool ExcelFile::Open(CString FileName)
{//打开excel文件
//利用模板文件建立新文档
m_Books.AttachDispatch(m_ExcelApp.GetWorkbooks(),true);
LPDISPATCH lpDis = NULL;
lpDis = m_Books.Add(_variant_t(FileName)); // 如何判断文件是否打开?
if (lpDis)
{
m_Book.AttachDispatch(lpDis);
//得到Worksheets
m_sheets.AttachDispatch(m_Book.GetWorksheets(),true);
return true;
}
return false;
}
int ExcelFile::GetSheetCount()
{
return m_sheets.GetCount();
}
CString ExcelFile::GetSheetName(int iIndex)
{
_Worksheet sheet;
sheet.AttachDispatch(m_sheets.GetItem(_variant_t((long)iIndex)),true);
CString name = sheet.GetName();
sheet.ReleaseDispatch();
return name;
}
bool ExcelFile::LoadSheet(int iIndex)
{
LPDISPATCH lpDis = NULL;
m_Rge.ReleaseDispatch();
m_sheet.ReleaseDispatch();
lpDis = m_sheets.GetItem(_variant_t((long)iIndex));
if (lpDis)
{
m_sheet.AttachDispatch(lpDis,true);
m_Rge.AttachDispatch(m_sheet.GetCells(), true);
return true;
}
return false;
}
int ExcelFile::GetColumnCount()
{
Range range;
Range usedRange;
usedRange.AttachDispatch(m_sheet.GetUsedRange(), true);
range.AttachDispatch(usedRange.GetColumns(), true);
int count = range.GetCount();
usedRange.ReleaseDispatch();
range.ReleaseDispatch();
return count;
}
int ExcelFile::GetRowCount()
{
Range range;
Range usedRange;
usedRange.AttachDispatch(m_sheet.GetUsedRange(), true);
range.AttachDispatch(usedRange.GetRows(), true);
int count = range.GetCount();
usedRange.ReleaseDispatch();
range.ReleaseDispatch();
return count;
}
CString ExcelFile::GetCell(int iRow, int iColumn)
{
Range range;
range.AttachDispatch(m_Rge.GetItem (COleVariant((long)iRow),COleVariant((long)iColumn)).pdispVal, true);
COleVariant vResult =range.GetValue2();
CString str;
if(vResult.vt == VT_BSTR) //字符串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_INT)
{
str.Format("%d",vResult.pintVal);
}
else if (vResult.vt==VT_R8) //8字节的数字
{
str.Format("%f",vResult.dblVal);
//str.Format("%.0f",vResult.dblVal);
//str.Format("%1f",vResult.fltVal);
}
else if(vResult.vt==VT_DATE) //时间格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //单元格空的
{
str="(NULL)";
}
range.ReleaseDispatch();
return str;
}
int ExcelFile::GetCellInt(int iRow, int iColumn)
{
Range range;
range.AttachDispatch(m_Rge.GetItem (COleVariant((long)iRow),COleVariant((long)iColumn)).pdispVal, true);
COleVariant vResult =range.GetValue2();
int num;
num = (int)vResult.date;
range.ReleaseDispatch();
return num;
}
void ExcelFile::ShowInExcel(bool bShow)
{
m_ExcelApp.SetVisible(bShow);
}