使用CFileDialog 可以弹出如下选择要导入的文件的对话框:
打开excel文件使用如下代码
void xxxDialog::OnUpdateExcelPath(CString &excelPath, CString &filetype){
CFileDialog fileDialog(TRUE, NULL, utilReportDir(_T("")), OFN_HIDEREADONLY, _T("全部文件(*.*)|*.*|xlsx文件(*.xlsx)|*.xlsx|xls文件(*.xls)|*.xls||"), NULL);
INT_PTR ret = fileDialog.DoModal();
if (ret == IDOK)
{
CString strFilePath = fileDialog.GetPathName();
if (strFilePath != ""){
filetype = fileDialog.GetFileExt();
excelPath = utilFileName(strFilePath);
}
else{
AfxMessageBox(_T("打开文件失败!"));
}
}
}
CString utilReportDir(CString doc_path_name)
{
return utilDataDir(CString(_T("report\\")) + doc_path_name);
}
CString utilDataDir(CString sub_path)
{
TCHAR sgCurPath[100];
ZeroMemory(sgCurPath, sizeof(sgCurPath));
GetModuleFileName(NULL, sgCurPath, 100);
CString sgModulePath = sgCurPath;
//获取上级目录
int pos = sgModulePath.ReverseFind('\\');
sgModulePath = sgModulePath.Left(pos);
pos = sgModulePath.ReverseFind('\\');
sgModulePath = sgModulePath.Left(pos);
CString sub;
sub.Format(_T("\\data\\"));
//连接图纸相对路径
sgModulePath += sub;
return sgModulePath+sub_path;
}
utilReportDir(_T(""))的作用是使得对话框打开时自动定位到data/report目录中。(为excel数据存放的目录)
打开image文件获得文件路径,使用如下代码
CFileDialog fileDialog(TRUE, NULL, utilDiagramDir(_T("")), OFN_HIDEREADONLY, _T("全部文件(*.*)|*.*|位图文件(*.BMP)|*.BMP|jpg文件(*.jpg)|*.jpg|png文件(*.png)|*.png||"), NULL);
下拉框中选择文件类型可以自动筛选