Java Excel文件上传

Posted 我叫普朗台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
......}

 





















以上是关于Java Excel文件上传的主要内容,如果未能解决你的问题,请参考以下文章

从excel表格读取数据用Java代码实现批量上传写入数据库

直接把excel文件上传到服务器,然后用java后台语言读入excel,求完整的代码,最好是能提供一个实例,嘻嘻

求excel上传到服务器的java代码

Java Excel文件上传

Java 上传解压zip文件,并且解析文件里面的excel和图片

如何用ABAP代码实现上传本地excel文件,并将其作为附件发邮件