/// <summary> /// 設置表頁的列寬度自適應 /// </summary> /// <param name="sheet">worksheet對象</param> void setColumnWithAuto(Worksheet sheet) { Cells cells = sheet.Cells; int columnCount = cells.MaxColumn; //獲取表頁的最大列數 int rowCount = cells.MaxRow; //獲取表頁的最大行數 for (int col = 0; col < columnCount; col++) { sheet.AutoFitColumn(col, 0, rowCount); } for (int col = 0; col < columnCount; col++) { cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30); } }
在調用Aspose.Cells的過程中,開始的時候以為sheet.Cells.Rows.Count獲取到的是表頁的總行數,這樣在循環的時候就可以作為上限,結果用這個屬性獲取到的值是0,很奇怪,這個屬性獲取到的值是錯誤的,於是試着用其他方法獲取總行數.用了一些折中的方法,解決了問題,后來在使用時候發現了
cells.MaxRow這個屬性能獲取到最大的行數,這個行數跟總行數的值應該是一樣的.
cells.MaxDataRow;獲取到的是最大的數據行(有數據的行的最大值--這個屬性解釋起來比較麻煩,慢慢理解一下)
11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99 | |
cells.MaxRow的值是2;
cells.MaxDataRow的值是1;
對於列也有同樣的屬性可以調用.