通过Workbook保存Excel文件
这个不能实现我的需求,我想要的是:从哪个文件写数据,然后把数据写进去,然后把东西保存在原来选中的的文件中,而不是另存为。麻烦懂的帮我改下,感激不尽
bool CExcel::InitExcel(CString Sheet, CString path, bool save)
{
CoInitialize(NULL); //初始化COM
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if( !app.CreateDispatch(_T("Excel.Application"),&e))
{
e.ReportError();
AfxMessageBox(_T("无法创建Excel应用!"));
return false;
}
books.AttachDispatch(app.get_Workbooks());
book.AttachDispatch(books.Add(_variant_t(path))); //文件路径
sheets.AttachDispatch(book.get_Worksheets());
sheet.AttachDispatch(sheets.get_Item(_variant_t(Sheet))); //工作表
range.AttachDispatch(sheet.get_Cells());
range.put_Item(_variant_t((long)2), _variant_t((long)2), _variant_t(L"XXX"));
if (save)
{
book.Save();
}
app.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
::CoUninitialize();
}