如何解决Scala代码中的以下错误?

时间:2019-02-09 10:31:34

标签: scala

Error:(36, 22) value CELL_TYPE_NUMERIC is not a member of object org.apache.poi.ss.usermodel.Cell
           case Cell.CELL_TYPE_NUMERIC =>

使用的代码:

import java.io.File
import java.io.FileInputStream
import java.util
import javax.print.DocFlavor.STRING
import org.apache.poi.ss.usermodel.{Cell, CellType, Row}
import org.apache.poi.xssf.usermodel.XSSFRow
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook

object Excel_read_switch_Scala {
  var row: XSSFRow = null    
  @throws[Exception]
  def main(args: Array[String]): Unit = {
    val fis = new FileInputStream(new File("D:\\MyFirstExcel.xlsx"))
    val workbook = new XSSFWorkbook(fis)
    val spreadsheet = workbook.getSheetAt(0)
    val rowIterator = spreadsheet.iterator
    while ( {
      rowIterator.hasNext
    }) {
      row = rowIterator.next.asInstanceOf[XSSFRow]
      val cellIterator = row.cellIterator
      while (cellIterator.hasNext()) {
        val cell = cellIterator.next    
        var myChoice:Any = cell.getCellType
          myChoice match {
           case Cell.CELL_TYPE_NUMERIC =>
             System.out.print(cell.getNumericCellValue + " \t\t ")
           case Cell.CELL_TYPE_STRING =>
             System.out.print(cell.getStringCellValue + " \t\t ")
         }
      }
      System.out.println()
    }
    fis.close()
  }
}

Xlsx输入为:

enter image description here

1 个答案:

答案 0 :(得分:1)

getCellType返回类型为CellType的值,因此您需要CellType.NUMERIC等。这是我用来从单元格获取值的一些代码:

sealed trait ExcelValue {
  def asString: String

  override def toString = asString
}

case class NumericValue(asString: String, value: Double) extends ExcelValue
case class StringValue(asString: String, value: String) extends ExcelValue
case class BooleanValue(asString: String, value: Boolean) extends ExcelValue
case class ErrorValue(asString: String, value: Int) extends ExcelValue
case class FormulaValue(asString: String) extends ExcelValue
case object BlankValue extends ExcelValue { val asString = "" }
case object NoneValue extends ExcelValue { val asString = "" }

object ExcelValue {
  def apply(cell: CellValue): ExcelValue =
    cell.getCellType match {
      case CellType.NUMERIC => NumericValue(cell.formatAsString, cell.getNumberValue)
      case CellType.STRING => StringValue(cell.formatAsString, cell.getStringValue)
      case CellType.BOOLEAN => BooleanValue(cell.formatAsString, cell.getBooleanValue)
      case CellType.ERROR => ErrorValue(cell.formatAsString, cell.getErrorValue)
      case CellType.FORMULA => FormulaValue(cell.formatAsString)
      case CellType.BLANK => BlankValue
      case CellType._NONE => NoneValue
    }
}
相关问题