Qt5 import Excel data

#ifndef ZEXCELPARSER_H
#define ZEXCELPARSER_H

#include <QObject>
#include <QXmlStreamReader>
#include <QFile>
#include <QHash>
typedef struct
{
    QString ssID;
    QString ssName;
    QString ssVertical;
    QString ssHorizontal;
    QString ssFontName;
    qint32 ssSize;
}ZExcelStyleInfo;
typedef struct
{
    QString ssStyleID;
    QString ssType;
    qint32 mergeDown;
    qint32 mergeAcross;
    QString ssData;
}ZExcelCellInfo;
typedef struct
{
    qint32 ssAutoFitHeight;
    qreal ssHeight;
    QList<ZExcelCellInfo*> cellInfoList;
}ZExcelRowInfo;
typedef struct
{
    qint32 ssExpandedColumnCount;
    qint32 ssExpandedRowCount;
    qreal ssDefaultColumnWidth;
    qreal ssDefaultRowHeight;
    QList<ZExcelRowInfo*> rowInfoList;
}ZExcelTableInfo;
class ZExcelParser : public QObject
{
    Q_OBJECT
public:
    explicit ZExcelParser(QObject *parent = 0);
    ~ZExcelParser();

    qint32 ZParserXmlFile(QString excelXmlFile);

    ZExcelStyleInfo*  ZGetStyleInfo(QString styleID);
    QList<ZExcelTableInfo*>* ZGetTableInfo();
signals:

public slots:

private:
    //<Workbook></Workbook>
    void ZXmlReadWorkbook();

    //<Styles></Styles>
    void ZXmlReadWorkbookStyles();
    void ZXmlReadWorkbookStylesStyle(QString ssID);
    void ZXmlReadWorkbookStylesStyleAlignment(QString ssID);
    void ZXmlReadWorkbookStylesStyleFont(QString ssID);

    //<Worksheet></Worksheet>
    void ZXmlReadWorkbookWorkSheet();
    void ZXmlReadWorkbookWorkSheetTable();
    void ZXmlReadWorkbookWorkSheetTableRow(ZExcelTableInfo *tableInfo);
    void ZXmlReadWorkbookWorkSheetTableRowCell(ZExcelRowInfo *rowInfo);
    void ZXmlReadWorkbookWorkSheetTableRowCellData(ZExcelCellInfo *cellInfo);
private:
    QXmlStreamReader *m_xmlReader;
    QHash<QString,ZExcelStyleInfo*> m_styleHash;
    QList<ZExcelTableInfo*> m_tableInfo;
};

#endif // ZEXCELPARSER_H

