zoukankan      html  css  js  c++  java
  • 导EXCEL单表单方法

    //记着在函数外面释放
    char *Cov(CString str,int &nBytes)
    {
    int nLength = str.GetLength();
    nBytes = WideCharToMultiByte(CP_ACP,0,str,nLength,NULL,0,NULL,NULL);
    char* pName = new char[nBytes + 1];
    memset(pName,0,nLength + 1);
    WideCharToMultiByte(CP_OEMCP, 0, str, nLength, pName, nBytes, NULL, NULL);
    pName[nBytes] = 0;
    return pName;
    }
    //导出EXCEL( 换行, 换单元格)
    void CCShowMaterialInfo::OnBnClickedBtnOk()
    {
    // TODO: 在此添加控件通知处理程序代码
    //导出Excel
    BOOL bNameValid;
    CString strType;
    CString strAllType;
    CString strWrite,strTmp;
    char *bufWrite=NULL;
    int nBytes=0;
    int nR=0;
    int nC=0;
    bNameValid = strType.LoadString(IDS_STRTYPEXLS);
    VASSERT(bNameValid);
    bNameValid = strAllType.LoadString(IDS_STRALLTYPEXLS);
    VASSERT(bNameValid);

    CFileDialog dlg(false,
    strType,
    NULL,
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    strAllType);

    if(dlg.DoModal() == IDOK)
    {
    CString m_FilePath = dlg.GetPathName();
    CString m_path;
    m_path = m_FilePath;
    UpdateData(TRUE);
    try
    {
    CFile f;
    f.Open(m_path,CFile::modeWrite|CFile::modeCreate);
    strWrite=_T("当前总码:");
    if (b_flg)
    {
    ((CComboBox *)GetDlgItem(IDC_COMBO_TotalCode))->GetWindowText(strTmp);
    }
    else
    {
    ((CEdit *)GetDlgItem(IDC_EDIT_RTotalCode))->GetWindowText(strTmp);
    }
    strWrite+=strTmp;
    strWrite+=_T(" ");

    strTmp=_T("后制程总码:");
    strWrite+=strTmp;
    ((CEdit *)GetDlgItem(IDC_EDIT_ATotalCode))->GetWindowText(strTmp);
    strWrite+=strTmp;
    strWrite+=_T(" ");

    ((CStatic *)GetDlgItem(IDC_STATIC_RproductName))->GetWindowText(strTmp);
    strWrite+=strTmp;
    strWrite+=_T(" ");

    ((CStatic *)GetDlgItem(IDC_STATIC_RproductCode))->GetWindowText(strTmp);
    strWrite+=strTmp;
    strWrite+=_T(" ");//中途空两行
    strTmp=_T("物料详细信息: ");
    strWrite+=strTmp;

    nBytes=0;
    bufWrite=Cov(strWrite,nBytes);
    f.Write(bufWrite,nBytes);
    if (bufWrite!=NULL)
    {
    delete[]bufWrite;
    bufWrite=NULL;
    }
    //先写物料信息
    int nColCount=m_listMaterialInfo.GetHeaderCtrl()->GetItemCount();
    LVCOLUMN lvcolu;
    TCHAR str[256];
    lvcolu.mask = LVCF_TEXT;
    lvcolu.pszText = str;
    lvcolu.cchTextMax = 256;
    strWrite.Empty();
    for (nC=0;nC<nColCount;nC++)
    {
    strTmp.Empty();
    m_listMaterialInfo.GetColumn(nC,&lvcolu);
    strTmp=lvcolu.pszText;
    strTmp+=_T(" ");
    strWrite+=strTmp;
    }
    strWrite+=_T(" ");
    nBytes=0;
    bufWrite=Cov(strWrite,nBytes);
    f.Write(bufWrite,nBytes);
    if (bufWrite!=NULL)
    {
    delete[]bufWrite;
    bufWrite=NULL;
    }
    //开始写物料数据
    int nRowNum=m_listMaterialInfo.GetItemCount();
    for (nR=0;nR<nRowNum;nR++)
    {
    strWrite.Empty();
    for (nC=0;nC<nColCount;nC++)
    {
    strTmp.Empty();
    strTmp=m_listMaterialInfo.GetItemText(nR,nC);
    strTmp+=_T(" ");
    strWrite+=strTmp;
    }
    strWrite+=_T(" ");
    nBytes=0;
    bufWrite=Cov(strWrite,nBytes);
    f.Write(bufWrite,nBytes);
    if (bufWrite!=NULL)
    {
    delete[]bufWrite;
    bufWrite=NULL;
    }
    }
    //开始电检信息写入
    strWrite.Empty();
    strWrite+=_T(" ");
    strTmp=_T("电池性能检测: ");
    strWrite+=strTmp;
    nColCount=m_listElec.GetHeaderCtrl()->GetItemCount();
    for (nC=0;nC<nColCount;nC++)
    {
    strTmp.Empty();
    m_listElec.GetColumn(nC,&lvcolu);
    strTmp=lvcolu.pszText;
    strTmp+=_T(" ");
    strWrite+=strTmp;
    }
    strWrite+=_T(" ");
    nBytes=0;
    bufWrite=Cov(strWrite,nBytes);
    f.Write(bufWrite,nBytes);
    if (bufWrite!=NULL)
    {
    delete[]bufWrite;
    bufWrite=NULL;
    }
    //开始写电检结果
    nRowNum=m_listElec.GetItemCount();
    for (nR=0;nR<nRowNum;nR++)
    {
    strWrite.Empty();
    for (nC=0;nC<nColCount;nC++)
    {
    strTmp.Empty();
    strTmp=m_listElec.GetItemText(nR,nC);
    strTmp+=_T(" ");
    strWrite+=strTmp;
    }
    strWrite+=_T(" ");
    nBytes=0;
    bufWrite=Cov(strWrite,nBytes);
    f.Write(bufWrite,nBytes);
    if (bufWrite!=NULL)
    {
    delete[]bufWrite;
    bufWrite=NULL;
    }
    }

    //开始气密性信息写入
    strWrite.Empty();
    strWrite+=_T(" ");
    strTmp=_T("气密性能检测: ");
    strWrite+=strTmp;
    nColCount=m_listGas.GetHeaderCtrl()->GetItemCount();
    for (nC=0;nC<nColCount;nC++)
    {
    strTmp.Empty();
    m_listGas.GetColumn(nC,&lvcolu);
    strTmp=lvcolu.pszText;
    strTmp+=_T(" ");
    strWrite+=strTmp;
    }
    strWrite+=_T(" ");
    nBytes=0;
    bufWrite=Cov(strWrite,nBytes);
    f.Write(bufWrite,nBytes);
    if (bufWrite!=NULL)
    {
    delete[]bufWrite;
    bufWrite=NULL;
    }
    //开始写气密性结果
    nRowNum=m_listGas.GetItemCount();
    for (nR=0;nR<nRowNum;nR++)
    {
    strWrite.Empty();
    for (nC=0;nC<nColCount;nC++)
    {
    strTmp.Empty();
    strTmp=m_listGas.GetItemText(nR,nC);
    strTmp+=_T(" ");
    strWrite+=strTmp;
    }
    strWrite+=_T(" ");
    nBytes=0;
    bufWrite=Cov(strWrite,nBytes);
    f.Write(bufWrite,nBytes);
    if (bufWrite!=NULL)
    {
    delete[]bufWrite;
    bufWrite=NULL;
    }
    }
    f.Flush();
    f.Close();
    MessageBox(_T("导出成功!"),_T("提示"));
    }
    catch(...)
    {

    }
    }
    }

  • 相关阅读:
    加密算法使用(五):RSA使用全过程
    加密算法使用(四):AES的使用
    加密算法使用(三):用用BASE64
    加密算法使用(二):使用MD5加密字符串(另:byte数组转16进制自动补零方法写法)
    加密算法使用(一):用CRC32来压缩32uuid字符串
    【转载】阿里云ECS Linux服务器禁止某些IP访问
    【转载】Sqlserver数据库备份的几种方式
    【转载】网站域名备案相关流程介绍
    【转载】C#工具类:实现文件操作File的工具类
    【转载】 禁止国外IP访问你的网站
  • 原文地址:https://www.cnblogs.com/lishennan/p/4775560.html
Copyright © 2011-2022 走看看