Apache POI 是否有 Scala 包装器?

发布于 2024-10-18 01:50:00 字数 156 浏览 1 评论 0原文

我想使用 Apache POI 在 Scala 应用程序中读取/创建 Excel 文件。当然,我可以直接使用 POI 库,毕竟它是 Java,但我想要有 Scala 的感觉。那么是否有一个 Scala 包装器带来 Scala 的感觉(使用隐式转换),即某种免费提供的“Scala-POI-DSL”?

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 feel (using implicit conversions), i.e. some kind of "Scala-POI-DSL" freely available?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

错爱 2024-10-25 01:50:00

感谢 Dave Griffith 的回答,我已经破解了类似于他的 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")

代码可以在此处找到。

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.

听不够的曲调 2024-10-25 01:50:00

鉴于 Scala 缺乏先进的开源电子表格包装器,我开始开发 Spoiwo:https://github.com/诺伯特-拉迪克/spoiwo。它允许生成 XSSFWorkbook 并支持 POI 功能的重要子集。

它仍然需要一些文档,但下面应该给出关于其功能的粗略了解:

使用 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")
  }
} 

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 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")
  }
} 
依 靠 2024-10-25 01:50:00

Fancy POI - 似乎找不到太多信息,但我想这就是你正在寻找的为了。

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

清醇 2024-10-25 01:50:00

这完全没有帮助,但我为 POI 编写了一个类似 Scala 的 DSL。它允许像这样的代码

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

可悲的是,如果没有与我的老板核实,我就无法将其发布,但说实话,这并不难做到。您应该能够毫不费力地从该示例中对大部分内容进行逆向工程。

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.

痴者 2024-10-25 01:50:00

如果您正在编写 Office XML,则可以考虑避免使用 POI 并直接创建 XML(并将其一起归档)。对于简单的电子表格来说,做起来非常简单,并且消除了很多变量。

请注意,通过 POI 的 OOXML 支持,如果您打算创建大型电子表格(超过 65k 行),则需要使用其流模式。

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).

口干舌燥 2024-10-25 01:50:00

我不知道有任何 Scala 库可以进行这种封装。

关于使用 apache POI 的大多数问题都是关于迭代器
alex Cheng 项目可以导入 Excel 文档,并且有一个 测试用例
但仅此而已。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文