//created by shell.albert@gmail.com
//April 15,2015.
//1811543668@qq.com
#include "zexcelparser.h"
#include <QDebug>
ZExcelParser::ZExcelParser(QObject *parent) :
    QObject(parent)
{
    this->m_xmlReader=new QXmlStreamReader;
}
ZExcelParser::~ZExcelParser()
{
    delete this->m_xmlReader;
}
qint32 ZExcelParser::ZParserXmlFile(QString excelXmlFile)
{
    //read data to memory.
    QFile tXmlFile(excelXmlFile);
    if(!tXmlFile.open(QIODevice::ReadOnly))
    {
        return  -1;
    }
    QString tXmlFileData=QString(tXmlFile.readAll());
    if(!tXmlFileData.contains("Workbook") || !tXmlFileData.contains("Worksheet"))
    {
        return -1;
    }
    if(!tXmlFileData.contains("Table") || !tXmlFileData.contains("Row") || !tXmlFileData.contains("Cell") || !tXmlFileData.contains("Data"))
    {
        return -1;
    }

    tXmlFile.seek(0);
    this->m_xmlReader->setDevice(&tXmlFile);
    while(!this->m_xmlReader->atEnd())
    {
        if(this->m_xmlReader->name()=="Workbook" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbook();
            //parse <Workbook> finish,read next tag.
            this->m_xmlReader->readNext();
        }else{
            this->m_xmlReader->readNext();
        }
    }
    tXmlFile.close();

#if 1
    QHash<QString,ZExcelStyleInfo*>::iterator tIt;
    for(tIt=this->m_styleHash.begin();tIt!=this->m_styleHash.end();tIt++)
    {
        ZExcelStyleInfo *tStyleInfo=tIt.value();
        qDebug()<<tStyleInfo->ssID<<tStyleInfo->ssName<<tStyleInfo->ssHorizontal<<tStyleInfo->ssVertical<<tStyleInfo->ssFontName<<tStyleInfo->ssSize;
    }
#endif

#if 1
    for(qint32 i=0;i<this->m_tableInfo.count();i++)
    {
        ZExcelTableInfo *tTableInfo=this->m_tableInfo.at(i);
        qDebug()<<"Table:"<<i;
        qDebug()<<"RowCount:"<<tTableInfo->ssExpandedRowCount<<"ColumnCount:"<<tTableInfo->ssExpandedColumnCount;
        qDebug()<<"RowHeight:"<<tTableInfo->ssDefaultRowHeight<<"ColumnWidth:"<<tTableInfo->ssDefaultColumnWidth;
        for(qint32 j=0;j<tTableInfo->rowInfoList.count();j++)
        {
            ZExcelRowInfo *tRowInfo=tTableInfo->rowInfoList.at(j);
            qDebug()<<"Row:"<<j;
            qDebug()<<"AutoFitHeight:"<<tRowInfo->ssAutoFitHeight<<"Height:"<<tRowInfo->ssHeight;
            for(qint32 k=0;k<tRowInfo->cellInfoList.count();k++)
            {
                ZExcelCellInfo *tCellInfo=tRowInfo->cellInfoList.at(k);
                qDebug()<<"Cell:"<<k;
                qDebug()<<"StyleID:"<<tCellInfo->ssStyleID<<"Type:"<<tCellInfo->ssType<<"Data:"<<tCellInfo->ssData<<tCellInfo->mergeDown<<tCellInfo->mergeAcross;
            }
        }
    }
#endif
    return 0;
}
//<Workbook></Workbook>
void ZExcelParser::ZXmlReadWorkbook()
{
    //current label is <Workbook>.
    while(!this->m_xmlReader->atEnd())
    {
        if(this->m_xmlReader->name()=="Workbook" && this->m_xmlReader->isEndElement())
        {
            //finish this tag,exit.
            break;
        } else if(this->m_xmlReader->name()=="Styles" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbookStyles();
            //parse <Styles> finish,read next tag.
            this->m_xmlReader->readNext();
        }else if(this->m_xmlReader->name()=="Worksheet" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbookWorkSheet();
            //parse <Worksheet> finish,read next tag.
            this->m_xmlReader->readNext();
        }else {
            this->m_xmlReader->readNext();
        }
    }
}
//<Styles></Styles>
void ZExcelParser::ZXmlReadWorkbookStyles()
{
    //current label is <Styles>.
    while(!this->m_xmlReader->atEnd())
    {
        if(this->m_xmlReader->name()=="Styles" && this->m_xmlReader->isEndElement())
        {
            //finish this tag,exit.
            break;
        }else if(this->m_xmlReader->name()=="Style" && this->m_xmlReader->isStartElement())
        {
            ZExcelStyleInfo *tStyleInfo=new ZExcelStyleInfo;
            tStyleInfo->ssID=this->m_xmlReader->attributes().value("ss:ID").toString();
            tStyleInfo->ssName=this->m_xmlReader->attributes().value("ss:Name").toString();
            //some default value set here.
            tStyleInfo->ssHorizontal="Left";
            tStyleInfo->ssVertical="Top";
            tStyleInfo->ssFontName="Serif";
            tStyleInfo->ssSize=14;

            //insert to hash.
            this->m_styleHash.insert(tStyleInfo->ssID,tStyleInfo);
            this->ZXmlReadWorkbookStylesStyle(tStyleInfo->ssID);
            //parse <Style> finish,read next tag.
            this->m_xmlReader->readNext();
        }else{
            this->m_xmlReader->readNext();
        }
    }
}
void ZExcelParser::ZXmlReadWorkbookStylesStyle(QString ssID)
{
    //current label is <Style>.
    while(!this->m_xmlReader->atEnd())
    {
        if(this->m_xmlReader->name()=="Style" && this->m_xmlReader->isEndElement())
        {
            //finish this tag,exit.
            break;
        }else if(this->m_xmlReader->name()=="Alignment" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbookStylesStyleAlignment(ssID);
            //parse <Alignment> finish,read next tag.
            this->m_xmlReader->readNext();
        }else if(this->m_xmlReader->name()=="Font" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbookStylesStyleFont(ssID);
            //parse <Font> finish,read next tag.
            this->m_xmlReader->readNext();
        }else{
            this->m_xmlReader->readNext();
        }
    }
}
void ZExcelParser::ZXmlReadWorkbookStylesStyleAlignment(QString ssID)
{
    //current label is <Alignment>.
    ZExcelStyleInfo *tStyleInfo=this->m_styleHash.value(ssID);
    tStyleInfo->ssHorizontal=this->m_xmlReader->attributes().value("ss:Horizontal").toString();
    tStyleInfo->ssVertical=this->m_xmlReader->attributes().value("ss:Vertical").toString();
}
void ZExcelParser::ZXmlReadWorkbookStylesStyleFont(QString ssID)
{
    //current label is <Font>.
    ZExcelStyleInfo *tStyleInfo=this->m_styleHash.value(ssID);
    tStyleInfo->ssFontName=this->m_xmlReader->attributes().value("ss:FontName").toString();
    tStyleInfo->ssSize=this->m_xmlReader->attributes().value("ss:Size").toString().toInt();
}

