Changeset 8079 for lang/scala/sandbox
- Timestamp:
- 03/18/08 18:29:01 (9 months ago)
- Location:
- lang/scala/sandbox/src/jp/ryugate/apache
- Files:
-
- 2 modified
-
POI.scala (modified) (13 diffs)
-
POISpecification.scala (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/scala/sandbox/src/jp/ryugate/apache/POI.scala
r7820 r8079 3 3 /** 4 4 * POI wrapper 5 * 2008/3/1 25 * 2008/3/17 6 6 */ 7 7 object POI { 8 8 9 import scala.Math 9 10 import java.io.InputStream 10 11 11 import org.apache.poi.hssf.usermodel.HSSFWorkbook 12 import org.apache.poi.hssf.usermodel.HSSFSheet 13 import org.apache.poi.hssf.usermodel.HSSFRow 14 import org.apache.poi.hssf.usermodel.HSSFCell 12 import org.apache.poi.hssf.usermodel.{HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell, HSSFRichTextString} 15 13 import org.apache.poi.hssf.usermodel.HSSFCell._ 16 14 17 private def makeSomeList[ T](f:int => T, n:int*)18 = n.foldLeft(List[ T]()){(acc, i) => f(i) :: acc}.reverse19 15 private def makeSomeList[A,B](f:A => B, n:A*) 16 = n.foldLeft(List[B]()){(acc, i) => f(i) :: acc}.reverse 17 20 18 private def eachWithIndex[T,V](lst:List[T], f2:(T,int) => V) 21 19 = lst.zipWithIndex.foreach{case (row,idx) => f2(row,idx)} 22 20 21 private def stoi(str:String, conv:Char=>Int, radix:Int) = { 22 def pow(n:Int,i:Int):Int = i match { 23 case 0 => 1 24 case _ => n * pow(n, i-1) 25 } 26 27 str.toArray.reverse.zipWithIndex.foldLeft(0) { 28 case (acc, (chr,digit)) => acc + conv(chr) * pow(radix,digit) 29 } 30 } 31 32 private def colstrToInt(str:String) = stoi(str.replaceAll("[0-9]", ""), (c:Char)=>c-'A'+1, 26) 33 private def rowstrToInt(str:String) = str.replaceAll("[A-Z]", "").toInt 34 23 35 /** 24 36 * Book 25 37 */ 26 38 class MyBook(hssf_book:HSSFWorkbook) { 27 def sheet(index: int ) = hssf_book.getSheetAt(index-1)// index start from 128 def sheet(name:String) = hssf_book.getSheet(name) match {39 def sheet(index:Int ):HSSFSheet = hssf_book.getSheetAt(index-1) // index start from 1 40 def sheet(name:String):HSSFSheet = hssf_book.getSheet(name) match { 29 41 case null => throw new NoSuchElementException(name + "(Sheet not found)") 30 42 case v => v … … 34 46 def withSheet[V](name:String)(f:HSSFSheet => V) = f(sheet(name)) 35 47 36 def getSheets(idxs: int* ) = makeSomeList(sheet, idxs:_*)37 def getSheets(range:Range ) = makeSomeList(sheet, range:_*)48 def getSheets(idxs:Int* ) = makeSomeList[Int,HSSFSheet](sheet, idxs:_*) 49 def getSheets(range:Range ) = makeSomeList[Int,HSSFSheet](sheet, range:_*) 38 50 39 51 def eachSheets[V](idxs:int* )(f:HSSFSheet => V) = getSheets(idxs:_* ).foreach{f(_)} 40 52 def eachSheets[V](range:Range)(f:HSSFSheet => V) = getSheets(range:_*).foreach{f(_)} 53 54 def getSheetsByName(idxs:String*) = makeSomeList[String,HSSFSheet](sheet, idxs:_*) 55 def eachSheetsByName[V](idxs:String*)(f:HSSFSheet => V) = getSheetsByName(idxs:_* ).foreach{f(_)} 41 56 42 57 def eachSheetsWithIndex[V](idxs:int* )(f:(HSSFSheet,int) => V) = eachWithIndex(getSheets(idxs:_*), f) … … 55 70 case row => row 56 71 } 57 def col(ci:int) = new Col(this, ci) 58 59 def getCell(ri:int,ci:int) = row(ri).get(ci) 60 def cell(ri:int,ci:int) = row(ri).apply(ci) 61 72 def col(ci:int ):Col = new Col(this, ci) 73 def col(cis:String):Col = col(colstrToInt(cis)) 74 75 def getCell(ci:int,ri:int ):Option[HSSFCell] = row(ri).get(ci) 76 def getCell(cellstr:String):Option[HSSFCell] = getCell(colstrToInt(cellstr), rowstrToInt(cellstr)) 77 78 def cell(ci:int,ri:int ):HSSFCell = row(ri).apply(ci) 79 def cell(cellstr:String):HSSFCell = cell(colstrToInt(cellstr), rowstrToInt(cellstr)) 80 81 def cells(cellstrs:String*) = makeSomeList(cell, cellstrs:_*) 82 83 def applyOrCreateCell(ci:int,ri:int ):HSSFCell = row(ri).applyOrCreate(ci) 84 def applyOrCreateCell(cellstr:String):HSSFCell = applyOrCreateCell(colstrToInt(cellstr), rowstrToInt(cellstr)) 85 62 86 def withRow[V](ri:int)(f:HSSFRow => V) = f(row(ri)) 63 def withCol[V](ci:int)(f:Col => V) = f(col(ci))64 65 def rows(rowIdxs:int*) = makeSomeList(row, rowIdxs:_*)66 def cols(colIdxs:int*) = makeSomeList(col , colIdxs:_*)67 def rows(range:Range ) = makeSomeList(row, range:_*)68 def cols(range:Range ) = makeSomeList(col , range:_*)87 def withCol[V](ci:int)(f:Col => V) = f(col(ci)) 88 89 def rows(rowIdxs:int*) = makeSomeList(row, rowIdxs:_*) 90 def cols(colIdxs:int*) = makeSomeList(col:int=>Col, colIdxs:_*) 91 def rows(range:Range ) = makeSomeList(row, range:_*) 92 def cols(range:Range ) = makeSomeList(col:int=>Col, range:_*) 69 93 70 94 def eachRows[V](rowIdxs:int*)(f:HSSFRow => V) = rows(rowIdxs:_*).foreach{f(_)} 71 def eachCols[V](colIdxs:int*)(f:Col => V) = cols(colIdxs:_*).foreach{f(_)}95 def eachCols[V](colIdxs:int*)(f:Col => V) = cols(colIdxs:_*).foreach{f(_)} 72 96 def eachRows[V](range:Range )(f:HSSFRow => V) = rows(range ).foreach{f(_)} 73 def eachCols[V](range:Range )(f:Col => V) = cols(range ).foreach{f(_)}97 def eachCols[V](range:Range )(f:Col => V) = cols(range ).foreach{f(_)} 74 98 75 99 def eachRowsWithIndex[V](idxs:int* )(f:(HSSFRow,int) => V) = eachWithIndex(rows(idxs:_*), f) 76 def eachColsWithIndex[V](idxs:int* )(f:(Col, int) => V) = eachWithIndex(cols(idxs:_*), f)100 def eachColsWithIndex[V](idxs:int* )(f:(Col, int) => V) = eachWithIndex(cols(idxs:_*), f) 77 101 def eachRowsWithIndex[V](range:Range)(f:(HSSFRow,int) => V) = eachWithIndex(rows(range:_*), f) 78 def eachColsWithIndex[V](range:Range)(f:(Col, int) => V) = eachWithIndex(cols(range:_*), f)102 def eachColsWithIndex[V](range:Range)(f:(Col, int) => V) = eachWithIndex(cols(range:_*), f) 79 103 80 104 def each[V](f:HSSFRow => V) = eachRows(0 to getLastRowNum)(f) … … 83 107 84 108 //-------------------------------------------- 85 def setCell(ri:Int, ci:Int, value:String, encoding:Short):HSSFCell = row(ri).setCell(ci, value, encoding) 86 def setCell(ri:Int, ci:Int, value:String):HSSFCell = row(ri).setCell(ci, value) 109 def setCell(ri:Int, ci:Int, value:Any, encoding:Short):HSSFCell = row(ri).setCell(ci, value, encoding) 110 def setCell(ri:Int, ci:Int, value:Any ):HSSFCell = row(ri).setCell(ci, value) 111 def setCell(cellstr:String, value:Any ):HSSFCell = row(rowstrToInt(cellstr)).setCell(colstrToInt(cellstr), value) 87 112 } 88 113 implicit def HSSFSheetToExtSheet(sheet:HSSFSheet) = new MySheet(sheet) … … 97 122 } 98 123 99 def getOrCreate(ci:int) = get(ci) match { // index start from 1100 case None => hssf_row.createCell((ci-1).toShort)101 case Some(v) => v102 }103 104 124 def apply(cellnum:int) = get(cellnum) match { 105 125 case None => throw new NoSuchElementException(cellnum + " (Cell not found)") … … 108 128 def col(cellnum:int) = apply(cellnum) 109 129 def cell(cellnum:int) = apply(cellnum) 130 131 def applyOrCreate(ci:int) = get(ci) match { // index start from 1 132 case None => hssf_row.createCell((ci-1).toShort) 133 case Some(v) => v 134 } 110 135 111 136 def getCells(idxs:int* ) = makeSomeList(get, idxs:_*) … … 113 138 def cells(idxs:int* ) = makeSomeList(apply, idxs:_*) 114 139 def cells(range:Range) = makeSomeList(apply, range:_*) 140 141 def applyOrCreateCells(idxs:Int* ) = makeSomeList(applyOrCreate, idxs:_*) 142 def applyOrCreateCells(range:Range) = makeSomeList(applyOrCreate, range:_*) 115 143 116 144 def eachCells[V](idxs:int* )(f:HSSFCell => V) = cells(idxs:_* ).foreach{f(_)} … … 123 151 124 152 //-------------------------------------------- 125 def setCell(ci:int, value:String, encoding:Short):HSSFCell = { 126 val cell = getOrCreate(ci) 127 cell.setEncoding(encoding) 128 cell.setCellValue(value) 129 cell 130 } 131 132 def setCell(cellnum:int, value:String):HSSFCell = setCell(cellnum, value, ENCODING_UNCHANGED) 153 def setCell(ci:int, value:Any, encoding:Short):HSSFCell = applyOrCreate(ci).set(value, encoding) 154 def setCell(ci:int, value:Any ):HSSFCell = setCell(ci, value, ENCODING_UNCHANGED) 133 155 } 134 156 implicit def HSSFRowToExtRow(row:HSSFRow) = new MyRow(row) … … 143 165 def row(ri:int) = apply(ri) 144 166 def cell(ri:int) = apply(ri) 167 168 def applyOrCreate(ri:int) = sheet.row(ri).applyOrCreate(colIdx) 145 169 146 170 def getCells(idxs:int* ) = makeSomeList(get, idxs:_*) … … 148 172 def cells(idxs:int* ) = makeSomeList(apply, idxs:_*) 149 173 def cells(range:Range) = makeSomeList(apply, range:_*) 174 175 def applyOrCreateCells(idxs:Int* ) = makeSomeList(applyOrCreate, idxs:_*) 176 def applyOrCreateCells(range:Range) = makeSomeList(applyOrCreate, range:_*) 150 177 151 178 def eachCells[V](idxs:int* )(f:HSSFCell => V) = cells(idxs:_* ).foreach{f(_)} … … 158 185 159 186 //-------------------------------------------- 160 def setCell( cellnum:int, value:String, encoding:Short):HSSFCell = sheet.row(cellnum).setCell(colIdx, value, encoding)161 def setCell( cellnum:int, value:String):HSSFCell = sheet.row(cellnum).setCell(colIdx, value)187 def setCell(ri:int, value:Any, encoding:Short):HSSFCell = sheet.row(ri).setCell(colIdx, value, encoding) 188 def setCell(ri:int, value:Any ):HSSFCell = sheet.row(ri).setCell(colIdx, value) 162 189 } 163 190 … … 166 193 */ 167 194 class MyCell(hssf_cell:HSSFCell) { 168 //def get = apply match {169 //case null => None170 //case v => new Some(v)171 //}195 def get = apply match { 196 case null => None 197 case v => new Some(v) 198 } 172 199 173 200 def apply = hssf_cell.getCellType match { … … 179 206 case _ => hssf_cell.toString 180 207 } 181 182 208 def value = apply 209 210 def set(value:Any, encoding:Short):HSSFCell = { 211 import java.util.{Calendar,Date} 212 213 hssf_cell.setEncoding(encoding) 214 value match { 215 case v:boolean => hssf_cell.setCellValue(v) 216 case v:Calendar => hssf_cell.setCellValue(v) 217 case v:Date => hssf_cell.setCellValue(v) 218 case v:double => hssf_cell.setCellValue(v) 219 case v:Int => hssf_cell.setCellValue(v.toDouble) 220 case v:String => hssf_cell.setCellValue(new HSSFRichTextString(v)) 221 } 222 hssf_cell 223 } 224 def set(value:Any):HSSFCell = set(value, ENCODING_UNCHANGED) 225 226 def setFormula(value:String) = { 227 hssf_cell.setCellFormula(value) 228 hssf_cell 229 } 183 230 } 184 231 implicit def HSSFCellToExtCell(cell:HSSFCell) = new MyCell(cell) -
lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala
r7820 r8079 7 7 import jp.ryugate.apache.POI._ 8 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook 9 import org.apache.poi.hssf.usermodel.HSSFSheet10 import org.apache.poi.hssf.usermodel.HSSFRow11 import org.apache.poi.hssf.usermodel.HSSFCell12 import org.apache.poi.hssf.usermodel.HSSFCell._13 9 import org.apache.poi.hssf.usermodel.HSSFCellStyle 10 // import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator 14 11 15 12 import java.lang._ … … 41 38 42 39 book.eachSheets(1 to 3) {sheet => sheet must notBeNull} 40 book.eachSheetsByName("Sheet1","Sheet3") {sheet => sheet must notBeNull} 43 41 book.eachSheets(1,10) {sheet => ; } must throwException(new IndexOutOfBoundsException) 44 42 … … 60 58 61 59 "get cell" in { 62 sheet1.cell(1,1).value must is_==(1.0)63 sheet1.col(1).row(1).value must is_==(1.0)64 60 sheet1.row(1).col(1).value must is_==(1.0) 61 sheet1.cell("B1").value must is_==(2.0) 62 sheet1.cell(2,1).value must is_==(2.0) 63 sheet1.col(3).row(1).value must is_==(3.0) 64 65 sheet1.row(1).col(1).get must is_==(Some(1.0)) 66 sheet1.cell("B1").get must is_==(Some(2.0)) 67 sheet1.cell(2,1).get must is_==(Some(2.0)) 68 sheet1.col(3).row(1).get must is_==(Some(3.0)) 69 70 sheet1.cells("A1","C1") 65 71 } 66 72 … … 72 78 val sheet = bk.createSheet("test") 73 79 bk.sheet(1) must notBeNull 74 sheet.row(1).setCell(1,"hoge").setCellStyle(style) 75 sheet.col(1).setCell(1,"piyo") 76 sheet.setCell(3,3,"HOGE").setCellStyle(style) 80 sheet.row(1).setCell(1, "hoge").setCellStyle(style) 81 sheet.col(1).setCell(1, "piyo") 82 sheet.setCell(3,3, "HOGE").setCellStyle(style) 83 sheet.setCell("A5", 1) 84 sheet.setCell("B5", 2) 85 sheet.applyOrCreateCell(5,3).setFormula("A5+B5") 86 sheet.row(5).col(2).value must is_==(2.0) 87 88 sheet.row(10).applyOrCreateCells(1 to 10).foreach {cell => 89 cell.setCellStyle(style) 90 } 91 92 // val ev = new HSSFFormulaEvaluator(sheet, bk) 93 // ev.evaluate(sheet.col(3).row(5)).getNumberValue must is_==(3.0) 77 94 78 95 bk.write(new FileOutputStream("testdata/testoutput.xls"))
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)