Help us understand the problem. What is going on with this article?

Pray Frameworkで文字コードShift-JISなCSVを出力する

More than 1 year has passed since last update.

ScalaでPray Frameworkで文字コードShift-JISなCSVを出力する

CSVでダウンロードさせたいみたいな案件がある時に有効。

条件

1, CSVライブラリにはPureCSVを使う
https://github.com/melrief/PureCSV
2, Shift-JISに変換するのはPlayの機能で補う

実装

  • purecsvのREADMEに書かれてるとおりに build.sbt を記述
resolvers += Resolver.sonatypeRepo("releases")
libraryDependencies += "com.github.melrief" %% "purecsv" % "0.1.1"
  • Controllerを実装
import play.api.mvc.Codec
import purecsv.safe._

case class CSV(id: String, name: String)
val header = CSV("ID", "氏名")
val csv = header +: Seq(CSV("1", "ヤマダ"), CSV("2", "スズキ"))

implicit val myCustomCharset = Codec.javaSupported("Shift_JIS")
Ok(csv.toCSV()).as("text/csv; charset=shift_jis")

最後の2行で、Shift-JIS指定ができる。Content-Typeも書き換わってくれる。
指定したroutes にアクセスすると、Shift-JISのCSVが取得できる

peutes
Scala/JavaScript/PHP/Python/Erlang/C++/MVC/Java/Go/Perl 他:MySQL/memcached/Redis/emacs/IntelliJ/GCS/Gatling
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした