该字段太小,无法接受您在写入Excel时尝试添加的数据量

时间:2014-10-11 16:48:37

标签: c++ excel visual-c++ ado

当我在VC ++中将Cstring数据写入xls文件时,我收到此错误。 其实我在同一个文件中读取不同列的数据。在做了一些操作后,我将它放回到同一文件中的不同列中,对于已经接受的小数据。 一旦出现较大尺寸的消息,它就会抛出一个异常并中断。

功能代码粘贴在下面:

void CLoadOcxDlg::read(std::string excelFile, int sheetIndex, bool header, std::string csvFile)
{
clock_t t1 = clock();

std::cout << "reading " << excelFile;

  if(FAILED(::CoInitialize(NULL))) return;

  _RecordsetPtr pSchema = NULL;
  _RecordsetPtr pRec = NULL;

  int cellCount = 0;

  try
  {
      _bstr_t connStr(makeConnStr(excelFile, header).c_str());

      TESTHR(pRec.CreateInstance(__uuidof(Recordset)));       
      TESTHR(pRec->Open(sqlSelectSheet(connStr, sheetIndex).c_str(), connStr, adOpenStatic, adLockOptimistic, adCmdText));
      //TESTHR(pRec->Open(sqlSelectSheet(connStr, sheetIndex).c_str(), connStr, adOpenKeyset, adLockUnspecified, adCmdText));   

      std::ofstream stream(csvFile.c_str());

      while(!pRec->adoEOF)
      {
          for(long i = 8; i < pRec->Fields->GetCount(); )//++i)
          {
              CString Label = pRec->Fields->GetItem("LABEL/SMI")->Value;  

              if((Label == "AA") || (Label == "A6") || (Label == "BA") || (Label == "B6"))
              {

                  CString str  = pRec->Fields->GetItem(i + 8)->Value;
                  //_variant_t v = pRec->Fields->GetItem(i+8)->Value;
                  //if((v.vt == VT_R8) || (v.vt == VT_BSTR)) 
                  CString baseString = "/";

              {

                  if(str.GetLength())
                  {
                      int iCount = 0;

                      iCount = str.ReverseFind('/');
                  //Removing the Message part before '/'
                      str.Delete(0,iCount+1);
                      //CString baseString = "/";
                      baseString.Append(Label);
                      baseString.Append(" ");
                      baseString.Append(str);
                      baseString.Delete(baseString.GetLength() - 5,baseString.GetLength()); 
                      //pRec->Fields->GetItem(i + 9)->Value = _variant_t(baseString);
                  }



                  m_strDecodedMesg = m_ctrlDecoder.DecodeMessage(baseString);

                  long length = m_strDecodedMesg.GetLength();

                  m_strDecodedMesg.Insert(0,'"');
                  m_strDecodedMesg.Insert((m_strDecodedMesg.GetLength()+1),'"');                                
                  stream <<m_strDecodedMesg

                  **pRec-="">
  Fields->GetItem("DECODED_MESSAGE")->Value = _variant_t(m_strDecodedMesg);**

  pRec->Fields->GetItem("LENGTH")->Value = _variant_t(length);

  DataTypeEnum ctype;
  ctype = pRec->Fields->GetItem("DECODED_MESSAGE")->GetType();

  TESTHR(pRec->Update());

  }
  ++cellCount;
  }
  else
  {

  stream << _T("\" \"");
          }
          stream << std::endl;
          pRec->MoveNext();
          /*if(pRec->adoEOF)
          MessageBox("Decoding is Completed");*/
        }

      }


  }
  catch(_com_error &e)
  {
  _bstr_t bstrDescription(e.Description());
  CharToOem(bstrDescription, bstrDescription);
  std::cout << bstrDescription << std::endl;
  }
  //if(connStr->State == adStateOpen) connStr->Close();   
  ::CoUninitialize();


  clock_t t2 = clock();
  double t = (double)(t2 - t1) / CLOCKS_PER_SEC;    
  std::cout << ": " << t << " sec; " << cellCount / t << " cells/sec" << "; see " << csvFile << std::endl;   
} 

1 个答案:

答案 0 :(得分:1)

如果您准备了以下几项内容,问题就会解决:

  1. 删除您更改的行(使用正确的方式,意味着正确的sql查询,或者可能是其他类似的方法,Excel单元格位于顶部,我们可以看到,在后台是数据库,你知道...表名是表名......等,这个ADO,微软......)
  2. 之后使用字段名称,您可以重新排列行,这对您来说很有意义,保持字段类型:)
  3. 我刚刚完成了一项类似于Add On to Bio metrical access_control软件的任务,使用了这些功能(你可以使用):

        pCmd->Execute(NULL, NULL, adCmdText);
       TESTHR(pRec.CreateInstance(__uuidof(Recordset)));
        pRec->Open("SELECT * FROM MySheet", _variant_t((IDispatch*)pCon), adOpenKeyset, adLockOptimistic, adCmdText);
    
        TESTHR(pRec->Update());
        TESTHR(pRec->Close());                
    }
    catch(_com_error &e)
    {        
        _bstr_t bstrDescription(e.Description());      
        CharToOem(bstrDescription, bstrDescription);
        std::cout << bstrDescription << std::endl;
    }
    

    该物质非常具体,而且过于复杂,只能用于时间任务。 我记得Kraig Bruckshmidt(OLE和COM的畅销书籍作者)在组件对象模型中告诉David Kruglinski:&#34; David,当我开始使用COM时,前六个月为我制造了一个迷雾。&#34 ;

    无法一次解释所有问题。 如果您有疑问或需要建议,我会公开。