#include "sheetmapper.h"
#include <QXmlStreamReader>
#include <iostream>
SheetMapper::SheetMapper() {}
SheetMapper::SheetMapper(const QByteArray& data){
setData(data);
}
int SheetMapper::insertRow(int index){
auto it = sheet.rbegin();
for(;it < sheet.rend();++it){
if(it->first==index) break;
}
if(it==sheet.rend()){
SheetRow row(index,SheetCol());
sheet.emplace_back(row);
}
return index;
}
int SheetMapper::insertCol(int row,int col,QString value){
if(row<1||col<1||value.isEmpty()) return 0;
auto it = sheet.rbegin();
for(;it<sheet.rend();++it){
if(it->first==row) break;
}
if(it==sheet.rend()){
return 0;
}
it->second.emplace_back(col,value);
return col;
}
int SheetMapper::intIndex(QString index){
int idx = 0;
for(int i=0;i<index.size();++i){
if(index[i].isDigit()) break;
else {
idx*=10;
idx += index[i].toLatin1()-'A'+1;
}
}
return idx;
}
void SheetMapper::dimension(QString ref){
bool isLast = false;
auto compute=[](int&row,int& col,QChar val){
if(!val.isDigit()){
col*=10;
col += val.toLatin1()-'A'+1;
}
else{
row*=10;
row += val.toLatin1()-'0';
}
};
for(int i=0;i<ref.size();++i){
if(!isLast){
if(ref[i]==':'){
isLast = true;
}
else compute(firstRow,firstCol,ref[i]);
}
else compute(lastRow,lastCol,ref[i]);
}
}
bool SheetMapper::setData(const QByteArray& data){
QXmlStreamReader reader(data);
if(reader.hasError()){
return false;
}
reader.readNext();
int currentRow = 0;
this->firstRow = 0;
while(!reader.atEnd()) {
if(reader.isStartElement()){
if(this->firstRow<1){
if(reader.name().toString()=="dimension"){
dimension(reader.attributes().value("ref").toString());
}
}
if(reader.name().toString()=="row"){
QString index = reader.attributes().value("r").toString();
if(!index.isEmpty()) currentRow = insertRow(index.toInt());
}
if(reader.name().toString()=="c"){
QString index = reader.attributes().value("r").toString();
int col = intIndex(index);
reader.readNext();
if(currentRow>0&& col>0) insertCol(currentRow,col,reader.readElementText());
}
}
reader.readNext();
}
return true;
}
void SheetMapper::displayMapper() const{
std::cout<<"sheet-mapper rows:"<<this->sheet.size()<<std::endl;
for(const auto& it :this->sheet){
std::cout<<"row:"<<it.first<<std::endl;
for(const auto& itor:it.second){
std::cout<<itor.first<<":"<<itor.second.toStdString()<<" ";
}
std::cout<<std::endl;
}
}
QString SheetMapper::data(const int& row,const int& col) const{
for(auto it = sheet.begin();it!= sheet.end();++it){
if(it->first==row){
for(auto itor = it->second.begin();itor!=it->second.end();++itor){
if(itor->first==col){
return itor->second;
}
}
}
}
return QString();
}
void SheetMapper::format(int width){
if(width<1) width=1;
int j = this->firstCol;
while(j<=this->lastRow){
for(size_t i=0;i<sheet.size();++i){
std::vector<QString> temp;
for(auto it:sheet[i].second){
if(it.first>=j && it.first<j+width){
temp.emplace_back(it.second);
}
}
if(temp.size()>0) sheetFormat.emplace_back(temp);
}
j+=width;
}
}
Sheet SheetMapper::sheetData() const{
return sheet;
}
SheetFormat SheetMapper::formatData() const{
return sheetFormat;
}
void SheetMapper::displayFormat() const{
std::cout<<"sheet-format rows:"<<this->sheet.size()<<std::endl;
for(const auto& it :this->sheetFormat){
for(const auto& itor:it){
std::cout<<itor.toStdString()<<" ";
}
std::cout<<std::endl;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
untitled.rar (9个子文件)
sheetmapper.h 2KB
untitled.pro 640B
main.cpp 452B
workbook.h 572B
workbook.cpp 2KB
untitled.pro.user 19KB
xlsxhandler.h 631B
xlsxhandler.cpp 3KB
sheetmapper.cpp 4KB
共 9 条
- 1
资源评论
石悼花
- 粉丝: 49
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功