{"id":1896,"date":"2021-10-23T00:40:11","date_gmt":"2021-10-22T15:40:11","guid":{"rendered":"https:\/\/www.itchefblog.com\/?p=1896"},"modified":"2021-10-23T00:49:25","modified_gmt":"2021-10-22T15:49:25","slug":"spring-boot%ec%97%90%ec%84%9c-csv-%ec%b6%9c%eb%a0%a5","status":"publish","type":"post","link":"https:\/\/www.itchefblog.com\/?p=1896","title":{"rendered":"Spring Boot\uc5d0\uc11c CSV \ucd9c\ub825 \uae30\ub2a5 \ub9cc\ub4e4\uae30"},"content":{"rendered":"\n<p>\ud14c\uc774\ube14\uc758 \ub370\uc774\ud130\ub97c CSV\ub85c \ucd9c\ub825\ud560 \uc77c\uc774 \uc0dd\uaca8\uc11c \uc774\ub9ac\uc800\ub9ac \uac80\uc0c9\ud558\ub2e4 \uc54c\uac8c\ub41c \ub0b4\uc6a9\uc744 \uac04\ub2e8\ud788 \uc815\ub9ac\ud55c\ub2e4.<\/p>\n\n\n\n<p>\uc774 \uc815\ub9ac\ub294 Kotlin + Gradle \uae30\ubc18\uc73c\ub85c \uc9c4\ud589\ud55c\ub2e4.<\/p>\n\n\n\n<p><strong>Dependency<\/strong><\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimplementation(&quot;org.apache.commons:commons-csv:1.9.0&quot;)\n<\/pre>\n\n\n<p><strong>CSV Generator Class<\/strong><\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npackage com.itchefblog.blog.utilities.csv\n\nimport org.apache.commons.csv.CSVFormat\nimport org.apache.commons.csv.CSVPrinter\nimport java.io.IOException\nimport java.io.PrintWriter\n\nclass CSVGenerator {\n    companion object {\n        fun dataToCSV(writer: PrintWriter, dataList: List&lt;TestData&gt;) {\n            var csvPrinter: CSVPrinter? = null\n            try {\n                csvPrinter = CSVPrinter(writer, CSVFormat.EXCEL.withHeader(&quot;\uc2dc\ud000\uc2a4&quot;, &quot;\uc81c\ubaa9&quot;, &quot;\ub0b4\uc6a9&quot;, &quot;\uc791\uc131\uc790&quot;, &quot;\uc791\uc131\uc77c\uc2dc&quot;, &quot;\uc218\uc815\uc77c\uc2dc&quot;))\n                for(data in dataList) {\n                    val item = listOf(\n                        data.no,\n                        data.title,\n                        data.content,\n                        data.userId,\n                        data.registerDatetime,\n                        data.updateDatetime,\n                    )\n                    csvPrinter.printRecord(item)\n                    csvPrinter.flush()\n                }\n            }catch(e: Exception){\n                e.printStackTrace()\n            }finally {\n                try {\n                    csvPrinter!!.close()\n                }catch(e: IOException){\n                    println(&quot;Flushing or Closing Error&quot;)\n                    e.printStackTrace()\n                }\n            }\n        }\n    }\n}\n\n<\/pre>\n\n\n<p>\uc704\uc640 \uac19\uc774 CSV Generator\uc758 dataToCSV \uba54\uc18c\ub4dc\uc5d0\uc11c \uc804\ub2ec\ubc1b\uc740 dataList\uc758 \ub0b4\uc6a9\uc744 \ub8e8\ud504\ub97c \ub3cc\uba74\uc11c item\uc5d0 \ub9e4\ud551\ud558\uc5ec csvPrinter.printRecord\uc5d0 \ub118\uaca8\uc900\ub2e4.<\/p>\n\n\n\n<p><strong>Controller<\/strong><\/p>\n\n\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@GetMapping(&quot;\/csv&quot;)\n    fun downloadTestDataAsCSV(\n        response: HttpServletResponse,\n    ) {\n            val dataList = testDataService.findAll()\n            val fileName = &quot;${LocalDateTime.now().format(DateTimeFormatter.ofPattern(&quot;yyyyMMddHHmm&quot;))}_TEST_DATA.csv&quot;\n            response.contentType = &quot;text\/csv&quot;\n            response.setHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=$fileName&quot;)\n            CSVGenerator.dataToCSV(response.writer, dataList)\n    }\n<\/pre>\n\n\n<p>\ucee8\ud2b8\ub864\ub7ec\uc5d0\uc11c\ub294 \uc704\uc640\uac19\uc774 \/csv\ub85c \ub4e4\uc5b4\uc628 request\uc5d0 \ub530\ub77c testDataService\uc758 \ub370\uc774\ud130\ub97c \ubaa8\ub450 \uc77d\uc5b4\uc640\uc11c dataList\uc5d0 \uc800\uc7a5\ud558\uace0, \uc774\ub140\uc11d\uc744 CSVGenerator.dataToCSV\uc5d0 response.writer\uacfc \ud568\uaed8 \ub118\uaca8\uc900\ub2e4.<br>CSVGenerator\uc5d0\uc11c csv\ucc98\ub9ac\ud55c \ub0b4\uc6a9\ub4e4\uc744 HttpServletResponse\uac00 \ube0c\ub77c\uc6b0\uc800\ub85c \ub9ac\ud134\ud574\uc900\ub2e4.<br>\ud30c\uc77c\uba85\uc740 \uc704\uc758 fileName\uc73c\ub85c \uc800\uc7a5\uc774 \ub41c\ub2e4.<\/p>\n\n\n\n<p>CSVGenerator\ub97c \uc0dd\uc131\ud558\uc9c0 \uc54a\uc558\ub294\ub370\ub3c4 \uba54\uc18c\ub4dc\ub97c \ubc14\ub85c \ud638\ucd9c \uac00\ub2a5\ud55c \uc774\uc720\ub294&#8230;?<br>companion object \ub85c \uc120\uc5b8\ub418\uc5c8\uae30\uc5d0..<br><br>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n\n\n\n<p>\uc5ec\uae30\uc5d0 \uc778\uc99d \ub4f1\uc758 \uae30\ub2a5\uc744 \ucd94\uac00\ud558\uba74 \uad8c\ud55c\uc774 \uc788\ub294 \uc0ac\uc6a9\uc790\ub9cc \ub2e4\uc6b4\ub85c\ub4dc\uac00 \uac00\ub2a5\ud558\ub2e4.<br>(\uc2e4\uc81c \uc801\uc6a9\ud55c \ucf54\ub4dc\uc5d0\ub294 \uc778\uc99d\uae30\ub2a5\uc774 \uc788\uc9c0\ub9cc \uc774 \ud3ec\uc2a4\ud305\uc740 CSV \uae30\ub2a5\ub9cc\uc744 \uc704\ud55c\uac70\ub77c \uc81c\uac70\ud588\ub2e4.)<\/p>\n\n\n\n<p>\uc774 \uae30\ub2a5\uc744 \ub3c4\uc785\ud55c \ubaa9\uc801\uc740 ajax\ub85c header\uac12\uc744 \uac19\uc774 \ub118\uaca8\uc11c \uc778\uc99d\ud55c \ud6c4 \ub2e4\uc6b4\ub85c\ub4dc \ucc98\ub9b4 \ud558\ub294\uac83\uc774\uc5c8\uae30\uc5d0 \ub2e8\uc21c\ud788 get \ubc29\uc2dd\uc73c\ub85c url redirect\ub9cc \ud574\uc11c\ub294 \uc0ac\uc6a9\ud560 \uc218 \uc5c6\uc5c8\ub2e4..<\/p>\n\n\n\n<p>\uadf8\ub798\uc11c \ub2e4\uc74c \ud3ec\uc2a4\ud305\uc5d0\uc11c\ub294 jQuery ajax\ub97c \uc774\uc6a9\ud574\uc11c header\uc640 \ud568\uaed8 csv \ub2e4\uc6b4\ub85c\ub4dc \uc694\uccad\uc744 \ud558\uace0, \ub9ac\ud134\ubc1b\uc740 response \ub370\uc774\ud130\ub97c \uc774\uc6a9\ud574 \uc2e4\uc81c\ub85c \uc800\uc7a5\ud558\ub294 \uc608\uc81c\ub97c \uc791\uc131\ud574\ubcfc \uc608\uc815\uc774\ub2e4.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-it-chef\uc758-\uc77c\uc0c1-\ube14\ub85c\uadf8 wp-block-embed-it-chef\uc758-\uc77c\uc0c1-\ube14\ub85c\uadf8\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"FQfHJPC486\"><a href=\"https:\/\/www.itchefblog.com\/?p=1899\">ajax\ub85c \ubc1b\uc544\uc628 CSV \ub370\uc774\ud130 \ud30c\uc77c\ub85c \uc800\uc7a5\ud558\uae30<\/a><\/blockquote><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;ajax\ub85c \ubc1b\uc544\uc628 CSV \ub370\uc774\ud130 \ud30c\uc77c\ub85c \uc800\uc7a5\ud558\uae30&#8221; &#8212; IT + CHEF\uc758 \uc77c\uc0c1 \ube14\ub85c\uadf8\" src=\"https:\/\/www.itchefblog.com\/?p=1899&#038;embed=true#?secret=FQfHJPC486\" data-secret=\"FQfHJPC486\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud14c\uc774\ube14\uc758 \ub370\uc774\ud130\ub97c CSV\ub85c \ucd9c\ub825\ud560 \uc77c\uc774 \uc0dd\uaca8\uc11c \uc774\ub9ac\uc800\ub9ac \uac80\uc0c9\ud558\ub2e4 \uc54c\uac8c\ub41c \ub0b4\uc6a9\uc744 \uac04\ub2e8\ud788 \uc815\ub9ac\ud55c\ub2e4. \uc774 \uc815\ub9ac\ub294 Kotlin + Gradle \uae30\ubc18\uc73c\ub85c \uc9c4\ud589\ud55c\ub2e4. Dependency CSV Generator Class \uc704\uc640 \uac19\uc774 CSV Generator\uc758 dataToCSV \uba54\uc18c\ub4dc\uc5d0\uc11c \uc804\ub2ec\ubc1b\uc740 dataList\uc758 \ub0b4\uc6a9\uc744 \ub8e8\ud504\ub97c \ub3cc\uba74\uc11c item\uc5d0 \ub9e4\ud551\ud558\uc5ec csvPrinter.printRecord\uc5d0 \ub118\uaca8\uc900\ub2e4. Controller \ucee8\ud2b8\ub864\ub7ec\uc5d0\uc11c\ub294 \uc704\uc640\uac19\uc774 \/csv\ub85c \ub4e4\uc5b4\uc628 request\uc5d0 \ub530\ub77c testDataService\uc758 \ub370\uc774\ud130\ub97c \ubaa8\ub450 \uc77d\uc5b4\uc640\uc11c dataList\uc5d0 \uc800\uc7a5\ud558\uace0, \uc774\ub140\uc11d\uc744 CSVGenerator.dataToCSV\uc5d0 response.writer\uacfc \ud568\uaed8 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[349,555,7,4],"tags":[651,652,654,655,484,556,596,648,649,650],"_links":{"self":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1896"}],"collection":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1896"}],"version-history":[{"count":6,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1896\/revisions"}],"predecessor-version":[{"id":1906,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1896\/revisions\/1906"}],"wp:attachment":[{"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itchefblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}