该字段太小,无法接受您在写入excel时尝试添加的数据量
The field is too small to accept the amount of data you attempted to add when writing to excel
当我在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;
}
如果你准备了一些东西,比如:,问题就会得到解决
- 删除你更改的行(使用正确的方式,意思是正确的sql查询,或者可能是另一种类似的方法,Excel单元格在顶部,我们可以看到,在后台是一个数据库,你知道…表名是一个表名…等等,这个ADO,Microsoft…)
- 之后,使用字段名称,您可以重新排列行,这对您来说很有趣,并保持字段类型:)
我刚刚完成了一个类似的任务,添加到生物测量访问控制软件,使用功能(你可以使用):
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时,前六个月对我来说是一片迷雾。"
不可能一下子全部解释清楚。如果你有问题或需要建议,我很乐意。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- Qt和C++:将QLineEdit添加到QTabWidget中
- 将QIcon添加到QTableView单元格
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- ATL COM将函数添加到excel中
- 该字段太小,无法接受您在写入excel时尝试添加的数据量
- 如何使用非托管C++向 Excel 文件添加新行