1. 程式人生 > >Java Excel檔案上傳

Java Excel檔案上傳

需求,前端使用者上傳Excel檔案,後臺讀取解析檔案,做一系列處理後插入資料庫。

1.前臺程式碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>匯入</title>
</head>
<style>
    .fileinput-button input {
        position: absolute;
        right: 0px;
        top: 0px;
    }
</style> <body> <p>請上傳.xlsx或.xls格式Excel檔案</p> <hr/> <form id="importer" action="/api/improtExcel" method="post" enctype="multipart/form-data">   <span class=""> <span>上傳</span> <input type="file" name="excel"> </
span>   <input class="fileinput-button" type="submit" name="Submit" value="提交"/> </form> </body> </html>

2.controller

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @RestController @RequestMapping(value = "/excel", produces = "text/plain;charset=UTF-8") public class ExcelController { @PostMapping("/improt") public String improtExcel(HttpServletRequest request) throws IOException, InvalidFormatException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("excel"); xxxx(file);//執行業務程式碼 } }

3.讀取Excel

 
 
/*------------------------------------------------------ExcelUtil------------------------------------------------------------------------*/

//.xls和.xlsx格式的讀取方式不同,需要做一個判斷然後返回WorkBook物件
public
static Workbook readExcel(MultipartFile file) throws IOException { Workbook workbook = null; if (null == file) { throw new RuntimeException("File can not be null!"); } //判斷excel型別 .xls 或則 .xlsx格式 String fileName = file.getOriginalFilename(); String excelFormat = fileName.substring(fileName.lastIndexOf(".")); if (".xls".equals(excelFormat)) { workbook = new HSSFWorkbook(file.getInputStream()); } else if (".xlsx".equals(excelFormat)) { workbook = new XSSFWorkbook(file.getInputStream()); } else { throw new RuntimeException("File format can only be .xls or .xlsx"); } return workbook; }

/*------------------------------------------------------業務程式碼------------------------------------------------------------------------*/
//讀sheet、row
public String importExcel(MultipartFile file) throws IOException {
Workbook workbook = ExcelUtil.readExcel(file);
Sheet sheet = workbook.getSheetAt(0);//讀取第一個sheet
Row header = sheet.getRow(2);//讀取頭行資料
companyId = header.getCell(0).toString();
companeName = header.getCell(1).toString();
List<InsertInVo> insertInVos = new ArrayList<>();
for (int i = 4; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
String departmentNo = row.getCell(0).toString();//不應該這樣寫,容易出現nullpointerException,應該先判斷空
String departmentName = row.getCell(1).toString();
String projectNo = row.getCell(2).toString();
String projectName = row.getCell(3).toString();
String bookNo = row.getCell(4).toString();
String bookName = row.getCell(5).toString();
......}