{"id":1025,"date":"2024-04-16T19:48:46","date_gmt":"2024-04-16T10:48:46","guid":{"rendered":"http:\/\/inwelly.com\/?p=1025"},"modified":"2024-04-21T13:22:23","modified_gmt":"2024-04-21T04:22:23","slug":"java-spring-poi-excel-download-sample-on-controller-java","status":"publish","type":"post","link":"https:\/\/inwelly.com\/?p=1025","title":{"rendered":"JAVA Spring &#8211; POI Excel download sample on Controller.java"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>import org.apache.poi.hssf.usermodel.HSSFCellStyle;<\/p>\n\n\n\n<p>import org.apache.poi.hssf.util.HSSFColor;<\/p>\n\n\n\n<p>import org.apache.poi.ss.usermodel.CellStyle;<\/p>\n\n\n\n<p>import org.apache.poi.xssf.streaming.SXSSFCell;<\/p>\n\n\n\n<p>import org.apache.poi.xssf.streaming.SXSSFRow;<\/p>\n\n\n\n<p>import org.apache.poi.xssf.streaming.SXSSFSheet;<\/p>\n\n\n\n<p>import org.apache.poi.xssf.streaming.SXSSFWorkbook;<\/p>\n\n\n\n<p>import java.io.OutputStream;<\/p>\n\n\n\n<p>import java.text.SimpleDateFormat;<\/p>\n\n\n\n<p>&nbsp; @RequestMapping(value = &#8220;\/test\/test\/excel.do&#8221;)<\/p>\n\n\n\n<p>public void excel(HttpServletResponse response) throws Exception {<\/p>\n\n\n\n<p>List&lt;SomereportVO&gt; list = statisticsService.selectExcelList();<\/p>\n\n\n\n<p>SimpleDateFormat format1 = new SimpleDateFormat (&#8220;yyyyMMddHHmmssSSS&#8221;);<\/p>\n\n\n\n<p>Date time = new Date();<\/p>\n\n\n\n<p>String TODAYTXT = format1.format(time);<\/p>\n\n\n\n<p>SXSSFWorkbook wb = new SXSSFWorkbook(100);<\/p>\n\n\n\n<p>SXSSFSheet&nbsp; sheet = wb.createSheet(&#8220;TEST_&#8221;+TODAYTXT);<\/p>\n\n\n\n<p>SXSSFRow&nbsp; row = sheet.createRow(0);<\/p>\n\n\n\n<p>SXSSFCell cell = row.createCell(1);<\/p>\n\n\n\n<p>\/\/SXSSFWorkbook wb = new SXSSFWorkbook(100);<\/p>\n\n\n\n<p>\/\/Sheet sheet = wb.createSheet(&#8220;TEST&#8221;); \/\/ Sheet Name<\/p>\n\n\n\n<p>\/\/Row row = null;<\/p>\n\n\n\n<p>\/\/Cell cell = null;<\/p>\n\n\n\n<p>int rowNo = 0;<\/p>\n\n\n\n<p>String[] columns_head = new String[]{&#8220;\ub9e4\ucd9c\uc77c\uc790&#8221;, &#8220;\uc0c1\ud488 ID&#8221;, &#8220;\uace0\uac1d ID&#8221;,&#8221;TEST1&#8243;,&#8221;TEST2&#8243;,&#8221;TEST3&#8243;,&#8221;TEST4&#8243;,&#8221;TEST5&#8243;};<\/p>\n\n\n\n<p>int[] columns_width = new int[]{1000, 5000, 3000, 1000 , 2000, 3000,1000 , 500};<\/p>\n\n\n\n<p>try {<\/p>\n\n\n\n<p>\/\/ \ud14c\uc774\ube14 \ud5e4\ub354\uc6a9 \uc2a4\ud0c0\uc77c<\/p>\n\n\n\n<p>CellStyle headStyle = wb.createCellStyle();<\/p>\n\n\n\n<p>headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>headStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);<\/p>\n\n\n\n<p>headStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);<\/p>\n\n\n\n<p>headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);<\/p>\n\n\n\n<p>\/\/ \ub370\uc774\ud130\uc6a9 \uacbd\uacc4 \uc2a4\ud0c0\uc77c<\/p>\n\n\n\n<p>CellStyle bodyStyle = wb.createCellStyle();<\/p>\n\n\n\n<p>bodyStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>bodyStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>bodyStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>bodyStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);<\/p>\n\n\n\n<p>\/\/ \ud5e4\ub354 \ubd80\ubd84 \uc0dd\uc131<\/p>\n\n\n\n<p>row = sheet.createRow(rowNo++);<\/p>\n\n\n\n<p>for(int i = 0; i &lt; columns_head.length; i++)&nbsp;<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>sheet.setColumnWidth(i, (sheet.getColumnWidth(i)) + columns_width[i]);<\/p>\n\n\n\n<p>cell = row.createCell(i);<\/p>\n\n\n\n<p>cell.setCellStyle(headStyle);<\/p>\n\n\n\n<p>cell.setCellValue(columns_head[i]);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/ \ub370\uc774\ud130 \ubd80\ubd84 \uc0dd\uc131<\/p>\n\n\n\n<p>for(SaleFigureMDreportVO vo : list) {<\/p>\n\n\n\n<p>row = sheet.createRow(rowNo++);<\/p>\n\n\n\n<p>for(int j = 0; j &lt; columns_head.length ; j++)&nbsp;<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>cell = row.createCell(j);<\/p>\n\n\n\n<p>cell.setCellStyle(bodyStyle);<\/p>\n\n\n\n<p>if( j == 0 ) { cell.setCellValue(vo.getSDt()); }<\/p>\n\n\n\n<p>if( j == 1 ) { cell.setCellValue(vo.getGId()); }<\/p>\n\n\n\n<p>if( j == 2 ) { cell.setCellValue(vo.getNo()); }<\/p>\n\n\n\n<p>if( j == 3 ) { cell.setCellValue(vo.getBKo()); }<\/p>\n\n\n\n<p>if( j == 4 ) { cell.setCellValue(vo.getCNo()); }<\/p>\n\n\n\n<p>if( j == 5 ) { cell.setCellValue(vo.getCNm()); }<\/p>\n\n\n\n<p>if( j == 6 ) { cell.setCellValue(vo.getONo()); }<\/p>\n\n\n\n<p>if( j == 7 ) { cell.setCellValue(vo.getINm()); }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/ \uc5d1\uc140 \ucd9c\ub825<\/p>\n\n\n\n<p>response.setHeader(&#8220;Set-Cookie&#8221;, &#8220;fileDownload=true; path=\/&#8221;);<\/p>\n\n\n\n<p>response.setHeader(&#8220;Content-Disposition&#8221;, String.format(&#8220;attachment; filename=\\&#8221;test_&#8221;+TODAYTXT+&#8221;.xlsx\\&#8221;&#8221;)); \/\/ \ud30c\uc77c\uba85<\/p>\n\n\n\n<p>wb.write(response.getOutputStream());<\/p>\n\n\n\n<p>}&nbsp; &nbsp;catch(Exception e) {<\/p>\n\n\n\n<p>response.setHeader(&#8220;Set-Cookie&#8221;, &#8220;fileDownload=false; path=\/&#8221;);<\/p>\n\n\n\n<p>response.setHeader(&#8220;Cache-Control&#8221;, &#8220;no-cache, no-store, must-revalidate&#8221;);<\/p>\n\n\n\n<p>response.setHeader(&#8220;Content-Type&#8221;,&#8221;text\/html; charset=utf-8&#8243;);<\/p>\n\n\n\n<p>OutputStream out = null;<\/p>\n\n\n\n<p>try {<\/p>\n\n\n\n<p>out = response.getOutputStream();<\/p>\n\n\n\n<p>byte[] data = new String(&#8220;fail..&#8221;).getBytes();<\/p>\n\n\n\n<p>out.write(data, 0, data.length);<\/p>\n\n\n\n<p>} catch(Exception ignore) {<\/p>\n\n\n\n<p>ignore.printStackTrace();<\/p>\n\n\n\n<p>} finally {<\/p>\n\n\n\n<p>if(out != null) try { out.close(); } catch(Exception ignore) {}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>} finally {<\/p>\n\n\n\n<p>\/\/ \ub514\uc2a4\ud06c \uc801\uc5c8\ub358 \uc784\uc2dc\ud30c\uc77c\uc744 \uc81c\uac70\ud569\ub2c8\ub2e4.<\/p>\n\n\n\n<p>wb.dispose();<\/p>\n\n\n\n<p>try { wb.close(); } catch(Exception ignore) {}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>}<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import java.io.OutputStream; import java.text.SimpleDateFormat; &nbsp; @RequestMapping(value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1280,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[],"class_list":["post-1025","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1025"}],"version-history":[{"count":1,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1025\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1025\/revisions\/1026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/media\/1280"}],"wp:attachment":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}