QtXlsx读写.xlsx基本内容
2015-12-26 09:57
537 查看
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<Worksheet *>(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];elseheight = default_row_height;return static_cast<int>(4.0 / 3.0 *height);}
int WorksheetPrivate::colPixelsSize(int col) const{double max_digit_width = 7.0; //For Calabri 11double padding = 5.0;int pixels = 0;if (col_sizes.contains(col)) {double width = col_sizes[col];if (width < 1)pixels = static_cast<int>(width * (max_digit_width + padding) + 0.5);elsepixels = static_cast<int>(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 valuesforeach (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 setformatProps.defaultColWidth = WorksheetPrivate::calculateColWidth(formatProps.baseColWidth);}}XlsxSheetFormatProps formatProps 这个结构体赋予了所有的读取值,然后就没后然后了;而写入的时候 干脆没有默认行宽
writer.writeEndElement();//sheetViewswriter.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 Script
- Pyqtdeploy Complie Win32 Version PyQtWin32静态编译记录
- QTableView用法小结
- Tslib和Qt 4.8.4与在开发板上的移植
- Qt Creator的配置和开发初步测试
- MQTT协议简记
- Qt QGroupBox StyleSheet 边框设置
- .xlsx文件读写库QT库,QtXlsx使用整理
- Qt5.5下连接Mysql显示QSqlDatabase: QMYSQL driver not loaded but available
- QT插入图片1
- Qt5 中的 signal/slot 新语法
- Qt Xml 添加节点
- PyQt5系列教程(五)制作fastboot烧写器
- qt的
- Qt自定义界面类并提升(提升的窗口部件)
- Qt设计用户界面的三种方法
- pyqt QIcon 使用
- PyQt5教程(1) 介绍
- 打算翻译几篇pyqt的教程
- QT中的show()与exec()区别