MFC_VC++_时间获取与保存列表控件内容到excel文件操作方法
void CDataView::OnBnClickedBtnExporttoexcel()
{
CTime time = CTime::GetCurrentTime();
CString strRptTime = time.Format("%Y%m%d%H%M%S");
CString strRptTime1 = time.Format("%Y%m%d");
// 实现存盘
CString buff0[], buff1[], buff2[], buff3[], buff4[], buff5[],buff6[];
CString fileName = "d:\\" + strRptTime + ".xls";//保存路径是d:盘根目录
CFile file(fileName, CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive);
// CFile file(fileName, CFile::modeCreate | CFile::modeReadWrite | CFile::typeText);
file.Write("表面沾污检测\n", ); //一个汉字2个字节,斜杆符号与字母1个字节,所以共:14,字节数对应,就不会乱码
file.Write("生成时间:" + strRptTime1 + "\n", );
file.Write("\n", );
file.Write("\n", );
//file.Write("\n", 1);
//file.Write("\n", 1);
//file.Write(strRptTime1+"\n", 10);
//file.Write("\t\n", 10);
file.Write("序号\t桶编号\t面\t坐标位置\t数据\t结果\t时间\r\n", );
int i = ; int j = ; j = m_Grid.GetItemCount();
if (j > )
{
for (i = ; i<j; i++) {
buff0[i] = m_Grid.GetItemText(i, );
buff1[i] = m_Grid.GetItemText(i, );
buff2[i] = m_Grid.GetItemText(i, );
buff3[i] = m_Grid.GetItemText(i, );
buff4[i] = m_Grid.GetItemText(i, );
buff5[i] = m_Grid.GetItemText(i, );
buff6[i] = m_Grid.GetItemText(i, );
CString msg;
msg.Format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n", buff0[i], buff1[i], buff2[i], buff3[i], buff4[i], buff5[i], buff6[i]);
file.Write(msg, msg.GetLength());
}
}
file.Close();
MessageBox("数据导出成功在:" + fileName);
// TODO: 在此添加控件通知处理程序代码
}