Quick start guide - norbert-radyk/spoiwo GitHub Wiki

Prerequisites

Please ensure that you've added spoiwo dependency to you project, which can be configured via number of dependency management tools (including Ivy, Maven and SBT) or downloaded and added directly to your classpath. See here for more details.

Hello World example

This code below generates the simplest imaginable workbook, with one sheet having a single row, which has the 'Hello World!' value in the first column.

import com.norbitltd.spoiwo.model.{Row, Sheet}
import com.norbitltd.spoiwo.natures.xlsx.Model2XlsxConversions._
 
object HelloWorld {

  def main(args: Array[String]) {
    val helloWorldSheet = Sheet(name = "Hello Sheet",
      row = Row().withCellValues("Hello World!")
    )
    helloWorldSheet.saveAsXlsx("C:\\Reports\\hello_world.xlsx")
  }
}
  • In order to generate the workbook, you need to create a model representing the workbook.
  • Once this is done, you need to decide to which format you'd like to convert the model and import the relevant conversions.
  • Once the conversions have been imported successfully your model objects will be enriched with the conversion methods, so you can now execute saveAsXlsx method on your sheet.

The complete code example can be found here

Complex example

The HelloWorld example represents the general approach to the reports generation via Spoiwo, which can be summarized in 3 simple steps:

  1. Define spreadsheet using Spoiwo model
  2. Import the required format conversions
  3. Execute the conversion

Lets see how this approach is going to, when we're trying to define a slightly more complex example, where we define a spreadsheet representing the list of some famous physicists.

First lets define the Spoiwo model:

val headerStyle =
  CellStyle(fillPattern = CellFill.Solid, fillForegroundColor = Color.AquaMarine, font = Font(bold = true))

val gettingStartedSheet = Sheet(name = "Some serious stuff")
  .withRows(
    Row(style = headerStyle).withCellValues("NAME", "BIRTH DATE", "DIED AGED", "FEMALE"),
    Row().withCellValues("Marie Curie", new LocalDate(1867, 11, 7), 66, true),
    Row().withCellValues("Albert Einstein", new LocalDate(1879, 3, 14), 76, false),
    Row().withCellValues("Erwin Shrodinger", new LocalDate(1887, 8, 12), 73, false)
  )
  .withColumns(
    Column(index = 0, style = CellStyle(font = Font(bold = true)), autoSized = true)
  )

In this definition you can see a few unfamillar bits and pieces. Firstly we define a headerStyle which is the style of the cells we will use for our header row - this style will not only make our font bold, but also will provide an aquamarine background color.

Next we're moving to defining the sheet itself. You can see that the first row in the sheet is defined with the style = headerStyle - this is the way you define model object properties in Spoiwo. While some model objects can have multiple properties all of them are optional, so you always choose only the ones you want to define (i.e. in the next rows we don't define the style and leave the default formatting). In addition to the rows we also define that first column should be bold and auto-sized to the content width.

Once the model is defined what's left is only to import required conversion and call the conversion as below:

import com.norbitltd.spoiwo.natures.xlsx.Model2XlsxConversions._

gettingStartedSheet.saveAsXlsx("C:\\Reports\\getting_started.xlsx")

The full example can be found here

More examples

Should you crave for even more examples, I recommend you to go through the POI Busy Developers' Guide which we've converted to Spoiwo style here (matching Java functions can be found here)