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

我得到这个错误,当我写Cstring数据到VC ++ xls文件。 其实我正在从同一个文件的不同列读取数据。 做了一些操作之后,我把它放回到同一个文件的不同列中,对于接受的小数据。 一旦大尺寸的消息来了,它是抛出一个怀疑和打破。

代码function粘贴如下:

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,微软…)
  2. 之后,使用字段名称,你可以重新安排行,这是有趣的,保持字段的types:)

我只需完成一个类似的任务,就是使用下面的函数(可以使用)来添加到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:“David,当我开始使用COM的时候,前六个月对我来说是个迷雾。

一次不可能全部解释。 如果您有任何问题或需要build议,我很乐意。