Skip to content

Instantly share code, notes, and snippets.

@masanobuimai
Created February 10, 2009 10:20
Show Gist options
  • Save masanobuimai/61337 to your computer and use it in GitHub Desktop.
Save masanobuimai/61337 to your computer and use it in GitHub Desktop.
GrailsでExcelダウンロード
import groovy.xml.MarkupBuilder
class ExcelController {
def type1 = {
render(contentType: "application/vnd.ms-excel") {
html {
body {
h1 "Books"
table(border:1) {
tr { th "a"; th "b" }
// 日本語は, あああ のようにエスケープされる。
tr { td "あああ"; td "aaa" }
// MarkupBuilderを使っているワケではないので,yieldUnescaped()は使えない。
tr { td { yieldUnescaped "<b>いいい</b><br>ううう\nえええ" }; td "bbb" }
tr { td "おおお"; td "ccc" }
}
}
}
}
}
def type2 = {
def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
builder.html {
body {
h1 "Books"
table(border:1) {
tr { th "a"; th "b" }
tr { td "あああ"; td "aaa" }
tr { td { yieldUnescaped "<b>いいい</b><br>ううう\nえええ" }; td "bbb" }
tr { td "おおお"; td "ccc" }
}
}
}
// render()で明示的にエンコードを Shift_JIS に指定する。
render(contentType: "application/vnd.ms-excel", text:writer, encoding:'Shift_JIS')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment