Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.76 KB | None | 0 0
  1. package com.oak.excel
  2. import java.io.{File, FileInputStream}
  3. import org.apache.poi.ss.usermodel.{Cell, Sheet, Workbook}
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook
  5.  
  6. case class MpdLink(mpdTask: String, ammTask: String,descr: String, accDescr:Option[String],prepHH: Double, taskHH: Double)
  7.  
  8. class MpdReader {
  9.     def read(mpd: String) {
  10.       val inputStream: FileInputStream = new FileInputStream(new File(mpd))
  11.       val workbook: Workbook = new XSSFWorkbook(inputStream)
  12.       val firstSheet:Sheet = workbook.getSheetAt(1)
  13.       val iterator = firstSheet.rowIterator()
  14.       val dump = scala.collection.mutable.ArrayBuffer[MpdLink]()
  15.       while(iterator.hasNext){
  16.         val row = iterator.next
  17.         val prepHH = setType( row.getCell(12) )
  18.         val taskHH = setType( row.getCell(13) )
  19.         val descr = setType( row.getCell(6) )
  20.         if( isDouble( taskHH ) && isDouble( prepHH ) )
  21.         dump.+=(MpdLink(
  22.           setType( row.getCell(1) ),
  23.           setType( row.getCell(9) ),
  24.           descr,
  25.           accessRemark(descr),
  26.           prepHH.toDouble,
  27.           taskHH.toDouble
  28.         ))
  29.       }
  30.       dump.foreach(m=> println(m.accDescr))
  31.       workbook.close()
  32.       inputStream.close()
  33.     }
  34.  
  35.     def setType(cell: Cell): String =
  36.       if( cell != null ) cell.getCellType match {
  37.         case Cell.CELL_TYPE_STRING => cell.getStringCellValue
  38.         case Cell.CELL_TYPE_NUMERIC => cell.getNumericCellValue.toString
  39.         case Cell.CELL_TYPE_BLANK => ""
  40.         case _ => ""
  41.       } else ""
  42.  
  43.   def isDouble(cell: String): Boolean = cell.matches("[0-9.]+")
  44.  
  45.   def accessRemark(descr: String): Option[String] = {
  46.     val rx = "ДОСТУП ПРИМЕЧАНИЕ:(.*?)$".r
  47.     for(m <- rx.findFirstIn(descr)) yield m
  48.   }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement