QtXlsx讀寫 xlsx基本內容

2021-07-09 04:21:22 字數 3679 閱讀 3160

qtxlsx例子中方法還是很全的,這裡整理一下

.xlsx實質上是乙個zip壓縮的xml檔案集,可以把字尾名改為.zip開啟。

qstring filepath = qfiledialog::getopenfilename(0, "open xlsx file", qstring(), "*.xlsx");

if (filepath.isempty())

document* xlsx=new document(filepath);

workbook* myworkbook=xlsx->workbook();

foreach(qstring sheetname,myworkbook->sheetnames())

從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

int worksheetprivate::colpixelssize(int col) const

else

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)

else if(attrib.name() == qlatin1string("customheight")) else if(attrib.name() == qlatin1string("defaultcolwidth")) else if(attrib.name() == qlatin1string("defaultrowheight")) else if(attrib.name() == qlatin1string("outlinelevelcol")) else if(attrib.name() == qlatin1string("outlinelevelrow")) else if(attrib.name() == qlatin1string("thickbottom")) else if(attrib.name() == qlatin1string("thicktop")) else if(attrib.name() == qlatin1string("zeroheight"))

}if(formatprops.defaultcolwidth == 0.0)

}

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

有需要的話自己補一下吧

QtXlsx基本使用

最近有個需求是可以將程式中的資料生成excel報表,於是搜了搜,找到乙個蠻方便的qt外掛程式qtxlsx。qtxlsx是乙個可以讀寫excel檔案的庫。基本使用在github鏈結下方都有,這邊簡單描述下使用方式。github鏈結 在make的時候,可能會出現上述報錯,那是因為你沒有裝perl或者環境...

使用QtXlsx來讀寫excel檔案

概述 qtxlsx是功能非常強大和使用非常方便的操作excel類庫。包括對excel資料讀寫 excel資料格式設定及在excel裡面根據資料生成各種圖表。下面重點介紹如何安裝和使用qtxlsx。一 獲取qtxlsx。1 通過下面位址獲取 2 得到的是包括原始碼 各種例項的檔案包。3 解壓某個盤的根...

利用mmap dev mem 讀寫Linux記憶體

使用 hexedit dev mem 可以顯示所有物理記憶體中的資訊。運用mmap將 dev mem map出來,然後直接對其讀寫可以實現使用者空間的核心操作。以下是我寫的乙個sample include include include include include includeint main...