XSSFWorkbook workbook = new XSSFWorkbook()
XSSFSheet sheet = workbook.createSheet('Tweets')
XSSFRow headerRow = sheet.createRow(0)
['User', 'Favourites', 'Retweets','Text'].eachWithIndex { header, i ->
XSSFCell headerCell = headerRow.createCell(i)
headerCell.cellValue = header
}
tweets.eachWithIndex { Status tweet, int i ->
XSSFRow row = sheet.createRow(i + 1)
XSSFCell screenNameCell = row.createCell(0)
screenNameCell.cellValue = tweet.user.screenName
XSSFCell favouriteCountCell = row.createCell(1)
favouriteCountCell.cellValue = tweet.favoriteCount
XSSFCell retweetCountCell = row.createCell(2)
retweetCountCell.cellValue = tweet.retweetCount
XSSFCell textCell = row.createCell(3)
textCell.cellValue = tweet.text
}
workbook.write(out)
Headlines and status per each row
SpreadsheetBuilder builder = PoiSpreadsheetBuilder.create(out)
builder.build {
sheet 'Tweets', {
row {
cell 'User'
cell 'Favourites'
cell 'Retweets'
cell 'Text'
}
tweets.each { tweet ->
row {
cell tweet.user.screenName
cell tweet.favoriteCount
cell tweet.retweetCount
cell tweet.text
}
}
}
}
Bold headlines with filter and scroll freeze, status per each row, some data highlighted
builder.build {
// ...
sheet 'Tweets', {
filter auto
freeze 0, 1
row {
// ...
}
// ...
}
}
builder.build {
style 'headline', {
font {
style bold
}
}
style 'text', {
wrap text
align center, justify
}
style 'highlighted', {
foreground orange
}
sheet 'Tweets', {
// ...
row {
style 'headline'
// ...
}
}
}
criteria.query {
sheet {
row {
cell {
style {
font {
style bold
}
}
}
}
}
}.size() == 5
criteria.query {
sheet {
row {
cell {
style {
foreground orange
}
}
}
}
}.rows.size() == 1
criteria.query {
sheet {
row {
cell {
value 'yes'
}
}
}
}.size() == 73
Non-tabular structure, different text styles, borders, print setup
// | A | B | C |
// -----------------------------------------
// 1 | Name | Text | Fav. Count |
// ---- - - -
// 2 | Name | Text | Fav. Count |
// --------------- ----------------
// 3 | Handle | Text | Rtw. Count |
// --------------- - -
// 4 | Create | Text | Rtw. Count |
// -----------------------------------------
sorted.each { tweet ->
row {
cell {
value tweet.user.name
rowspan 2
}
cell {
value tweet.text
rowspan 4
}
cell {
value tweet.retweetCount
rowspan 2
}
}
row()
row {
cell { value "@$tweet.user.screenName" }
cell('C') {
value tweet.favoriteCount
rowspan 2
}
}
row { cell { value tweet.createdAt } }
}
sorted.each { tweet ->
row {
cell {
value tweet.user.name
width 4 cm
}
cell {
value tweet.text
width 14 cm
}
cell {
value tweet.retweetCount
width 2 cm
}
}
row()
row {
cell { value "@$tweet.user.screenName" }
cell('C') { value tweet.favoriteCount }
}
row {
cell { value tweet.createdAt }
}
}
sheet('Tweets') {
page {
paper a4
orientation portrait
}
}
style 'date', {
format 'dd.mm.yyyy hh:mm'
}
style 'border-top', {
border top, {
style thick
color black
}
}
style 'border-bottom', {
border bottom, {
style thick
color black
}
}
style 'border-left', {
border left, {
style thick
color black
}
}
style 'border-right', {
border right, {
style thick
color black
}
}
Cell productCell = criteria.query {
sheet {
row {
cell {
value 'Product'
style { font { style bold } }
}
}
}
}.cell
Cell quantityCell = criteria.query {
sheet {
row {
cell {
value 'Qty'
style { font { style bold } }
}
}
}
}.cell
Cell totalRow = criteria.query {
sheet {
row {
cell('A') {
value 'Total'
style { font { style bold } }
}
}
}
}.row
Map<String, Integer> quantities = [:]
Cell productNameLineCell = productCell.bellow
Cell quantityLineCell = quantityCell.bellow
while (productNameLineCell && productNameLineCell.row.number < totalRow.number) {
quantities[productNameLineCell.read(String)] = quantityLineCell.read(Number).intValue()
productNameLineCell = productNameLineCell.bellow
quantityLineCell = quantityLineCell.bellow
}