打印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,内容和表单名称。
希望能帮助到你!
- Java POI删除单元格
- 如何使用POIparsingExcel文件中的UTF-8字符
- apache poi excel小时格式设置单元格值java
- 将图像插入到Excel中时出现错误信息
- Java的Apache POI空白单元格
- 使用Apache POI打开.xlsx文件会导致出现NoClassDefFoundError InvalidFormatExceptionexception
- 写入工作簿时获取java.lang.NullPointerException WorkBook.write(out)Apache POI
- 如何使用java分割Excel文件?
- 如何使用Java HSSF附加到现有的Excel文件