springboot - 웹 페이지에서 가져온 excel 파일 데이터를 백그라운드로 처리합니다.

26285 단어 springboot
웹 페이지에 excel 파일을 백엔드에 업로드하고 백엔드에서 데이터를 추출하고 처리합니다
controller 레이어 UploadExcelController 클래스
package com.example.demo.controller;

import com.example.demo.service.UploadExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.List;

@Controller
public class UploadExcelController {

    @Autowired
    private UploadExcelService uploadExcelService;

    @PostMapping(value = "/upload")
    @ResponseBody
    public String uploadExcel(HttpServletRequest request) throws Exception{
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;

        MultipartFile file = multipartHttpServletRequest.getFile("filename");

        if (file.isEmpty()) {
            return " ";
        }

        InputStream in = file.getInputStream();
        List<List<Object>> lists = uploadExcelService.getBankListByExcel(in,file.getOriginalFilename());

        in.close();

        for (int i=0; i<lists.size(); i++) {
            List<Object> list = lists.get(i);
			// , 
            System.out.println("mac:" + list.get(0)+" cpuId:"+list.get(1));
        }
        return " ";
    }
}


서비스 계층 UploadExcelService 클래스
package com.example.demo.service;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class UploadExcelService {

    public List getBankListByExcel(InputStream in, String fileName) throws Exception{
        List list = new ArrayList();

        // excel 
        Workbook work = this.getWorkbook(in,fileName);
        if (work == null) {
            throw new Exception(" Excel !");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;

        for (int i=0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if (sheet == null) {
                continue;
            }

            for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if (row == null || row.getFirstCellNum() == j) {
                    continue;
                }

                List<Object> li = new ArrayList<>();
                for (int k = row.getFirstCellNum(); k < row.getLastCellNum(); k++) {
                    cell = row.getCell(k);
                    li.add(cell);
                }
                list.add(li);
            }
        }
        work.close();
        return list;
    }

    public Workbook getWorkbook(InputStream inStr, String fileName) throws Exception{
        Workbook workbook = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));

        if (".xls".equals(fileType)) {
            workbook = new HSSFWorkbook(inStr);
        } else if (".xlsx".equals(fileType)) {
            workbook = new XSSFWorkbook(inStr);
        } else {
            throw  new Exception(" excel !");
        }
        return workbook;
    }

}


웹 층 index.html

<html>
<head>
    <meta charset="UTF-8"/>
    <title> title>
head>
<body>
<form method="post" enctype="multipart/form-data" id="form" action="/upload">
    <input type="file" name="filename"/>
    <input type="submit" value=" "/>
form>

body>
html>

전체 데모 다운로드
블로그 참조:https://www.cnblogs.com/zhenghengbin/p/9490511.html

좋은 웹페이지 즐겨찾기