QtXlsx读写.xlsx基本内容

QtXlsx读写.xlsx基本内容

QtXlsx例子中方法还是很全的,这里整理一下

.xlsx实质上是一个zip压缩的xml文件集,可以把后缀名改为.zip打开。


QString filePath = QFileDialog::getOpenFileName(0, "Open xlsx file", QString(), "*.xlsx");
if (filePath.isEmpty())
{
    qDebug()<<"load file fails";
    return NULL;
}
Document* xlsx=new Document(filePath);
Workbook* myworkbook=xlsx->workbook();

foreach(QString sheetName,myworkbook->sheetNames())
{
       Worksheet *sheet = static_cast(myworkbook->sheet(sheetName));
}


从QList容器 sheetNames()中获得sheetName,从Worksheet中获取 Worksheet 指针
sheet->dimension().lastRow()
sheet->dimension().lastColumn()
可以获得最后一排的有效数据的位置,

double row_heigh=sheet->rowHeight(i+1);
double col_width=sheet->columnWidth(j+1);

可以获得在xlsx文件中保存的行高和列宽,这里注意QtXlsx行和列是从1开始的,所以获得行和列获取的都是i+1和j+1;

                        Cell *cell =sheet->cellAt(i+1, j+1);

获得i行j列的Cell指针

Cell中的内容就比较多了,其中
Format cell_format=cell->format();是单元格格式的最重要内容

Format是QtXlsx 自定的类,其中有排版,字体,背景色等重要的信息
而 Cell中的文字内容就可以很简单的
cell->value().toString()
获得

sheet->mergedCells()中可以获得合并单元格的信息
foreach (CellRange range, sheet->mergedCells())
        table_new->setSpan(range.firstRow()-1, range.firstColumn()-1, range.rowCount(), range.columnCount());


QtXlsx和标准的.xlsx文件关于行和列的单位不一样,这里我们直接用的pix,而.xlsx行高用的是磅,而列宽用的是字符
在QtXlsx其实有一个私有方法告诉你怎么从.xlsx转换成像素
int WorksheetPrivate::rowPixelsSize(int row) const
{
    double height;
    if (row_sizes.contains(row))
        height = row_sizes[row];
    else
        height = default_row_height;
    return static_cast(4.0 / 3.0 *height);
}

int WorksheetPrivate::colPixelsSize(int col) const
{
    double max_digit_width = 7.0; //For Calabri 11
    double padding = 5.0;
    int pixels = 0;

    if (col_sizes.contains(col)) {
        double width = col_sizes[col];
        if (width < 1)
            pixels = static_cast(width * (max_digit_width + padding) + 0.5);
        else
            pixels = static_cast(width * max_digit_width + 0.5) + padding;
    } else {
        pixels = 64;
    }
    return pixels;
}

但是纵观整个QtXlsx都没用到过这两个方法,所以在自己存读.xlsx文件时,最好是对原行高和列宽进行处理,否则别人用office等工具打开的时候会非常难看
我是直接在源码上进行了修改,方便以后调用

QtXlsx 自定义了一个默认行高和默认列宽
    double defaultColWidth;
    double defaultRowHeight;

其中 defaultRowHeight 为15,baseColWidth 为8,虽然写了读取默认值的方法,但是读取的值并没有给sheet....具体可以看
void WorksheetPrivate::loadXmlSheetFormatProps(QXmlStreamReader &reader)
不知道是不是我眼拙,但是设置这个值并没什么用。office中默认值为 13.5和9 可以自己根据具体情况具体修改一下。
void WorksheetPrivate::loadXmlSheetFormatProps(QXmlStreamReader &reader)
{
    Q_ASSERT(reader.name() == QLatin1String("sheetFormatPr"));
    QXmlStreamAttributes attributes = reader.attributes();
    XlsxSheetFormatProps formatProps;

    //Retain default values
    foreach (QXmlStreamAttribute attrib, attributes) {
        if(attrib.name() == QLatin1String("baseColWidth") ) {
            formatProps.baseColWidth = attrib.value().toString().toInt();
        } else if(attrib.name() == QLatin1String("customHeight")) {
            formatProps.customHeight = attrib.value() == QLatin1String("1");
        } else if(attrib.name() == QLatin1String("defaultColWidth")) {
            formatProps.defaultColWidth = attrib.value().toString().toDouble();
        } else if(attrib.name() == QLatin1String("defaultRowHeight")) {
            formatProps.defaultRowHeight = attrib.value().toString().toDouble();
        } else if(attrib.name() == QLatin1String("outlineLevelCol")) {
            formatProps.outlineLevelCol = attrib.value().toString().toInt();
        } else if(attrib.name() == QLatin1String("outlineLevelRow")) {
            formatProps.outlineLevelRow = attrib.value().toString().toInt();
        } else if(attrib.name() == QLatin1String("thickBottom")) {
            formatProps.thickBottom = attrib.value() == QLatin1String("1");
        } else if(attrib.name() == QLatin1String("thickTop")) {
            formatProps.thickTop  = attrib.value() == QLatin1String("1");
        } else if(attrib.name() == QLatin1String("zeroHeight")) {
            formatProps.zeroHeight = attrib.value() == QLatin1String("1");
        }
    }

    if(formatProps.defaultColWidth == 0.0) { //not set
        formatProps.defaultColWidth = WorksheetPrivate::calculateColWidth(formatProps.baseColWidth);
    }

}
XlsxSheetFormatProps formatProps 这个结构体赋予了所有的读取值,然后就没后然后了;


而写入的时候 干脆没有默认行宽
    writer.writeEndElement();//sheetViews

    writer.writeStartElement(QStringLiteral("sheetFormatPr"));
    writer.writeAttribute(QStringLiteral("defaultRowHeight"), QString::number(d->default_row_height));
    if (d->default_row_height != 15)
        writer.writeAttribute(QStringLiteral("customHeight"), QStringLiteral("1"));
    if (d->default_row_zeroed)
        writer.writeAttribute(QStringLiteral("zeroHeight"), QStringLiteral("1"));
    if (d->outline_row_level)
        writer.writeAttribute(QStringLiteral("outlineLevelRow"), QString::number(d->outline_row_level));
    if (d->outline_col_level)
        writer.writeAttribute(QStringLiteral("outlineLevelCol"), QString::number(d->outline_col_level));
    //for Excel 2010
    //    writer.writeAttribute("x14ac:dyDescent", "0.25");
    writer.writeEndElement();//sheetFormatPr

有需要的话自己补一下吧





你可能感兴趣的:(Qt)