qtexcel
所属分类:大数据
开发工具:C++
文件大小:692KB
下载次数:1
上传日期:2020-12-05 03:23:06
上 传 者:
sh-1993
说明: qt利用QAxObjecti操作大数据excel
(Qt uses QAxObjecti to operate big data excel)
文件列表:
untitled (0, 2020-12-05)
untitled\configMC.txt (85, 2020-12-05)
untitled\equipment.cpp (1952, 2020-12-05)
untitled\equipment.h (1053, 2020-12-05)
untitled\exceloperator.cpp (14065, 2020-12-05)
untitled\exceloperator.h (2852, 2020-12-05)
untitled\flatui.cpp (7909, 2020-12-05)
untitled\flatui.h (4424, 2020-12-05)
untitled\image.qrc (225, 2020-12-05)
untitled\image (0, 2020-12-05)
untitled\image\back1.jpg (104052, 2020-12-05)
untitled\image\back111.jpg (329521, 2020-12-05)
untitled\image\back2.jpg (159320, 2020-12-05)
untitled\image\back3.jpg (23012, 2020-12-05)
untitled\image\main_person.png (24518, 2020-12-05)
untitled\image\yayaya.jpg (65044, 2020-12-05)
untitled\loginwin.cpp (8553, 2020-12-05)
untitled\loginwin.h (741, 2020-12-05)
untitled\loginwin.ui (15115, 2020-12-05)
untitled\main.cpp (219, 2020-12-05)
untitled\main.ico (67646, 2020-12-05)
untitled\mainwindow.cpp (14735, 2020-12-05)
untitled\settingwin.cpp (6586, 2020-12-05)
untitled\untitled.pro (1208, 2020-12-05)
untitled\untitled.pro.user (22792, 2020-12-05)
# qtexcel
qt利用QAxObjecti操作大数据excel
查阅了大部分资料,大数据读写excel文件用QAxObject对象最快,借鉴了一些读写excel文件的demo,所以本文也采用了这种方法。
读取单个单元格内容时,property("Value").toString(),一秒大概能读写200行左右。但是需要大量读写单元格时,以上方法将会变得十分缓慢,所以这时需要用到QVariant,一次读取或写入整个工作表。采用以下方法,能将效率大大提高,一万行大概500毫秒。这个版本还没采用多线程读写,后面加入效率还可以提高。
读写单元格时,常用以下这种方法:
// 根据行号、列号读取某个单元格内容
QAxObject* pCell = m_pWorksheet->querySubObject("Cells(int, int)", iirow, icolumn);
QString strCell = pCell->property("Value").toString();
// 根据行列值读取某个单元格内容
QAxObject* pCell = pWorksheet->querySubObject("Range(QString)", strnumber);
QString strCell = pCell->property("Value").toString();
// 根据行号、列号写入某个单元格内容
QAxObject* pCell = m_pWorksheet->querySubObject("Cells(int, int)", irow, icolumn);
QString pCell->setProperty("Value", strvalue);
// 根据行列值读取某个单元格内容
QAxObject* pCell = m_pWorksheet->querySubObject("Range(QString)", strnumber);
QString pCell->setProperty("Value", strvalue);
以下是一次读取工作表全部内容的方法:
void ExcelOperator::readAll(QList > &res)
{
QVariant var;
if (m_pWorksheet != NULL && ! m_pWorksheet->isNull())
{
// 获取该sheet的数据范围
QAxObject * ax_usedRange = m_pWorksheet->querySubObject("UsedRange");
if(NULL == ax_usedRange || ax_usedRange->isNull())
{
return;
}
// 获取该sheet的数据内容
var = ax_usedRange->dynamicCall("Value");
delete ax_usedRange;
}
this->castVariant2ListListVariant(var, res);
return;
}
void ExcelOperator::castVariant2ListListVariant(const QVariant &var, QList > &res)
{
QVariantList varRows = var.toList();
if(varRows.isEmpty())
{
return;
}
const int rowCount = varRows.size();
QVariantList rowData;
for(int i=0;i > &cells)
{
if(cells.size() <= 0)
return false;
if(NULL == this->m_pWorksheet || this->m_pWorksheet->isNull())
return false;
// 获取写入内容行数
int irow = cells.size();
// 获取写入内容列数
int icol = cells.at(0).size();
QString strRang;
// 获取写入矩形内容最右边列标识
convertToColName(icol, strRang);
// 获取写入矩形内容最右边右下角单元格标识
strRang += QString::number(irow);
// 获取写入矩形内容写入范围
strRang = "A2:" + strRang;
qDebug()<< strRang;
QAxObject *range = this->m_pWorksheet->querySubObject("Range(const QString&)", strRang);
if(NULL == range || range->isNull())
{
return false;
}
bool succ = false;
QVariant var;
castListListVariant2Variant(cells,var);
// 将数据写入到sheet
succ = range->setProperty("Value", var);
delete range;
return succ;
}
void ExcelOperator::castListListVariant2Variant(const QList > &listcells, QVariant &varres)
{
QVariantList vars;
const int rows = listcells.size();
for(int i = 0; i < rows; ++i)
{
vars.append(QVariant(listcells[i]));
}
varres = QVariant(vars);
}
void ExcelOperator::convertToColName(int idata, QString &strres)
{
Q_ASSERT(idata > 0 && idata<65535);
int tempData = idata / 26;
if(tempData > 0)
{
int mode = idata % 26;
convertToColName(mode, strres);
convertToColName(tempData, strres);
}
else
{
strres = (to26AlphabetString(idata)+ strres);
}
}
// 数字转换为26字母
QString ExcelOperator::to26AlphabetString(int idata)
{
QChar ch = idata + 0x40; // A对应0x41
return QString(ch);
}
近期下载者:
相关文件:
收藏者: