QtXlsx读写.xlsx基本内容
来源:互联网 发布:淘宝旗舰店有假货么 编辑:程序博客网 时间:2024/06/04 00:33
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]; else height = 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 11 double 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); else pixels = 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 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
有需要的话自己补一下吧
0 0
- QtXlsx读写.xlsx基本内容
- QtXlsx读写.xlsx的图片
- QtXlsx读写.xlsx的图片
- .xlsx文件读写库QT库,QtXlsx使用整理
- python 使用openpyxl 读写xlsx
- qt vs 读写xlsx文件
- 前端js用js-xlsx插件读取xlsx表格内容
- QtXlsx编译
- Python通过openpyxl读写xlsx文件
- Python读写Excel 2007文件(xlsx)
- 利用NPOI组件读写XLSX文件
- Python读写Excel 2007文件(xlsx)
- 使用ExcelDataReader读写excel(.xls,.xlsx)
- poi读写Excel,兼容2007xlsx
- nodejs--node-xlsx模块读写excel
- maven配置 poi 3.5包 读写excel 2007 xlsx
- android中如何读写.doc、.docx、.xls、.xlsx格式文件
- Apache POI 读写 Excel file (.xls or .xlsx)
- 归档与反归档
- 深入研究Servlet线程安全性问题
- XmlHttpReques
- 写/读文件程序---自己喜欢的
- mysql数据库系统数据库information_schema
- QtXlsx读写.xlsx基本内容
- OFBiz分布式事务
- centos下怎么设置Apache 开机启动!
- OFBiz开发之异步服务与计划任务(Job Scheduler)
- Linux源码查看
- OFBiz任务调度和Job机制
- 关于随机数的小问题
- 生成随机密码
- iOS当设置TableView为Group时如何隐藏第一行的间隔