我想给excel文件写一些电话号码,其中一些以0开头(如02167820096)。 我尝试将该列的NumberFormatLocal属性设置为字符串类型:
QAxObject* col=worksheet->querySubObject("Columns(int)",1);
if (!col)
{
qDebug()<<"col is NULL";
}
qDebug()<<"col 1 NumberFormatLocal:"<<col->property("NumberFormatLocal").toString();
col->setProperty("NumberFormatLocal","@");
qDebug()<<"col 1 NumberFormatLocal:"<<col->property("NumberFormatLocal").toString();
输出
col 1 NumberFormatLocal: "G/通用格式"
col 1 NumberFormatLocal: "@"
我可以看到第1列中的单元格确实设置为字符串类型(“@”)。
QAxObject * range = worksheet->querySubObject("Cells(int,int)", 1, 1);
if (!range)
{
qDebug()<<"range does not exist";
}
QVariant tel=QString("%1").arg(record["tel"].toString()); //tel is 02167820096
//qDebug()<<tel;
//range->dynamicCall("SetValue(const QVariant&)", tel);
qDebug()<<"NumberFormatLocal:"<<range->property("NumberFormatLocal").toString();
qDebug()<<"NumberFormat:"<<range->property("NumberFormat").toString();
range->setProperty("Value", tel.toString());
range->clear();
,输出
NumberFormatLocal: "@"
NumberFormat: "@"
但是当我打开保存的excel文件时,其中的所有单元格都被标记为常规类型,代码根本不起作用!
帮助!感谢...
答案 0 :(得分:0)
所以解决方案是,使用“SaveAs”将excel保存为2003格式。
QList<QVariant> lstParam;
qDebug()<<QDir::toNativeSeparators(file_path);
lstParam.append(QDir::toNativeSeparators(file_path));
lstParam.append(-4143);
lstParam.append("");
lstParam.append("");
lstParam.append(false);
lstParam.append(false);
lstParam.append(1);
lstParam.append(2);
lstParam.append(false);
lstParam.append(false);
lstParam.append(false);
lstParam.append(false);
QVariant res = workbook->dynamicCall("SaveAs(QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant)", lstParam);
if(res.toBool())
{
qDebug()<<"SaveAs successful";
}