这是我的一个操作过程,可以参考看看(如有更好,也请贴出啊):
void CTestExcelDlg::OnButton2()
{
// TODO: Add your control notification handler code here
_Application ExcelApp;
Workbooks books;
_Workbook book;
Worksheets sheets;
_Worksheet sheet;
Range range;
LPDISPATCH lpDisp = NULL;
//创建Excel 服务器(启动Excel)
if(!ExcelApp.CreateDispatch(_T("Excel.Application"),NULL))
{
AfxMessageBox(_T("启动Excel服务器失败!"));
return;
}
ExcelApp.SetVisible(false);
ExcelApp.SetUserControl(FALSE);
/*得到工作簿容器*/
books.AttachDispatch(ExcelApp.GetWorkbooks());
/*打开一个工作簿,如不存在,则新增一个工作簿*/
CString strBookPath =_T("f:\\tmpl.xls");
try
{
/*打开一个工作簿*/
lpDisp = books.Open(strBookPath,
vtMissing, vtMissing,vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing,vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing,vtMissing, vtMissing);
book.AttachDispatch(lpDisp);
}
catch(...)
{
/*增加一个新的工作簿*/
lpDisp = books.Add(vtMissing);
book.AttachDispatch(lpDisp);
}
/*得到工作簿中的Sheet的容器*/
sheets.AttachDispatch(book.GetSheets());
/*打开一个Sheet,如不存在,就新增一个Sheet*/
CString strSheetName =_T("Sheet1");
try
{
/*打开一个已有的Sheet*/
lpDisp =sheets.GetItem(_variant_t(strSheetName));
sheet.AttachDispatch(lpDisp);
}
catch(...)
{
/*创建一个新的Sheet*/
lpDisp = sheets.Add(vtMissing, vtMissing, _variant_t((long)1), vtMissing);
sheet.AttachDispatch(lpDisp);
sheet.SetName(strSheetName);
}
/*向Sheet中写入多个单元格,规模为4*30 */
lpDisp = sheet.GetRange(_variant_t("C6"),_variant_t("F35"));
range.AttachDispatch(lpDisp);
VARTYPE vt = VT_R4; /*数组元素的类型,float*/
SAFEARRAYBOUND sabWrite[2]; /*用于定义数组的维数和下标的起始值*/
sabWrite[0].cElements = 30;
sabWrite[0].lLbound = 0;
sabWrite[1].cElements = 4;
sabWrite[1].lLbound = 0;
COleSafeArray olesaWrite;
olesaWrite.Create(vt, sizeof(sabWrite)/sizeof(SAFEARRAYBOUND), sabWrite);
/*通过指向数组的指针来对二维数组的元素进行间接赋值*/
float (*pArray)[2]= NULL;
olesaWrite.AccessData((void **)&pArray);
memset(pArray, 0, sabWrite[0].cElements * sabWrite[1].cElements* sizeof(float));
/*释放指向数组的指针*/
olesaWrite.UnaccessData();
pArray = NULL;
/*对二维数组的元素进行逐个赋值*/
long index[2]= {0, 0};
long lFirstLBound = 0;
long lFirstUBound = 0;
long lSecondLBound = 0;
long lSecondUBound = 0;
olesaWrite.GetLBound(1, &lFirstLBound);
olesaWrite.GetUBound(1, &lFirstUBound);
olesaWrite.GetLBound(2, &lSecondLBound);
olesaWrite.GetUBound(2, &lSecondUBound);
long i = 0;
for (i = lFirstLBound;i <= lFirstUBound; i++)
{
index[0] = i;
for (long j =lSecondLBound; j <= lSecondUBound; j++)
{
index[1] = j;
float lElement = (float)(i * sabWrite[1].cElements + j);
olesaWrite.PutElement(index, &lElement);
}
}
/*把ColesaWritefeArray变量转换为VARIANT,并写入到Excel表格中*/
VARIANT varWrite = (VARIANT)olesaWrite;
range.SetValue2(varWrite);
range.SetNumberFormatLocal(COleVariant("0.00"));
/*根据文件的后缀名选择保存文件的格式*/
//CString strSaveAsName = _T("C:\\ew.xls");
//CString strSuffix = strSaveAsName.Mid(strSaveAsName.ReverseFind(_T('.')));
//XlFileFormat NewFileFormat = xlOpenXMLWorkbook;
////Excel::XlFileFormat NewFileFormat = xlWorkbookNormal;
//if (0 ==strSuffix.CompareNoCase(_T(".xls")))
//{
// NewFileFormat= xlExcel8;
//}
//book.SaveAs(_variant_t(strSaveAsName), _variant_t((long)NewFileFormat),vtMissing, vtMissing, vtMissing,
// vtMissing, 0, vtMissing, vtMissing, vtMissing,
// vtMissing, vtMissing);
book.Save();
/*释放资源*/
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
ExcelApp.Quit();
ExcelApp.ReleaseDispatch();
}