BasicExcel 对Excel 的操作使用


包含头文件和命名空间

#include "BasicExcel.hpp"
using namespace YExcel;

    BasicExcel e;
    e.New(1);

    QString strTableName = pageName;
    BasicExcelWorksheet* sheet = e.AddWorksheet(reinterpret_cast( strTableName.utf16()), -1);
    sheet = e.GetWorksheet(reinterpret_cast( strTableName.utf16()));

    if (!sheet) return false;

    BasicExcelCell* cell;
    cell = sheet->Cell(0, 0);
    QString str = "test";
 if( cell) cell->SetWString(reinterpret_cast(str.utf16()));

 
  

    QFileInfo info(fileName);
    QByteArray fileNameu = QFile::encodeName(info.filePath());
    const char * encodedName = fileNameu.constData();               //Valid as long as fileName exists

    BasicExcel e;
    if(!e.Load(encodedName)) return false;

    BasicExcelWorksheet* sheet = e.GetWorksheet(size_t(0));
    if(!sheet) return false;


    int rowTotal = (int)sheet->GetTotalRows();
    int colTotal = (int)sheet->GetTotalCols();

    if(rowTotal<= 0 || colTotal<=0 ) return false;

    //QVector vecData;
    // 创建 MapList
    // 坐标对应shape
    QList < QMap < int, QString> > shapeMapList;
    for(int k = 0; k < colTotal; k++)
    {
        QMap< int, QString> mmshapeHash;
        shapeMapList.append( mmshapeHash);       //.insert(j, mmshapeHash);
    }

    BasicExcelCell* cell;
    for(int i = 0; i < colTotal ; i++)
    {
        for(int j = 0; j < rowTotal; j++)
        {
            cell = sheet->Cell(j, i);

            QString cellStr;
            switch (cell->Type()){
            case BasicExcelCell::INT:
                cellStr = QString::number(cell->GetInteger(), 10);
                break;
            case BasicExcelCell::DOUBLE:
                cellStr = QString::number( cell->GetDouble(), 'g', 6);
                break;
            case BasicExcelCell::STRING:
                cellStr = QString::fromUtf8(cell->GetString());
                break;
            case BasicExcelCell::WSTRING:
                cellStr = QString::fromWCharArray(cell->GetWString());
                break;
            case BasicExcelCell::UNDEFINED:
                cellStr = QString::fromRawData((const QChar*)cell->GetString(), cell->GetStringLength());
                break;
            }

            if(!cellStr.isEmpty()){
                shapeMapList.operator [](i).insert(j, cellStr);
                qDebug() << " --- ("  << i  << ":"  << j  << ") : "  << shapeMapList[i][j];
            }
        }
    }





你可能感兴趣的:(Qt)