<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1971057972447315488</id><updated>2012-02-17T04:28:59.816+09:00</updated><category term='JRuby'/><title type='text'>miniレポ</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://minirepo.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1971057972447315488/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://minirepo.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>matsu</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1971057972447315488.post-7852259320146439440</id><published>2011-07-09T15:09:00.002+09:00</published><updated>2011-07-09T16:24:33.178+09:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JRuby'/><title type='text'>POIを使用してJRubyでExcel操作</title><content type='html'>RubyでExcelファイルを生成したいと思い、Spreadsheetを使ってみたのですが、&lt;br /&gt;テンプレートファイルからのコピーがうまく出来なかったため、POIとJRubyでExcelファイルを作成しました。&lt;br /&gt;&lt;pre class="brush:ruby"&gt;include_class "org.apache.poi.poifs.filesystem.POIFSFileSystem"&lt;br /&gt;include_class "org.apache.poi.hssf.usermodel.HSSFWorkbook"&lt;br /&gt;include_class "org.apache.poi.hssf.usermodel.HSSFSheet"&lt;br /&gt;include_class "org.apache.poi.hssf.usermodel.HSSFRow"&lt;br /&gt;include_class "org.apache.poi.hssf.usermodel.HSSFCell"&lt;br /&gt;include_class "org.apache.poi.hssf.usermodel.HSSFDateUtil"&lt;br /&gt;include_class "org.apache.poi.ss.util.CellRangeAddress"&lt;br /&gt;include_class "org.jruby.util.IOInputStream"&lt;br /&gt;&lt;br /&gt;def make_excel&lt;br /&gt;  filename = "template.xls"&lt;br /&gt;  ist = java.io.FileInputStream.new filename&lt;br /&gt;  template = HSSFWorkbook.new ist&lt;br /&gt;  @newSheet = template.createSheet&lt;br /&gt;   template.setSheetName(1, "シート名")&lt;br /&gt;  # シートの倍率設定&lt;br /&gt;  @newSheet.setZoom(85, 100)&lt;br /&gt;  # セルの結合数&lt;br /&gt;  unionCellCount = @tempsheet.getNumMergedRegions()&lt;br /&gt;  # 行のコピー&lt;br /&gt;  rowCopy(0, 4, unionCellCount, 0)&lt;br /&gt;  #値入力&lt;br /&gt;  setCellValue(@newSheet.getRow(1), 1, 10)&lt;br /&gt;  # 印刷倍率設定&lt;br /&gt;  ps = @newSheet.getPrintSetup()&lt;br /&gt;  ps.setScale(58)&lt;br /&gt;  template.removeSheetAt(0)&lt;br /&gt;  newFileName = "ファイル名.xls"&lt;br /&gt;  io = java.io.FileOutputStream.new(newFileName)&lt;br /&gt;  template.write(io)&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;シートの倍率設定など、基本的な操作は簡単に出来ます。&lt;br /&gt;ただ、テンプレートからのコピーになると、若干めんどくさくなります。&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:ruby"&gt;# セルに値を設定&lt;br /&gt;def setCellValue(targetRow, cellNum, content)&lt;br /&gt;  targetCell = targetRow.getCell(cellNum)&lt;br /&gt;  targetCell.setCellValue(content)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# セルの書式を設定&lt;br /&gt;def setCellFormula(targetRow, cellNum, content)&lt;br /&gt;  targetCell = targetRow.getCell(cellNum)&lt;br /&gt;  targetCell.setCellFormula(content)&lt;br /&gt;end&lt;br /&gt;    &lt;br /&gt;# 指定範囲の行コピー&lt;br /&gt;def rowCopy(startCount, endCount, unionCount, cnt)&lt;br /&gt;  for i in startCount .. endCount&lt;br /&gt;    # テンプレートの行情報取得&lt;br /&gt;    copyRow = @tempsheet.getRow(i)&lt;br /&gt;    # 新しい行を追加&lt;br /&gt;    newRow = @newSheet.createRow(i + cnt)&lt;br /&gt;    length = copyRow.getLastCellNum()&lt;br /&gt;    copyHeight = copyRow.getHeight()&lt;br /&gt;    newRow.setHeight(copyHeight)&lt;br /&gt;    for j in 0 .. length&lt;br /&gt;      copyCell = copyRow.getCell(j)&lt;br /&gt;      if copyCell != nil&lt;br /&gt;        newCell = newRow.createCell(j)&lt;br /&gt;        copyWidth = @tempsheet.getColumnWidth(j)&lt;br /&gt;        @newSheet.setColumnWidth(j, copyWidth)&lt;br /&gt;        copyCellStyle = copyCell.getCellStyle()&lt;br /&gt;        newCell.setCellStyle(copyCellStyle)&lt;br /&gt;        # セルの書式コピー&lt;br /&gt;        case copyCell.getCellType()&lt;br /&gt;        when HSSFCell::CELL_TYPE_STRING&lt;br /&gt;          newCell.setCellValue(copyCell.getRichStringCellValue())&lt;br /&gt;        when HSSFCell::CELL_TYPE_NUMERIC&lt;br /&gt;          if HSSFDateUtil::isCellDateFormatted(copyCell) then&lt;br /&gt;             newCell.setCellValue(copyCell.getDateCellValue())&lt;br /&gt;        else&lt;br /&gt;          newCell.setCellValue(copyCell.getNumericCellValue())&lt;br /&gt;        end&lt;br /&gt;        when HSSFCell::CELL_TYPE_FORMULA&lt;br /&gt;          newCell.setCellFormula(copyCell.getCellFormula())&lt;br /&gt;        when HSSFCell::CELL_TYPE_BOOLEAN&lt;br /&gt;          newCell.setCellValue(copyCell.getBooleanCellValue())&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  cra = nil&lt;br /&gt;  # セルの結合設定のコピー&lt;br /&gt;  for i in 0 .. unionCount - 1&lt;br /&gt;    cra = @tempsheet.getMergedRegion(i)&lt;br /&gt;    if cra.getFirstRow() &gt;= startCount &amp;&amp; cra.getFirstRow() &lt;= endCount then&lt;br /&gt;      cra.setFirstRow(cra.getFirstRow() + cnt)&lt;br /&gt;      cra.setLastRow(cra.getLastRow() + cnt)&lt;br /&gt;      @newSheet.addMergedRegion(cra);&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;行をコピーする際には、1行、1セルずつ、値、書式をコピーしています。&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1971057972447315488-7852259320146439440?l=minirepo.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1971057972447315488/posts/default/7852259320146439440'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1971057972447315488/posts/default/7852259320146439440'/><link rel='alternate' type='text/html' href='http://minirepo.blogspot.com/2011/07/mac-os-x-poijrubyexcel.html' title='POIを使用してJRubyでExcel操作'/><author><name>matsu</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>
