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));
}
Cell *cell =sheet->cellAt(i+1, j+1);
foreach (CellRange range, sheet->mergedCells())
table_new->setSpan(range.firstRow()-1, range.firstColumn()-1, range.rowCount(), range.columnCount());
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;
}
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