打印Excel文件的单元格值

我正尝试在Apache POI DSL上使用自定义DSL加载和读取Excel文件。

我想提供Excel支持火花和我使用Apache POI的目的。 我在Scala中find了上面的DSL存储库。 我正试图find一种方法来读取单元格并使用Scala来打印它们。

object XlsLoader{ def f1(): Unit ={ val path = "/Users/shashi/data311.xlsx" val res = Workbook(path) val res1 = res.getOrElse(null) println(res1) println("one") val q = res1.map { x => println("hello") println(x) println("sheetmap -- "+x.sheetMap) println("flatten -- "+x.sheetMap.toList) println("keys -- "+x.sheetMap.keys.toList) println("1he") x.sheetMap.keys.toList.foreach(n => println(n)) println("2he") println("toString -- "+x.toString()) } println("two") println(q) } } 

这是输出。

 scalaz.effect.IO$$anon$7@1a8e246b one two scalaz.effect.IO$$anon$7@34ccc2af 

我想find工作表的内部结构并打印出来。 我该怎么做 ?

这是供您参考的excel文件。

 c1 c2 1 100 2 200 3 300 4 400 5 500 

所以我正在使用以下依赖项:

 "info.folone" %% "poi-scala" % "0.18" 

现在代码中唯一缺less的是调用.run.unsafePerformIO (或者更安全的替代方法)。

我还创build了一个小样本,将逐步解释它,并提供一个给定的.xls文件的一般结构控制台,希望它是有用的:

 import java.io.InputStream import info.folone.scala.poi._ import scalaz.{-\/, \/-} object ReadExcelFile { def main(args: Array[String]): Unit = { val readResult = Workbook(xlsFile) .map(workbook => workbook.sheets) .run .unsafePerformIO() readResult match { case -\/(exception) => throw new RuntimeException("Could not read file", exception) case \/-(sheets) => sheets.foreach(printSheet) } } def printSheet(sheet: Sheet): Unit = { println(s"------------ ${sheet.name} ------------\n") sheet.rows.foreach(printRow) } def printRow(row: Row): Unit = println(row.cells.toList.sortBy(_.index).mkString(", ") + "\n") def xlsFile: InputStream = ReadExcelFile.getClass.getResourceAsStream("/test.xls") } 

输出将是这样的:

 ------------ Sample-spreadsheet-file ------------ FormulaCell(0, "=A1+1"), StringCell(1, "1.7 Cubic Foot Compact "Cube" Office Refrigerators"), StringCell(2, "Barry French"), NumericCell(3, 293.0), NumericCell(4, 457.81), NumericCell(5, 208.16), NumericCell(6, 68.02), StringCell(7, "Nunavut"), StringCell(8, "Appliances"), NumericCell(9, 0.58) 

正如你所看到的,它会打印单元格types,内容和表单名称。

希望能帮助到你!