λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
개발/Java

[Spring] μŠ€ν”„λ§ μ—‘μ…€νŒŒμΌ μ—…λ‘œλ“œ λ‹€μš΄λ‘œλ“œ κΈ°λ³Έ(POI 라이브러리 1편) :: 마이자λͺ½

by πŸŒ»β™š 2019. 4. 23.

μ—‘μ…€νŒŒμΌ μ—…λ‘œλ“œ λ‹€μš΄λ‘œλ“œ

μ–΄λŠ ν”„λ‘œμ νŠΈλ₯Ό μ§„ν–‰ν•˜λ˜κ°„μ— μ›ΉμœΌλ‘œ 데이터λ₯Ό κ΄€λ¦¬ν•˜λŠ”λ° μ—‘μ…€νŒŒμΌ κΈ°λŠ₯이 빠질 수 μ—†λ‹€.

DBλ°μ΄ν„°λ‚˜ μ–΄λ– ν•œ λ‘œμ§μ— μ˜ν•œ 결과값을 μ—‘μ…€νŒŒμΌ ν˜•νƒœλ‘œ λ‹€μš΄λ‘œλ“œλ₯Ό λ°›κ±°λ‚˜ μ—‘μ…€λ‘œ μ •λ¦¬ν•œ νŒŒμΌμ„ μ—…λ‘œλ“œν•΄μ„œ API에 ν•΄λ‹Ή 데이터λ₯Ό λ˜μ§€κ±°λ‚˜  DB에 μ €μž₯ν•˜λŠ” μž‘μ—…μ€ κΌ­ ν•„μš”ν•˜λ‹€.

