Changeset 7818 for lang/scala/sandbox

Show
Ignore:
Timestamp:
03/12/08 01:46:20 (9 months ago)
Author:
ryugate
Message:

apache/POI.scala:

MyRow#row?:存在しなければ作って返すようにした。
setCell系を整備(Sheet,Row,Col)

Location:
lang/scala/sandbox/src/jp/ryugate/apache
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/scala/sandbox/src/jp/ryugate/apache/POI.scala

    r7772 r7818  
    33/** 
    44* POI wrapper 
    5 * 2008/2/18 
     5* 2008/3/12 
    66*/ 
    77object POI { 
     
    2525  */ 
    2626  class MyBook(hssf_book:HSSFWorkbook) { 
    27     def sheet(index:int  ) = hssf_book.getSheetAt(index-1) 
     27    def sheet(index:int  ) = hssf_book.getSheetAt(index-1)                      // index start from 1 
    2828    def sheet(name:String) = hssf_book.getSheet(name) match { 
    2929      case null => throw new NoSuchElementException(name + "(Sheet not found)") 
     
    5151  */ 
    5252  class MySheet(hssf_sheet:HSSFSheet) { 
    53     def row(ri:int) = hssf_sheet.getRow(ri-1) 
     53    def row(ri:Int) = hssf_sheet.getRow(ri-1) match {                           // index start from 1 
     54      case null => hssf_sheet.createRow(ri-1) 
     55      case row => row  
     56    } 
    5457    def col(ci:int) = new Col(this, ci) 
    5558 
     
    7780    def each[V](f:HSSFRow => V) = eachRows(0 to getLastRowNum)(f) 
    7881     
     82    def getLastRowNum = hssf_sheet.getLastRowNum 
     83 
    7984    //-------------------------------------------- 
    80     def createRow(rownum:int) = hssf_sheet.createRow(rownum-1) 
    81      
    82     def getLastRowNum = hssf_sheet.getLastRowNum 
     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) 
    8387  } 
    8488  implicit def HSSFSheetToExtSheet(sheet:HSSFSheet) = new MySheet(sheet) 
     
    8892  */ 
    8993  class MyRow(hssf_row:HSSFRow) { 
    90     def get(cellnum:int) = hssf_row match { 
     94    def get(cellnum:int) = hssf_row.getCell((cellnum-1).toShort) match {        // index start from 1 
    9195      case null => None 
    92       case _ => hssf_row.getCell((cellnum-1).toShort) match { 
    93         case null => None 
    94         case v    => new Some(v) 
    95       } 
     96      case v    => new Some(v) 
    9697    } 
    9798 
     
    117118 
    118119    //-------------------------------------------- 
    119     def createCell(cellnum:int) = hssf_row.createCell(cellnum.toShort) 
    120  
    121     def setCell(cellnum:int, value:String, encoding:Short):HSSFCell = { 
    122       val cell = hssf_row.createCell(cellnum.toShort) 
     120    def setCell(ci:int, value:String, encoding:Short):HSSFCell = {              // index start from 1 
     121      val cell = hssf_row.createCell((ci-1).toShort) 
    123122      cell.setEncoding(encoding) 
    124123      cell.setCellValue(value) 
     
    152151       
    153152    def each[V](f:HSSFCell => V) = eachCells(0 to sheet.getLastRowNum)(f) 
     153 
     154    //-------------------------------------------- 
     155    def setCell(cellnum:int, value:String, encoding:Short):HSSFCell = sheet.row(cellnum).setCell(colIdx, value, encoding) 
     156    def setCell(cellnum:int, value:String):HSSFCell = sheet.row(cellnum).setCell(colIdx, value) 
    154157  } 
    155158   
  • lang/scala/sandbox/src/jp/ryugate/apache/POISpecification.scala

    r7772 r7818  
    1111  import org.apache.poi.hssf.usermodel.HSSFCell 
    1212  import org.apache.poi.hssf.usermodel.HSSFCell._ 
     13  import org.apache.poi.hssf.usermodel.HSSFCellStyle 
    1314   
    1415  import java.lang._ 
    1516  import java.io.FileInputStream 
     17  import java.io.FileOutputStream 
    1618   
    1719  "POI module" should { 
     
    5052      sheet1.cell(100,100) must throwException(new NoSuchElementException) 
    5153 
    52       sheet1.row(100) must beNull 
     54      sheet1.row(100) must notBeNull 
    5355      sheet1.col(100) must notBeNull 
    5456 
     
    6567    "create and get Sheet" in { 
    6668      val bk = new HSSFWorkbook 
    67       bk.createSheet("test") 
     69      val style = bk.createCellStyle 
     70      style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM) 
     71 
     72      val sheet = bk.createSheet("test") 
    6873      bk.sheet(1) must notBeNull 
    69       bk.sheet("hoge") must throwException(new NoSuchElementException) 
     74      sheet.row(1).setCell(1,"hoge").setCellStyle(style)  
     75      sheet.col(2).setCell(2,"piyo").setCellStyle(style) 
     76      sheet.setCell(3,3,"HOGE").setCellStyle(style) 
     77 
     78      bk.write(new FileOutputStream("testdata/testoutput.xls")) 
     79       
    7080    } 
    7181