MFC将数据写入excel

// 按键触发

// 导出报表
void CReportDlg::OnBnClickedButtonCheckOutExcel()
{
	
	CFileDialog dlg(false, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Excel Files(*.xls)|*.xls|All Files(*.*)|*.*|"), AfxGetMainWnd());
	CString strPath;
	if (dlg.DoModal() == IDOK)
	{
		strPath = dlg.GetPathName(); 
		if (strPath.Find(_T(".xls")) < 0) {
			strPath += _T(".xls");
		}
		ReportExcel(strPath);
	}
}


void CReportDlg::ReportExcel(LPCTSTR strPath)
{
	std::map::iterator it;

	TRY
	{
		CString cs;
		CStdioFile file(strPath, CFile::shareExclusive | CFile::modeWrite | CFile::modeCreate);

		setlocale(LC_CTYPE, ("chs")); //设置中文输出

		file.WriteString(_T("序号\t读卡器ID\t读卡器IP\t端口号\t连接状态\t连接上时刻\t断开连接时刻\n"));
		
		int row = 0;
		ReportClientInfo temp;
		for (it = m_reportMap.begin(); it != m_reportMap.end(); ++it)
		{
			temp = it->second;
			cs.Format(_T("%d\t`%s\t%s\t%s\t%s\t%s\t%s\n"),
				++row,
				temp.ReaderID,
				temp.ReaderIP,
				temp.ReaderPort,
				temp.ConnectStutes,
				temp.ConnectTime,
				temp.DisConnectTime);
			file.WriteString(cs);
		}
		file.Close();
		AfxMessageBox(_T("导出成功"));
	}
	CATCH_ALL(e)
	{
		e->ReportError();
		return;
	}
	END_CATCH_ALL

}



你可能感兴趣的:(C++)