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

The field is too small to accept the amount of data you attempted to add when writing to excel

本文关键字:excel 添加 数据 字段      更新时间:2023-10-16

当我在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. 删除你更改的行(使用正确的方式,意思是正确的sql查询,或者可能是另一种类似的方法,Excel单元格在顶部,我们可以看到,在后台是一个数据库,你知道…表名是一个表名…等等,这个ADO,Microsoft…)
  2. 之后,使用字段名称,您可以重新排列行,这对您来说很有趣,并保持字段类型:)

我刚刚完成了一个类似的任务,添加到生物测量访问控制软件,使用功能(你可以使用):

    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 Brucksmidt(OLE和COM畅销书的作者)在组件对象模型上对David Kruglinski说:"David,当我开始使用COM时,前六个月对我来说是一片迷雾。"

不可能一下子全部解释清楚。如果你有问题或需要建议,我很乐意。