Skip to content

Commit 47f2731

Browse files
authored
Merge pull request #65 from GitterRalf/Autosizing
Autosizing
2 parents 5052263 + 6678918 commit 47f2731

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

QXlsx/header/xlsxdocument.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ class Document : public QObject
116116

117117
bool changeimage(int filenoinmidea,QString newfile); // add by liufeijin20181025
118118

119+
bool autosizeColumnWidth(const CellRange &range);
120+
bool autosizeColumnWidth(int column);
121+
bool autosizeColumnWidth(int colFirst, int colLast);
122+
bool autosizeColumnWidth(void);
123+
124+
private:
125+
QMap<int, int> getMaximalColumnWidth(int firstRow=1, int lastRow=INT_MAX);
126+
127+
119128
private:
120129
Q_DISABLE_COPY(Document) // Disables the use of copy constructors and
121130
// assignment operators for the given Class.

QXlsx/source/xlsxdocument.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include <QPointF>
4747
#include <QBuffer>
4848
#include <QDir>
49+
#include <QDebug>
50+
4951

5052
/*
5153
From Wikipedia: The Open Packaging Conventions (OPC) is a
@@ -1134,4 +1136,134 @@ bool Document::changeimage(int filenoinmidea, QString newfile)
11341136
// liufeijin }}
11351137

11361138

1139+
/*!
1140+
Returns map of columns with there maximal width
1141+
*/
1142+
QMap<int, int> Document::getMaximalColumnWidth(int firstRow, int lastRow)
1143+
{
1144+
const int defaultPixelSize = 11; //Default font pixel size of excel?
1145+
int maxRows = -1;
1146+
int maxCols = -1;
1147+
QVector<CellLocation> cellLocation = currentWorksheet()->getFullCells(&maxRows, &maxCols);
1148+
1149+
QMap<int, int> colWidth;
1150+
1151+
for(int i=0; i < cellLocation.size(); i++)
1152+
{
1153+
int col = cellLocation.at(i).col;
1154+
int row = cellLocation.at(i).row;
1155+
int fs = cellLocation.at(i).cell->format().fontSize();
1156+
if( fs <= 0)
1157+
{
1158+
fs = defaultPixelSize;
1159+
}
1160+
1161+
// QString str = cellLocation.at(i).cell.data()->value().toString();
1162+
QString str = read(row, col).toString();
1163+
1164+
double w = str.length() * double(fs) / defaultPixelSize + 1; // width not perfect, but works reasonably well
1165+
1166+
if( (row >= firstRow) && (row <= lastRow))
1167+
{
1168+
if( w > colWidth.value(col))
1169+
{
1170+
colWidth.insert(col, int(w));
1171+
}
1172+
}
1173+
}
1174+
1175+
return colWidth;
1176+
}
1177+
1178+
1179+
/*!
1180+
Auto ets width in characters of columns with the given \a range.
1181+
Returns true on success.
1182+
*/
1183+
bool Document::autosizeColumnWidth(const CellRange &range)
1184+
{
1185+
bool erg = false;
1186+
1187+
if( !range.isValid())
1188+
{
1189+
return false;
1190+
}
1191+
1192+
QMap<int, int> colWidth = getMaximalColumnWidth(range.firstRow(), range.lastRow());
1193+
1194+
foreach(int key, colWidth.keys())
1195+
{
1196+
if( (key >= range.firstColumn()) && (key <= range.lastColumn()) )
1197+
{
1198+
erg |= setColumnWidth(key, colWidth.value(key));
1199+
}
1200+
}
1201+
1202+
return erg;
1203+
}
1204+
1205+
1206+
/*!
1207+
Auto sets width in characters \a column . Columns are 1-indexed.
1208+
Returns true on success.
1209+
*/
1210+
bool Document::autosizeColumnWidth(int column)
1211+
{
1212+
bool erg = false;
1213+
1214+
QMap<int, int> colWidth = getMaximalColumnWidth();
1215+
1216+
foreach(int key, colWidth.keys())
1217+
{
1218+
if( key == column)
1219+
{
1220+
erg |= setColumnWidth(key, colWidth.value(key));
1221+
}
1222+
}
1223+
1224+
return erg;
1225+
}
1226+
1227+
1228+
/*!
1229+
Auto sets width in characters for columns [\a colFirst, \a colLast]. Columns are 1-indexed.
1230+
Returns true on success.
1231+
*/
1232+
bool Document::autosizeColumnWidth(int colFirst, int colLast)
1233+
{
1234+
bool erg = false;
1235+
1236+
QMap<int, int> colWidth = getMaximalColumnWidth();
1237+
1238+
foreach(int key, colWidth.keys())
1239+
{
1240+
if( (key >= colFirst) && (key <= colLast) )
1241+
{
1242+
erg |= setColumnWidth(key, colWidth.value(key));
1243+
}
1244+
}
1245+
1246+
return erg;
1247+
}
1248+
1249+
1250+
/*!
1251+
Auto sets width in characters for all columns.
1252+
Returns true on success.
1253+
*/
1254+
bool Document::autosizeColumnWidth(void)
1255+
{
1256+
bool erg = false;
1257+
1258+
QMap<int, int> colWidth = getMaximalColumnWidth();
1259+
1260+
foreach(int key, colWidth.keys())
1261+
{
1262+
erg |= setColumnWidth(key, colWidth.value(key));
1263+
}
1264+
1265+
return erg;
1266+
}
1267+
1268+
11371269
QT_END_NAMESPACE_XLSX

TestExcel/rowcolumn.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,47 @@ int rowcolumn()
3737
xlsx.setColumnWidth(9, 16, 5.0);
3838
xlsx.setColumnFormat(9, 16, format2);
3939

40+
41+
// Autosize columns
42+
xlsx.addSheet("Autosize");
43+
QXlsx::Format header;
44+
header.setFontBold(true);
45+
header.setFontSize(20);
46+
47+
//Custom number formats
48+
int row = 2;
49+
xlsx.write(row, 1, "Small", header);
50+
xlsx.write(row, 2, "Large header", header);
51+
for (int i=0; i<10; ++i)
52+
{
53+
xlsx.write(row+1+i, 1, i);
54+
xlsx.write(row+1+i, 2, i);
55+
}
56+
xlsx.autosizeColumnWidth(1, 2);
57+
58+
xlsx.write(2, 4, "Small", header);
59+
xlsx.write(2, 5, "<--- Large header --->", header);
60+
for (int i=0; i<10; ++i)
61+
{
62+
xlsx.write(row+1+i, 4, exp(i));
63+
xlsx.write(row+1+i, 5, exp(i));
64+
}
65+
xlsx.autosizeColumnWidth(4, 5);
66+
67+
68+
xlsx.write(1, 7, "<--- Very big first line --->", header);
69+
xlsx.write(row, 7, "Small", header);
70+
xlsx.write(row, 8, "<--- Large header --->", header);
71+
for (int i=0; i<10; ++i)
72+
{
73+
xlsx.write(row+1+i, 7, exp(i));
74+
xlsx.write(row+1+i, 8, exp(i));
75+
}
76+
xlsx.autosizeColumnWidth(QXlsx::CellRange(2, 7, 1000, 8));
77+
78+
79+
80+
4081
xlsx.saveAs("rowcolumn.xlsx");
4182
return 0;
4283
}

0 commit comments

Comments
 (0)