{"id":4990,"date":"2025-01-21T15:03:57","date_gmt":"2025-01-21T07:03:57","guid":{"rendered":"https:\/\/qaqaq.top\/?p=4990"},"modified":"2025-01-21T15:03:57","modified_gmt":"2025-01-21T07:03:57","slug":"java-sql%e5%af%bc%e5%87%ba%e5%88%b0excel","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=4990","title":{"rendered":"Java SQL\u5bfc\u51fa\u5230Excel"},"content":{"rendered":"\n<p>\u5b57\u8282\u6d41<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    @PostMapping(\"\/downloadExcel\")\n    public ResponseEntity&lt;byte&#91;]> downloadExcel() throws Exception {\n        \/\/ Excel\u6587\u4ef6\u540d\n        String fileName = \"StockInfo.xlsx\";\n\n        \/\/ \u4f7f\u7528ByteArrayOutputStream\u4f5c\u4e3a\u8f93\u51fa\u6d41\n        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\n        ExcelWriter excelWriter = EasyExcel.write(byteArrayOutputStream).build();\n        WriteSheet writeSheet = EasyExcel.writerSheet(\"Data Sheet\").build();\n\n        \/\/ \u51c6\u5907\u8981\u5199\u5165\u7684\u6570\u636e\n        List&lt;List&lt;Object>> data = new ArrayList&lt;>();\n        data.add(Arrays.asList(\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"));\n\n        \/\/ \u5c06\u6570\u636e\u5199\u5165Excel\n        excelWriter.write(data, writeSheet);\n        \/\/ \u5173\u95edExcelWriter\n        excelWriter.finish();\n\n        \/\/ \u8bbe\u7f6eHTTP\u5934\u90e8\u4fe1\u606f\n        HttpHeaders headers = new HttpHeaders();\n        headers.setContentType(MediaType.parseMediaType(\"application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet\"));\n        headers.setContentDispositionFormData(\"attachment\", URLEncoder.encode(fileName, \"UTF-8\"));\n\n        \/\/ \u8fd4\u56deResponseEntity\uff0c\u5305\u542bExcel\u6587\u4ef6\u5185\u5bb9\u548c\u5934\u90e8\u4fe1\u606f\n        return ResponseEntity.ok().headers(headers).body(byteArrayOutputStream.toByteArray());\n    }<\/code><\/pre>\n\n\n\n<p>\u6587\u4ef6\u6d41<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    @PostMapping(\"\/downloadExcel\")\n    public ResponseEntity&lt;Resource> downloadExcel() throws Exception {\n        \/\/ Excel\u6587\u4ef6\u540d\n        String fileName = \"StockInfo.xlsx\";\n        \/\/ \u521b\u5efaExcel\u6587\u4ef6\u5728\u670d\u52a1\u5668\u7684\u4e34\u65f6\u5b58\u50a8\u8def\u5f84\n        File tempFile = File.createTempFile(\"StockInfo\", \".xlsx\");\n\n        \/\/ \u4f7f\u7528FileOutputStream\u4f5c\u4e3a\u8f93\u51fa\u6d41\n        FileOutputStream fileOutputStream = new FileOutputStream(tempFile);\n        ExcelWriter excelWriter = EasyExcel.write(fileOutputStream).build();\n        WriteSheet writeSheet = EasyExcel.writerSheet(\"Data Sheet\").build();\n\n        \/\/ \u51c6\u5907\u8981\u5199\u5165\u7684\u6570\u636e\n        List&lt;List&lt;Object>> data = new ArrayList&lt;>();\n        data.add(Arrays.asList(\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"));\n\n        \/\/ \u5c06\u6570\u636e\u5199\u5165Excel\n        excelWriter.write(data, writeSheet);\n        \/\/ \u5173\u95edExcelWriter\n        excelWriter.finish();\n        \/\/ \u5173\u95ed\u6587\u4ef6\u8f93\u51fa\u6d41\n        fileOutputStream.close();\n\n        \/\/ \u8bbe\u7f6eHTTP\u5934\u90e8\u4fe1\u606f\n        HttpHeaders headers = new HttpHeaders();\n        headers.setContentType(MediaType.parseMediaType(\"application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet\"));\n        headers.setContentDispositionFormData(\"attachment\", URLEncoder.encode(fileName, \"UTF-8\"));\n        \n        \/\/ \u4f7f\u7528FileSystemResource\u5305\u88c5\u6587\u4ef6\uff0c\u4ee5\u4fbf\u4e8e\u5728HTTP\u54cd\u5e94\u4e2d\u4f20\u8f93\n        Resource resource = new FileSystemResource(tempFile);\n\n        \/\/ \u8fd4\u56deResponseEntity\uff0c\u5305\u542bExcel\u6587\u4ef6\u5185\u5bb9\u548c\u5934\u90e8\u4fe1\u606f\n        return ResponseEntity.ok().headers(headers).contentLength(resource.contentLength()).body(resource);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u8282\u6d41 \u6587\u4ef6\u6d41<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-4990","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/4990"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4990"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/4990\/revisions"}],"predecessor-version":[{"id":4991,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/4990\/revisions\/4991"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}