//<Worksheet></Worksheet>
void ZExcelParser::ZXmlReadWorkbookWorkSheet()
{
    //current label is <Worksheet>.
    while(!this->m_xmlReader->atEnd())
    {
           if(this->m_xmlReader->name()=="Worksheet" && this->m_xmlReader->isEndElement())
           {
               //finish this tag,exit.
               break;
           }else if(this->m_xmlReader->name()=="Table" && this->m_xmlReader->isStartElement())
           {
               this->ZXmlReadWorkbookWorkSheetTable();
               //parse <Table> finish,read next tag.
               this->m_xmlReader->readNext();
           }else{
               this->m_xmlReader->readNext();
           }
    }
}
void ZExcelParser::ZXmlReadWorkbookWorkSheetTable()
{
    //current label is <Table>.
    ZExcelTableInfo *tTableInfo=new ZExcelTableInfo;
    tTableInfo->ssExpandedColumnCount=this->m_xmlReader->attributes().value("ss:ExpandedColumnCount").toInt();
    tTableInfo->ssExpandedRowCount=this->m_xmlReader->attributes().value("ss:ExpandedRowCount").toInt();
    tTableInfo->ssDefaultColumnWidth=this->m_xmlReader->attributes().value("ss:DefaultColumnWidth").toFloat();
    tTableInfo->ssDefaultRowHeight=this->m_xmlReader->attributes().value("ss:DefaultRowHeight").toFloat();
    this->m_tableInfo.append(tTableInfo);
    //
    while(!this->m_xmlReader->atEnd())
    {
        if(this->m_xmlReader->name()=="Table" && this->m_xmlReader->isEndElement())
        {
            //finish this tag,exit.
            break;
        }else if(this->m_xmlReader->name()=="Row" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbookWorkSheetTableRow(tTableInfo);
            //parse <Row> finish,read next tag.
            this->m_xmlReader->readNext();
        }else{
            this->m_xmlReader->readNext();
        }
    }
}
void ZExcelParser::ZXmlReadWorkbookWorkSheetTableRow(ZExcelTableInfo *tableInfo)
{
    //current label is <Row>.
    ZExcelRowInfo *tRowInfo=new ZExcelRowInfo;
    tRowInfo->ssAutoFitHeight=this->m_xmlReader->attributes().value("ss:AutoFitHeight").toInt();
    tRowInfo->ssHeight=this->m_xmlReader->attributes().value("ss:Height").toFloat();
    tableInfo->rowInfoList.append(tRowInfo);

    /
    while(!this->m_xmlReader->atEnd())
    {
        if(this->m_xmlReader->name()=="Row" && this->m_xmlReader->isEndElement())
        {
            //finish this tag,exit.
            break;
        }else if(this->m_xmlReader->name()=="Cell" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbookWorkSheetTableRowCell(tRowInfo);
            //parse <Cell> finish,read next tag.
            this->m_xmlReader->readNext();
        }else{
            this->m_xmlReader->readNext();
        }
    }
}
void ZExcelParser::ZXmlReadWorkbookWorkSheetTableRowCell(ZExcelRowInfo *rowInfo)
{
    //current label is <Cell>.
    ZExcelCellInfo *tCellInfo=new ZExcelCellInfo;
    tCellInfo->ssStyleID=this->m_xmlReader->attributes().value("ss:StyleID").toString();
    tCellInfo->mergeDown=this->m_xmlReader->attributes().value("ss:MergeDown").toInt();
    tCellInfo->mergeAcross=this->m_xmlReader->attributes().value("ss:MergeAcross").toInt();
    rowInfo->cellInfoList.append(tCellInfo);

   
    while(!this->m_xmlReader->atEnd())
    {
        if(this->m_xmlReader->name()=="Cell" && this->m_xmlReader->isEndElement())
        {
            //finish this tag,exit.
            break;
        }else if(this->m_xmlReader->name()=="Data" && this->m_xmlReader->isStartElement())
        {
            this->ZXmlReadWorkbookWorkSheetTableRowCellData(tCellInfo);
            //parse <Data> finish,read next tag.
            this->m_xmlReader->readNext();
        }else{
            this->m_xmlReader->readNext();
        }
    }
}
void ZExcelParser::ZXmlReadWorkbookWorkSheetTableRowCellData(ZExcelCellInfo *cellInfo)
{
    //current label is <Data>.
    cellInfo->ssType=this->m_xmlReader->attributes().value("ss:Type").toString();
    cellInfo->ssData=this->m_xmlReader->readElementText();
}
ZExcelStyleInfo*  ZExcelParser::ZGetStyleInfo(QString styleID)
{
    if(this->m_styleHash.contains(styleID))
    {
        return this->m_styleHash.value(styleID);
    }else{
        return NULL;
    }
}
QList<ZExcelTableInfo*>* ZExcelParser::ZGetTableInfo()
{
    return &this->m_tableInfo;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值