MFC_VC++_时间获取与保存列表控件内容到文件操作方法

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[1024], buff1[1024], buff2[1024], buff3[1024], buff4[1024], buff5[1024],buff6[1024];
    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", 14);   //一个汉字2个字节,斜杆符号与字母1个字节,所以共:14,字节数对应,就不会乱码
    file.Write("生成时间:" + strRptTime1 + "\n", 17);
    file.Write("\n", 1);
    file.Write("\n", 1);
    //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", 41);

    int i = 0; int j = 0; j = m_Grid.GetItemCount();
    if (j > 0)
    {
        for (i = 0; i<j; i++) {
            buff0[i] = m_Grid.GetItemText(i, 0);
            buff1[i] = m_Grid.GetItemText(i, 1);
            buff2[i] = m_Grid.GetItemText(i, 2);
            buff3[i] = m_Grid.GetItemText(i, 3);
            buff4[i] = m_Grid.GetItemText(i, 4);
            buff5[i] = m_Grid.GetItemText(i, 5);
            buff6[i] = m_Grid.GetItemText(i, 6);
            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: 在此添加控件通知处理程序代码
}

 

posted @ 2020-03-18 09:30  txwtech  阅读(470)  评论(0编辑  收藏  举报