μ—‘μ…€λ§Œ μž˜ν•˜λ©΄ ν”„λ‘œκ·Έλž¨μ΄ ν•„μš”μ—†λ‹€λŠ” 말이 μžˆλ“―μ΄, μ›Ήμƒμ—μ„œ μ›ν™œν•˜κ²Œ μ—‘μ…€νŒŒμΌμ„ ν™œμš©ν•  수 μžˆλ„λ‘ 관리도ꡬλ₯Ό λ§Œλ“€μ–΄μ£ΌλŠ” ν”„λ‘œκ·Έλž¨μ€ κΌ­ ν•„μš”ν•˜λ‹€. 이전에 올린 파이썬으둜 μ—‘μ…€νŒŒμΌμ„ λ‹€λ‘œλŠ” 법(https://myjamong.tistory.com/51) λ³΄λ‹€λŠ” 쑰금 더 λ³΅μž‘ν•œ ꡬ쑰둜 λ˜μ–΄ μžˆμ§€λ§Œ, 아직은 μžλ°” μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬λ₯Ό μ‚¬μš©ν•˜λŠ” 곡곡 업체듀이 λ§Žμ•„ κΌ­ μ•Œμ•„λ†”μ•Όν•œλ‹€κ³  μƒκ°ν•œλ‹€.

μŠ€ν”„λ§μ„ ν™œμš©ν•œ μ—‘μ…€ 파일 μ—…λ‘œλ“œ λ‹€μš΄λ‘œλ“œλ₯Ό ν…Œλ§ˆλ‘œ 작고 λͺ‡κ°œμ˜ κΈ€λ‘œ λ‚˜λˆ μ„œ κΈ°λ³ΈλΆ€ν„° κ³ κΈ‰λ‚΄μš©κΉŒμ§€ μ„€λͺ… 및 μ‹€μŠ΅μ„ 진행할 μ˜ˆμ •μ΄λ‹€.

 

 

POI 라이브러리

μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬μ—μ„œ μ—‘μ…€ νŒŒμΌμ„ 닀루기 μœ„ν•΄ μ‚¬μš©λ˜λŠ” λΌμ΄λΈŒλŸ¬λ¦¬κ°€ λ°”λ‘œ POI이닀. POI 라이브러리 없이도 λ‹¨μˆœνžˆ ν…μŠ€νŠΈλ§Œ μž…λ ₯λ˜μ–΄ μžˆλŠ” μ—‘μ…€νŒŒμΌμ„ 뢈러올렀면 HTML ν˜•νƒœλ₯Ό xlsx ν™•μž₯자둜 받아버리면 ν•΄κ²°λ˜μ§€λ§Œ, POI 라이브러리λ₯Ό μ‚¬μš©ν•˜λ©΄ μ…€μ˜ μŠ€νƒ€μΌ, μ…€μ˜ λ„ˆλΉ„ 등을 둜직 λ‚΄μ—μ„œ κ΅¬ν˜„μ΄ κ°€λŠ₯ν•˜κ³ , μˆ˜μ‹ 및 μ—‘μ…€μ˜ μ—¬λŸ¬ κΈ°λŠ₯을 같이 μ‚¬μš©ν•  수 μžˆλ‹€.
 

maven pom.xml

poi λΌμ΄λΈŒλŸ¬λ¦¬μ™€ ν•¨κ»˜ 이번 μ‹€μŠ΅μ„ μ§„ν–‰ν•˜κΈ° μœ„ν•΄
jsonν˜•νƒœλ‘œ μ—…λ‘œλ“œν•œ μ—‘μ…€ 데이터λ₯Ό 뿌렀주기 μœ„ν•΄ jackson 라이브러리 그리고
νŒŒμΌμ„ μ—…λ‘œλ“œ ν•΄μ•Όν•˜λ‹ˆκΉŒ MultipartFile둜 requestλ₯Ό λ°›μ•„μ£ΌκΈ° μœ„ν•΄ commons-fileupload 라이브러리λ₯Ό maven을 톡해 λ°›μ•„μ€€λ‹€.
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.0</version>
</dependency>    

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0.pr4</version>
</dependency>
    
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.9.1</version>
</dependency>  

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.2</version>
</dependency>

 

 

 

 

 

servlet-context

μ»¨νŠΈλ‘€λŸ¬μ—μ„œ view둜 λ°”λ‘œ λ§€ν•‘λ˜μ§€ μ•Šκ³  ν•΄λ‹Ή 객체둜 λ°”λ‘œ 맀핑해주기 μœ„ν•΄ servlet-contextμ—μ„œ 빈즈λ₯Ό λ“±λ‘ν•΄μ€˜μ•Όν•œλ‹€.
μ—¬κΈ°μ„œ μ£Όμš”ν•œκ±°λŠ” BeanNameViewResolver 빈즈의 μˆœμ„œκ°€ InternalResourceViewResolver보닀 μš°μ„ μ΄μ–΄μ•Όν•œλ‹€.
InternalResourceViewResolverκ°€ μš°μ„ μˆœμœ„λ₯Ό κ°–μœΌλ©΄ μš°λ¦¬κ°€ μ›ν•˜λŠ” json ν˜•νƒœμ˜ λ°˜ν™˜μ΄λ‚˜, μ—‘μ…€λ‹€μš΄λ‘œλ“œλ₯Ό μ§„ν–‰ν•˜μ§€ μ•Šκ³  ν•΄λ‹Ή 맀핑이름에 .jspλ₯Ό 달고 viewλ₯Ό μ°Ύκ²Œλœλ‹€(ex : excelDownloadView.jspλ₯Ό 찾느게 μ•„λ‹ˆλΌ  ExcelDownloadView 클래슀파일의 싀행이 ν•„μš”ν•œκ²ƒμ΄λ‹€.)
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <annotation-driven />

    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
        <beans:property name="order" value="0"/>
    </beans:bean>

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
        <beans:property name="order" value="1"></beans:property>
    </beans:bean>
    
    <beans:bean id="excelDownloadView" class="com.test.excel.util.ExcelDownloadView"/>
    
    <beans:bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
    
    <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    
    <context:component-scan base-package="com.*" />
    
</beans:beans>
 

 

 

μ—‘μ…€ μ—…λ‘œλ“œ λ‹€μš΄λ‘œλ“œ ν”„λ‘œμ νŠΈ

ν•΄λ‹Ή ν”„λ‘œμ νŠΈμ˜ μžλ°” κ΅¬μ‘°λŠ” μ•„λž˜μ™€κ°™μ΄ μƒμ„±ν–ˆλ‹€.

 

 

 

Fruit.java

과일객체가 λ‹΄κΈ΄ 리슀트λ₯Ό μž‘μ„±ν•˜μ—¬ μ—‘μ…€νŒŒμΌλ‘œ λ‹€μš΄λ‘œλ“œν•˜λŠ” μž‘μ—…κ³Ό

λ‹€μš΄λ°›μ€ μ—‘μ…€ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ—¬ 과일객체가 λ‹΄κΈ΄ λ¦¬μŠ€νŠΈμ— λ‹΄μ•„ 화면에 Jsonν˜•νƒœλ‘œ λΏŒλ €μ£ΌλŠ” μž‘μ—…κΉŒμ§€ 진행할거닀.

package com.test.excel.vo;

public class Fruit {
    String name;
    long price;
    int quantity;
    
    public Fruit() {
        
    }
    
    public Fruit(String name, long price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }
    
    public String getName() {
        return name;
    }
    
    public long getPrice() {
        return price;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void setPrice(long price) {
        this.price = price;
    }
    
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
}

 

 

 

 

 

ExcelDownloadView.java

μ—‘μ…€ λ‹€μš΄λ‘œλ“œν•˜λŠ”λ° μžˆμ–΄μ„œ κ°€μž₯ μ€‘μš”ν•œ ν΄λž˜μŠ€μ΄λ‹€. λŒ€λΆ€λΆ„μ˜ ν”„λ‘œμ νŠΈμ—μ„œ ν•΄λ‹Ή ν΄λž˜μŠ€λŠ” μ½”λ“œ μˆ˜μ •λ˜λŠ” 뢀뢄은 μœ„μ— 파일 이름 μ„€μ •ν•˜λŠ” λΆ€λΆ„ λΉΌκ³ λŠ” 거의 동일할것이닀.
μœ„ μ„œλΈ”λ¦Ώ μ„€μ •μ—μ„œ 봀듯이, excelDownloadView둜 μ»¨νŠΈλ‘€λŸ¬μ—μ„œ λ§€ν•‘μ‹œ ν•΄λ‹Ή 클래슀둜 λ°”λ‘œ 맀핑해쀀닀.
package com.test.excel.util;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.web.servlet.view.AbstractView;

public class ExcelDownloadView extends AbstractView{
    
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        Locale locale = (Locale) model.get("locale");
        String workbookName = (String) model.get("workbookName");
        
        // κ²ΉμΉ˜λŠ” 파일 이름 쀑볡을 ν”Όν•˜κΈ° μœ„ν•΄ μ‹œκ°„μ„ μ΄μš©ν•΄μ„œ 파일 이름에 μΆ”
        Date date = new Date();
        SimpleDateFormat dayformat = new SimpleDateFormat("yyyyMMdd", locale);
        SimpleDateFormat hourformat = new SimpleDateFormat("hhmmss", locale);
        String day = dayformat.format(date);
        String hour = hourformat.format(date);
        String fileName = workbookName + "_" + day + "_" + hour + ".xlsx";         
        
        // μ—¬κΈ°μ„œλΆ€ν„°λŠ” 각 λΈŒλΌμš°μ €μ— λ”°λ₯Έ νŒŒμΌμ΄λ¦„ μΈμ½”λ”©μž‘μ—…
        String browser = request.getHeader("User-Agent");
        if (browser.indexOf("MSIE") > -1) {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } else if (browser.indexOf("Trident") > -1) {       // IE11
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } else if (browser.indexOf("Firefox") > -1) {
            fileName = "\"" + new String(fileName.getBytes("UTF-8"), "8859_1") + "\"";
        } else if (browser.indexOf("Opera") > -1) {
            fileName = "\"" + new String(fileName.getBytes("UTF-8"), "8859_1") + "\"";
        } else if (browser.indexOf("Chrome") > -1) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < fileName.length(); i++) {
               char c = fileName.charAt(i);
               if (c > '~') {
                     sb.append(URLEncoder.encode("" + c, "UTF-8"));
                       } else {
                             sb.append(c);
                       }
                }
                fileName = sb.toString();
        } else if (browser.indexOf("Safari") > -1){
            fileName = "\"" + new String(fileName.getBytes("UTF-8"), "8859_1")+ "\"";
        } else {
             fileName = "\"" + new String(fileName.getBytes("UTF-8"), "8859_1")+ "\"";
        }
        
        response.setContentType("application/download;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
        response.setHeader("Content-Transfer-Encoding", "binary");
        
       OutputStream os = null;
       SXSSFWorkbook workbook = null;
       
       try {
           workbook = (SXSSFWorkbook) model.get("workbook");
           os = response.getOutputStream();
           
           // νŒŒμΌμƒμ„±
           workbook.write(os);
       }catch (Exception e) {
           e.printStackTrace();
       } finally {
           if(workbook != null) {
               try {
                   workbook.close();
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
           
           if(os != null) {
               try {
                   os.close();
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }
    }
}
 

 

 

 

μŠ€ν”„λ§ μ—‘μ…€ λ‹€μš΄λ‘œλ“œ μ—…λ‘œλ“œ

view

μžλ°”λ‘œμ§μ— μ•žμ„œ 좜λ ₯λ˜λŠ” viewλ‹€.
css 적용 없이 맀우 κ°„λ‹¨ν•˜κ²Œ μŠ€ν¬λ¦½νŠΈλΆ€λΆ„μ„ λ§Œλ“€μ–΄μ€¬λ‹€. νŠΉμ΄μ‚¬ν•­μœΌλ‘œ νŒŒμΌμ„ λ‹€λ£¨λŠ” 폼 μ΄λ―€λ‘œ form의  enctype은 κΌ­ multipart/form-data둜 ν•΄μ€€λ‹€.
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
    <title>Home</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<script type="text/javascript">
    function doExcelUploadProcess(){
        var f = new FormData(document.getElementById('form1'));
        $.ajax({
            url: "uploadExcelFile",
            data: f,
            processData: false,
            contentType: false,
            type: "POST",
            success: function(data){
                console.log(data);
                document.getElementById('result').innerHTML = JSON.stringify(data);
            }
        })
    }
    
    function doExcelDownloadProcess(){
        var f = document.form1;
        f.action = "downloadExcelFile";
        f.submit();
    }
</script>
<form id="form1" name="form1" method="post" enctype="multipart/form-data">
    <input type="file" id="fileInput" name="fileInput">
    <button type="button" onclick="doExcelUploadProcess()">μ—‘μ…€μ—…λ‘œλ“œ μž‘μ—…</button>
    <button type="button" onclick="doExcelDownloadProcess()">μ—‘μ…€λ‹€μš΄λ‘œλ“œ μž‘μ—…</button>
</form>
<div id="result">
</div>
</body>
</html>
 

 

 

μ—‘μ…€ λ‹€μš΄λ‘œλ“œ

과일 객체λ₯Ό λͺ‡κ°œ λ§Œλ“€μ–΄ λ¦¬μŠ€νŠΈμ— λ‹΄κ³  ν•΄λ‹Ή 리슀트의 λ‚΄μš©μ„ μ—‘μ…€νŒŒμΌλ‘œ λ‹€μš΄λ°›λŠ” μž‘μ—…μ„ 진행할것이닀.
 

ExcelController 맀핑

총 4개의 과일정보λ₯Ό 담은 객체λ₯Ό λ¦¬μŠ€νŠΈμ— λ„£μ–΄ μ—‘μ…€ μ›Œν¬λΆμœΌλ‘œ 가곡 ν›„ ExcelDownloadView 클래슀둜 λ§€ν•‘μ‹œν‚¨λ‹€.
@RequestMapping(value = "/downloadExcelFile", method = RequestMethod.POST)
    public String downloadExcelFile(Model model) {
        String[] names = {"자λͺ½", "μ• ν”Œλ§κ³ ", "멜둠", "μ˜€λ Œμ§€"};
        long[] prices = {5000, 10000, 7000, 6000};
        int[] quantities = {50, 50, 40, 40};
        List<Fruit> list = service.makeFruitList(names, prices, quantities);
        
        SXSSFWorkbook workbook = service.excelFileDownloadProcess(list);
        
        model.addAttribute("locale", Locale.KOREA);
        model.addAttribute("workbook", workbook);
        model.addAttribute("workbookName", "κ³ΌμΌν‘œ");
        
        return "excelDownloadView";
    }
 

 

 

 ExcelService μ—‘μ…€ μ‹œνŠΈ 생성 둜직

μ—‘μ…€ μ›Œν¬λΆμ„ λ§Œλ“œλŠ”κ³Όμ •μ€ μ£Όμ„μœΌλ‘œ μ„€λͺ…이 λ˜μ–΄μžˆλ‹€.
전체 흐름을 보면 λ³„λ‘œ μ–΄λ €μš΄ 뢀뢄은 μ—†λ‹€. μ‹œνŠΈλ₯Ό μƒμ„±ν•˜κ³ , 행을 μƒμ„±ν•˜κ³ , 셀을 μƒμ„±ν•˜κ³  μ…€μ•ˆμ˜ λ‚΄μš©μ„ λ„£μ–΄μ£Όλ©΄ 끝이닀.
   /**
     * μ—‘μ…€νŒŒμΌλ‘œ λ§Œλ“€ 리슀트 생성
     * @param names
     * @param prices
     * @param quantities
     * @return μ—‘μ…€νŒŒμΌ 리슀트
     */
    public ArrayList<Fruit> makeFruitList(String[] names, long[] prices, int[] quantities){
        ArrayList<Fruit> list = new ArrayList<Fruit>();
        for(int i=0; i< names.length; i++) {
            Fruit fruit = new Fruit(names[i], prices[i], quantities[i]);
            list.add(fruit);
        }
        return list;
    }
    
    /**
     * 과일 리슀트λ₯Ό κ°„λ‹¨ν•œ μ—‘μ…€ μ›Œν¬λΆ 객체둜 생성
     * @param list
     * @return μƒμ„±λœ μ›Œν¬λΆ
     */
    public SXSSFWorkbook makeSimpleFruitExcelWorkbook(List<Fruit> list) {
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        
        // μ‹œνŠΈ 생성
        SXSSFSheet sheet = workbook.createSheet("κ³ΌμΌν‘œ");
        
        //μ‹œνŠΈ μ—΄ λ„ˆλΉ„ μ„€μ •
        sheet.setColumnWidth(0, 1500);
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(0, 3000);
        sheet.setColumnWidth(0, 1500);
        
        // 헀더 ν–‰ 생
        Row headerRow = sheet.createRow(0);
        // ν•΄λ‹Ή ν–‰μ˜ 첫번째 μ—΄ μ…€ 생성
        Cell headerCell = headerRow.createCell(0);
        headerCell.setCellValue("번호");
        // ν•΄λ‹Ή ν–‰μ˜ λ‘λ²ˆμ§Έ μ—΄ μ…€ 생성
        headerCell = headerRow.createCell(1);
        headerCell.setCellValue("과일이름");
        // ν•΄λ‹Ή ν–‰μ˜ μ„Έλ²ˆμ§Έ μ—΄ μ…€ 생성
        headerCell = headerRow.createCell(2);
        headerCell.setCellValue("가격");
        // ν•΄λ‹Ή ν–‰μ˜ λ„€λ²ˆμ§Έ μ—΄ μ…€ 생성
        headerCell = headerRow.createCell(3);
        headerCell.setCellValue("μˆ˜λŸ‰");
        
        // κ³ΌμΌν‘œ λ‚΄μš© ν–‰ 및 μ…€ 생성
        Row bodyRow = null;
        Cell bodyCell = null;
        for(int i=0; i<list.size(); i++) {
            Fruit fruit = list.get(i);
            
            // ν–‰ 생성
            bodyRow = sheet.createRow(i+1);
            // 데이터 번호 ν‘œμ‹œ
            bodyCell = bodyRow.createCell(0);
            bodyCell.setCellValue(i + 1);
            // 데이터 이름 ν‘œμ‹œ
            bodyCell = bodyRow.createCell(1);
            bodyCell.setCellValue(fruit.getName());
            // 데이터 가격 ν‘œμ‹œ
            bodyCell = bodyRow.createCell(2);
            bodyCell.setCellValue(fruit.getPrice());
            // 데이터 μˆ˜λŸ‰ ν‘œμ‹œ
            bodyCell = bodyRow.createCell(3);
            bodyCell.setCellValue(fruit.getQuantity());
        }
        
        return workbook;
    }
    
    /**
     * μƒμ„±ν•œ μ—‘μ…€ μ›Œν¬λΆμ„ μ»¨νŠΈλ‘€λ ˆμ—μ„œ λ°›κ²Œν•΄μ€„ λ©”μ†Œ
     * @param list
     * @return
     */
    public SXSSFWorkbook excelFileDownloadProcess(List<Fruit> list) {
        return this.makeSimpleFruitExcelWorkbook(list);
    }
 

 

 

result

μ›ν•˜λŠ” ν˜•μ‹μœΌλ‘œ 좜λ ₯이 λœκ²ƒμ„ 확인할 수 μžˆλ‹€.

 

 

 

μ—‘μ…€ μ—…λ‘œλ“œ

νŠΉμ • 틀에 λ§žλŠ” μ—‘μ…€νŒŒμΌμ„ μ—…λ‘œλ“œν•˜μ—¬ 과일객체에 λ‹΄κ³  리슀트둜 λ§Œλ“€μ–΄ 화면에 μ΅œμ’…μ μœΌλ‘œ λΏŒλ €μ£ΌλŠ” μž‘μ—…μ„ μ§„ν–‰ν•œλ‹€.
 
 

ExcelController 맀핑

MultipartFile에 μ—‘μ…€νŒŒμΌμ„ λ‹΄μ•„ λ°›κ³  Jsonν˜•νƒœλ‘œ response ν•΄μ£ΌκΈ° μœ„ν•΄ jsonView 빈즈둜 λ§€ν•‘ν•œλ‹€.
    @RequestMapping(value = "/uploadExcelFile", method = RequestMethod.POST)
    public String uploadExcelFile(MultipartHttpServletRequest request, Model model) {
        MultipartFile file = null;
        Iterator<String> iterator = request.getFileNames();
        if(iterator.hasNext()) {
            file = request.getFile(iterator.next());
        }
        List<Fruit> list = service.uploadExcelFile(file);
        
        model.addAttribute("list", list);
        return "jsonView";
    }
 

 

 

ExcelService μ—‘μ…€νŒŒμΌ μ—…λ‘œλ“œ ν›„ 과일 리슀트둜 λ°˜ν™˜ 둜직

μ—‘μ…€νŒŒμΌμ„ μ½μ–΄λ“€μ΄λŠ” λ‘œμ§μ΄λ‹€.
μ—‘μ…€νŒŒμΌμ„ μƒμ„±ν–ˆμ„ λ•Œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ μ‹œνŠΈλ₯Ό 뢈러였고 ν–‰κ³Ό 셀을 λΆˆλŸ¬μ™€ μž‘μ—…μ„ μ§„ν–‰ν•œλ‹€.
λΆˆλŸ¬μ˜¬λ•ŒλŠ” 각 ν–‰κ³Ό μ…€μ˜ μœ„μΉ˜λ₯Ό 번호둜 λ°›μ•„μ˜¨λ‹€.
/**
     *μ—…λ‘œλ“œν•œ μ—‘μ…€νŒŒμΌμ„ 과일 리슀트둜 λ§Œλ“€κΈ°
     * @param excelFile
     * @return μƒμ„±ν•œ 과일 리슀트
     */
    public List<Fruit> uploadExcelFile(MultipartFile excelFile){
        List<Fruit> list = new ArrayList<Fruit>();
        try {
            OPCPackage opcPackage = OPCPackage.open(excelFile.getInputStream());
            XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);
            
            // 첫번째 μ‹œνŠΈ 뢈러였기
            XSSFSheet sheet = workbook.getSheetAt(0);
            
            for(int i=1; i<sheet.getLastRowNum() + 1; i++) {
                Fruit fruit = new Fruit();
                XSSFRow row = sheet.getRow(i);
                
                // 행이 μ‘΄μž¬ν•˜κΈ° μ•ŠμœΌλ©΄ 패슀
                if(null == row) {
                    continue;
                }
                
                // ν–‰μ˜ λ‘λ²ˆμ§Έ μ—΄(이름뢀터 λ°›μ•„μ˜€κΈ°) 
                XSSFCell cell = row.getCell(1);
                if(null != cell) fruit.setName(cell.getStringCellValue());
                // ν–‰μ˜ μ„Έλ²ˆμ§Έ μ—΄ λ°›μ•„μ˜€κΈ°
                cell = row.getCell(2);
                if(null != cell) fruit.setPrice((long)cell.getNumericCellValue());
                // ν–‰μ˜ λ„€λ²ˆμ§Έ μ—΄ λ°›μ•„μ˜€κΈ°
                cell = row.getCell(3);
                if(null != cell) fruit.setQuantity((int)cell.getNumericCellValue());
                
                list.add(fruit);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
 

 

 

result

νŒŒμΌμ„ μ„ νƒν•΄μ„œ λ‹€μš΄λ‘œλ“œλ°›μ€ μ—‘μ…€νŒŒμΌμ„ κ·ΈλŒ€λ‘œ 선택해주고, μ—‘μ…€μ—…λ‘œλ“œλ₯Ό μ§„ν–‰ν•˜μ—¬ 화면에 Jsonν˜•νƒœλ‘œ 좜λ ₯됬닀.

 

 

POI 라이브러리 1νŽΈμ—μ„œλŠ” κ°„λ‹¨ν•˜κ²Œ νŒŒμΌμ„ μ—…λ‘œλ“œν•˜κ³  λ‹€μš΄λ‘œλ“œν•˜λŠ” 기본적인 μž‘μ—…μ„ μ§„ν–‰ν–ˆλ‹€. μ–΄λ €μš΄ μ„ΈνŒ… 뢀뢄은 λ‹€ μ™„λ£Œλ˜μ—ˆλ‹€. μ΄μ œλŠ” ν•΄λ‹Ή λ‘œμ§μ„ 각각 μ‚¬μ΄νŠΈμ— λ§žμΆ°μ„œ μ‚¬μš©ν•΄μ£Όλ©΄ 데이터 μž‘μ—…μ€ 끝났닀. 이제 λ‹€μŒ κΈ€λ“€μ—μ„œλŠ” POI 라이브러리λ₯Ό μ‚¬μš©ν•΄μ„œ μ—‘μ…€ νŒŒμΌμ„ λ‹€μš΄ λ°›μ„λ•Œ μŠ€νƒ€μΌμ„ μž…νžˆκ±°λ‚˜ μˆ˜μ‹μ„ μ‚¬μš©ν•˜λŠ” λ‘œμ§μ„ κ΅¬ν˜„ν•΄λ³Ό μ˜ˆμ •μ΄λ‹€.

 

λŒ“κΈ€