vc 将LISTCTRL里的内容保存成EXCEL

  1.获取当前时间做EXCEL的文件名:
                  WORD  sYear,sMonth,sDay,sHour,sMinute,sSecond;
                  SYSTEMTIME  CurTime;/////定义SYSTEMTIME类的对象
                  GetLocalTime(&CurTime);////获取本地时间
                  sYear=CurTime.wYear;////获得年
                  sM.wMonth;//获得月
                  sDay=CurTime.wDay;
                  sHour=CurTime.wHour;
                  sMinute=CurTime.wMinute;
                  sSec.wSecond;
                  CString sFileName;
                  sFileName.Format("%d年%d月%d日%d点%d分%d秒.xls",
                                             sYear,sMonth,sDay,sHour,sMinute,sSecond);
2.添加保存对话框:
                  CFileDialog dlg(FALSE, ".xls", sFileName,
                                         OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,
                                         "(*.xls)|*.xls||", this);
                                         dlg.m_ofn.lpstrTitle = _T("保存文件");
3.单击确定执行:
                 if (dlg.DoModal() == IDOK)
                 {
                    // 实现存盘
                     CString buff0[1024],buff1[1024],buff2[1024],buff3[1024],buff4[1024];
                     CString fileName = dlg.GetPathName();
                     CFile file(fileName,
                                     CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive);
  
                     file.Write("操作/t表号/t读取类型/t读取值/t发送结果/n",35);
                    
                     int i = 0;
                     int j=0;
                     j = m_list.GetItemCount();
                     if( j > 0 )
                     {
                         for(i=0;i<j;i++)
                        {
                             buff0[i] = m_list.GetItemText( i, 0 );
                             buff1[i] = m_list.GetItemText( i, 1 );
                             buff2[i] = m_list.GetItemText( i, 2 );
                             buff3[i] = m_list.GetItemText( i, 3 );
                             buff4[i] = m_list.GetItemText( i, 4 );
                             CString msg;
                             msg.Format("%s/t%s/t%s/t%s/t%s/n",buff0[i],buff1[i],buff2[i],buff3[i],buff4[i]);
                             file.Write(msg,msg.GetLength());
                        }
                  }
  
             file.Close();
          }
     return;
  }

你可能感兴趣的:(Excel,File)