开发者

Is there a Scala wrapper for Apache POI?

I would like to use Apache POI to read/create Excel files in an Scala app. Sure, I can use the POI library directly, it's Java after all, but I would like to have the Scala feel. So is there a Scala wrapper bringing the Scala f开发者_如何学Goeel (using implicit conversions), i.e. some kind of "Scala-POI-DSL" freely available?


Thanks to Dave Griffith's answer, I've hacked something similar to his DSL.

Workbook {
      Sheet("name") {
        Row(1) {
          Cell(1, "data") :: Cell(2, "data2") :: Nil
        } ::
        Row(2) {
          Cell(1, "data") :: Cell(2, "data2") :: Nil
        } :: Nil
      } ::
      Sheet("name2") {
        Row(2) {
          Cell(1, "data") :: Cell(2, "data2") :: Nil
        } :: Nil
      } :: Nil
    }.save("/home/path/ok.xls")

Code can be found here.


Given the lack of advanced, open source spreadsheet wrappers for Scala, I've started the development of Spoiwo: https://github.com/norbert-radyk/spoiwo. It allows the generation of the XSSFWorkbook and supports a significant subset of POI's functionality.

It still requires a bit of documentation, but the below should give a rough idea about its capabilities:

  • The Quick Start Guide
  • The rewritten version of POI Busy Developers' Guide

The example of simple spreadsheet using Spoiwo:

object GettingStartedExample {

  val headerStyle =
    CellStyle(fillPattern = CellFill.Solid, fillForegroundColor = Color.AquaMarine, fillBackgroundColor = 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)
    )

  def main(args: Array[String]) {
    gettingStartedSheet.saveAsXlsx("C:\\Reports\\getting_started.xlsx")
  }
} 


Fancy POI - Not much info to be found it seems but I guess it is what you're looking for.


This is utterly unhelpful, but I hacked up an Scala-ish DSL for POI. It allows code like

Workbook{
  Sheet("Multiplication"){
      for(i<-1 to 10){
         Row{
           for(j<-1 to 10){
             Cell(i*j)
           }
         }
      }
  }
}.writeToFile("multiplication.xls")

Sadly I can't give it out without checking with my boss, but to be honest it wasn't that hard to do. You should be able to reverse-engineer most of it from that example without much trouble.


If you're writing Office XML, you could consider avoiding POI and creating the XML directly (plus archiving it together). For simple spreadsheets it's pretty simple to do it, and you're eliminating a lot of variables.

Note that with POI's OOXML support you'll need to use its streaming mode if you intend to create large spreadsheets (greater than 65k rows).


I don't know of any Scala library for this kind of encapsulation.

Most questions around using apache POI are about iterators.
And the alexcheng project does import an Excel document, and has a test case.
But that's about it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