尝试从excel读取标头时出现NoSuchElementException

时间:2015-09-06 13:42:43

标签: scala filereader import-from-excel

我有一个类为我解析xsl文件并用xsl的头部映射它。

我有另一个类,它是xsl每行的对象,并使用标题来知道哪个单元格通过我之前映射的标题获取哪个属性...

由于某些原因我在其中一个标题上出现错误NoSuchElementException,并且没有拼写错误...它之前有效,我现在不知道什么是错的。

这是控制xsl:

的DataSource.scala类(它的特性)
import java.io.File

import com.github.tototoshi.csv.CSVReader
import jxl.{Cell, Workbook}

import scala.collection.mutable

trait DataSource {

  def read (fileName: String): Seq[Map[String, String]]
}



object CsvDataSource extends DataSource {
  import com.github.tototoshi.csv.CSVFormat
  import com.github.tototoshi.csv.Quoting
  import com.github.tototoshi.csv.QUOTE_MINIMAL

  implicit object VATBoxFormat extends CSVFormat {
    val delimiter: Char = '\t'
    val quoteChar: Char = '"'
    val escapeChar: Char = '"'
    val lineTerminator: String = "\r\n"
    val quoting: Quoting = QUOTE_MINIMAL
    val treatEmptyLineAsNil: Boolean = false
  }

  override def read(file: String): Seq[Map[String, String]] = {
    val reader = CSVReader.open(file, "UTF-16")(VATBoxFormat)
    reader.readNext()
    val country = reader.readNext().get(5)
    reader.readNext()
    reader.iteratorWithHeaders.toSeq.map(c => c + ("country" -> country))
  }
}

object ExecDataSource extends DataSource {
  override def read(file: String): Seq[Map[String, String]] = {
    val workbook = Workbook.getWorkbook(new File(file))
    val sheet = workbook.getSheet(0)

    val rowsUsed: Int = sheet.getRows

    val headers = sheet.getRow(3).toList
    //    println(headers.map(_.getContents))
    val country = sheet.getCell(5, 1).getContents

    (4 until rowsUsed).map { i =>
      val c = headers.zip(sheet.getRow(i)).map{case (k,v) => (k.getContents, v.getContents)}.toMap
      c + ("country" -> country)
    }
  }
}

这是PurchaseInfo类,它正在创建excel的每一行的对象:

case class PurchaseInfo(
                         something1: String,
                         something2: String,
                         something3: String,
                         something4: String) {
}


object PurchaseInfo {

    private def changeDateFormat(dateInString: String): String = {
    //System.out.println(dateInString)
    val formatter: SimpleDateFormat = new SimpleDateFormat("MMM dd, yyyy")
    val formatter2: SimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy")
    val date: Date = formatter.parse(dateInString)
    return formatter2.format(date).toString
  }

    def fromDataSource (ds: DataSource)(fileName: String): Seq[PurchaseInfo] = {

      ds.read(fileName).map { c =>
        PurchaseInfo(
          something1 = c("Supplier Address Street Number"),
          something2 = c("Supplier Address Route"),
          something3 = c("Supplier Address Locality"),
          something4 = c("Supplier Address Postal Code")
        )
      }
    }
}

(iv在buyInfo中删除了一些var以使问题更短)

现在,这是我在运行代码时遇到的错误(来自运行我的操作的diff类,这是一个我使用selenium的自动化项目)

Exception in thread "main" java.util.NoSuchElementException: key not found: Supplier Address Street Number
    at scala.collection.MapLike$class.default(MapLike.scala:228)
    at scala.collection.AbstractMap.default(Map.scala:59)
    at scala.collection.MapLike$class.apply(MapLike.scala:141)
    at scala.collection.AbstractMap.apply(Map.scala:59)
    at PurchaseInfo$$anonfun$fromDataSource$1.apply(PurchaseInfo.scala:50)
    at PurchaseInfo$$anonfun$fromDataSource$1.apply(PurchaseInfo.scala:48)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
    at scala.collection.Iterator$class.foreach(Iterator.scala:742)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1194)
    at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
    at scala.collection.AbstractTraversable.map(Traversable.scala:104)
    at PurchaseInfo$.fromDataSource(PurchaseInfo.scala:48)
    at HolandPortal$.main(HolandPortal.scala:22)
    at HolandPortal.main(HolandPortal.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 1

有人能看到这个问题......?我不知道为什么他找不到"供应商地址街道号码",在xsl我有这个标题完全一样:/

感谢

0 个答案:

没有答案