测试代码:
vc6.0
office2000
以下代码需要MFC的支持,而且工程中还要包含EXCEL2000的定义文件:EXCEL9.H,EXCEL9.CPP
_Application ExcelApp;
Workbooks wbsMyBooks;
_Workbook wbMyBook;
Worksheets wssMysheets;
_Worksheet wsMysheet;
Range range;
LPDISPATCH lpDisp;
//创建Excel 2000服务器(启动Excel)
COleVariant vResult;
COleVariant covTrue((short)TRUE);
COleVariant covFalse((short)FALSE);
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CoInitialize(NULL);
if (!ExcelApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("创建Excel服务失败!");
exit(1);
}
ExcelApp.SetVisible(TRUE); //使Excel可见
ExcelApp.SetUserControl(TRUE); //允许其它用户控制Excel
//打开c:\\aaa.xls
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks());
lpDisp = wbsMyBooks.Open("C:\\aaa.xls",
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional );
//得到Workbook
wbMyBook.AttachDispatch(lpDisp);
//得到Worksheets
wssMysheets.AttachDispatch(wbMyBook.GetWorksheets());
//得到当前活跃sheet
//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
lpDisp=wbMyBook.GetActiveSheet();
wsMysheet.AttachDispatch(lpDisp);
//*****
//读取已经使用区域的信息,包括已经使用的行数、列数、起始行、起始列
Range usedRange;
usedRange.AttachDispatch(wsMysheet.GetUsedRange());
range.AttachDispatch(usedRange.GetRows());
long iRowNum=range.GetCount(); //已经使用的行数
range.AttachDispatch(usedRange.GetColumns());
long iColNum=range.GetCount(); //已经使用的列数
long iStartRow=usedRange.GetRow(); //已使用区域的起始行,从1开始
long iStartCol=usedRange.GetColumn(); //已使用区域的起始列,从1开始
for (long i=iStartRow; i<iRowNum+1; i++)
{
for (long j=iStartCol; j<iColNum+1; j++)
{
range.AttachDispatch(wsMysheet.GetCells());
range.AttachDispatch(range.GetItem (COleVariant((long)i),COleVariant((long)j)).pdispVal );
vResult =range.GetValue();
CString str;
if(vResult.vt == VT_BSTR) //字符串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_R8) //8字节的数字
{
str.Format("%f",vResult.dblVal);
}
else if(vResult.vt==VT_DATE) //时间格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //单元格空的
{
str="";
}
if (str.Find("44") != -1)
{
AfxMessageBox(str);
}
}
}
/*****
//读取第一个单元格的值
range.AttachDispatch(wsMysheet.GetCells());
range.AttachDispatch(range.GetItem (COleVariant((long)1),COleVariant((long)1)).pdispVal );
vResult =range.GetValue();
CString str;
if(vResult.vt == VT_BSTR) //字符串
{
str=vResult.bstrVal;
}
else if (vResult.vt==VT_R8) //8字节的数字
{
str.Format("%f",vResult.dblVal);
}
else if(vResult.vt==VT_DATE) //时间格式
{
SYSTEMTIME st;
VariantTimeToSystemTime(vResult.date, &st);
}
else if(vResult.vt==VT_EMPTY) //单元格空的
{
str="";
}
AfxMessageBox(str);
*/
/*
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),true);
wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t("c:\\aaa.xls")));
//MessageBox("gg1");
//得到Worksheets
wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(),true);
//得到sheet1
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("bend")),true);
//得到全部Cells,此时,rgMyRge是cells的集合
range.AttachDispatch(wsMysheet.GetCells(),true);
_variant_t vStr;
BSTR bStr;
vStr=range.GetItem(_variant_t((long)3),_variant_t((long)3));
bStr = (_bstr_t)vStr;
//CString outstr=(LPCTSTR)(LPSTR)bStr;
CString out2="";
for(int i=0;i<sizeof(bStr);i++)
{
out2+=(CString)(char)*(bStr+i);
}
AfxMessageBox(out2);
*/
//关闭execl
ExcelApp.SetUserControl(FALSE);
wbMyBook.Close (covOptional,covOptional,covOptional);
wbsMyBooks.Close();
ExcelApp.Quit();
//释放对象
range.ReleaseDispatch();
wsMysheet.ReleaseDispatch();
wssMysheets.ReleaseDispatch();
wbMyBook.ReleaseDispatch();
wbsMyBooks.ReleaseDispatch();
ExcelApp.ReleaseDispatch();
}
Quit之前
SetUserControl(FALSE);