//获得默认的文件名 BOOL CFunc::GetDefaultXlsFileName(CString& sExcelFile) { ///默认文件名:yyyymmddhhmmss.xls CString timeStr; CTime day; day=CTime::GetCurrentTime(); int filenameday,filenamemonth,filenameyear,filehour,filemin,filesec; filenameday=day.GetDay();//dd filenamemonth=day.GetMonth();//mm月份 filenameyear=day.GetYear();//yyyy filehour=day.GetHour();//hh filemin=day.GetMinute();//mm分钟 filesec=day.GetSecond();//ss timeStr.Format("%04d%02d%02d%02d%02d%02d",filenameyear,filenamemonth,filenameday,filehour,filemin,filesec); sExcelFile = timeStr + ".xls"; // prompt the user (with all document templates) CFileDialog dlgFile(FALSE,".xls",sExcelFile); CString title; CString strFilter; title = "导出"; strFilter = "Excel文件(*.xls)"; strFilter += (TCHAR)'\0'; // next string please strFilter += _T("*.xls"); strFilter += (TCHAR)'\0'; // last string dlgFile.m_ofn.nMaxCustFilter++; dlgFile.m_ofn.nFilterIndex = 1; // append the "*.*" all files filter CString allFilter; VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER)); strFilter += allFilter; strFilter += (TCHAR)'\0'; // next string please strFilter += _T("*.*"); strFilter += (TCHAR)'\0'; // last string dlgFile.m_ofn.nMaxCustFilter++; dlgFile.m_ofn.lpstrFilter = strFilter; dlgFile.m_ofn.lpstrTitle = title; if (dlgFile.DoModal()==IDCANCEL) return FALSE; // open cancelled sExcelFile.ReleaseBuffer(); if (MakeSurePathExists(sExcelFile,true)) { if(!DeleteFile(sExcelFile)) { // delete the file AfxMessageBox("覆盖文件时出错!"); return FALSE; } } return TRUE; }
bool C_file::Open(const wchar *fname, dword open_flags){ Close(); #ifdef _DEBUG //make sure the filename doesn't contain 2 slashes for(int i=1; fname[i]; i++){ if(fname[i]=='\\' && fname[i-1]=='\\') return false; } #endif bool is_registry = (*fname == ':'); switch(open_flags&0xff){ case FILE_READ: { C_file_read_base *cr; if(!is_registry) cr = new C_file_read; else cr = new C_file_read_registry; imp = cr; if(!cr->Open(fname)){ Close(); return false; } } break; case FILE_WRITE: { if(open_flags&FILE_WRITE_CREATE_PATH) MakeSurePathExists(fname); C_file_write_base *cw; if(!is_registry) cw = new C_file_write; else cw = new C_file_write_registry; imp = cw; if(!cw->Open(fname, open_flags)){ Close(); return false; } } break; default: LOG_RUN("C_file: inv mode"); return false; } return true; }
///////////////////////////////////////////////////// //名称:GetDefaultXlsFileName(CString& sExcelFile) //功能:获得默认文件名 ///////////////////////////////////////////////////// BOOL GetDefaultXlsFileName(CString& sExcelFile) { //默认文件名:yyyymmddmmss.xls CString timeStr; CTime day; day=CTime::GetCurrentTime(); int filenameday,filenamemonth,filenameyear,filehour,filemin,filesec; filenameday = day.GetDay(); //dd天 filenamemonth = day.GetMonth(); //mm月份 filenameyear = day.GetYear(); //yyyy年份 filehour = day.GetHour(); //hh小时 filemin = day.GetMinute(); //mm分钟 filesec = day.GetSecond(); //ss秒 timeStr.Format("%04d%02d%02d%02d%02d%02d",filenameyear,filenamemonth,filenameday,filehour,filemin,filesec); sExcelFile = timeStr + ".xls"; // prompt the user (with all document templates) CFileDialog dlgFile(FALSE,".xls",sExcelFile,OFN_HIDEREADONLY,_T("Excel 工作簿(*.xls)|*.xls|所有文件(*.*)|*.*||")); dlgFile.m_ofn.lpstrTitle = "导出"; if(dlgFile.DoModal()==IDCANCEL) { return FALSE; } sExcelFile.ReleaseBuffer(); if(MakeSurePathExists(sExcelFile)) { if(!DeleteFile(sExcelFile)) { AfxMessageBox("覆盖文件时出错!"); return FALSE; } } return TRUE; }