springboot实现导入excel

springboot实现excel数据导入

controller

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xxx.common.annotation.ValidationParam;
import com.xxx.common.base.ResponseHelper;
import com.xxx.common.base.ResponseModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Api(tags={"xx接口"})
@RestController
@RequestMapping("/Xx")
public class XxController {

	@ApiOperation(value = "",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "body", dataType = "Integer"),
            @ApiImplicitParam(name = "file", value = "导入文件", paramType = "body", dataType = "File")
    })
    @PostMapping("/importFile")
    public ResponseModel importFile(@RequestParam(value = "id")Integer robotId, @RequestParam(value = "file") MultipartFile file)throws Exception{
        File file1 = null;
        file1 = File.createTempFile("temp", null);
        file.transferTo(file1);
        InputStream ips = new FileInputStream(file1);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(ips);
        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
        // 获得总行数
        int rowNum = sheet.getLastRowNum();
        // 获取sheet的总行数
        int totalRows = sheet.getPhysicalNumberOfRows();
        // 获取该行的总列数
        int RowCells=sheet.getRow(0).getPhysicalNumberOfCells();
        // 读取单元格内容
        for (int i = 0; i <= rowNum; i++) {
        	// 读取行
            XSSFRow row = sheet.getRow(i);
            // 设置单元格格式
            row.getCell(0).setCellType(CellType.STRING);
            row.getCell(1).setCellType(CellType.NUMERIC);
            // 读取单元格内容
            String s = row.getCell(0).getStringCellValue();
            double d = row.getCell(1).getNumericCellValue();
        }
        //具体处理过程省略
    	return ResponseHelper.succeed(true);
    }
}

pom.xml文件

<properties>
	<poi.version>3.15</poi.version>
</properties>
<dependencyManagement>
	<dependencies>
	<dependency>
		<groupId>org.apache.poi</groupId>
    	<artifactId>poi-ooxml</artifactId>
		<version>${poi.version}</version>
	</dependency>
	<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi-ooxml-schemas</artifactId>
		<version>${poi.version}</version>
	</dependency>
</dependencyManagement>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache POI 库来实现 Spring Boot 中的复杂导入 Excel。具体步骤如下: 1. 引入 Apache POI 依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建 Excel 文件读取器: ```java public class ExcelReader { public static List<Map<String, Object>> readExcel(InputStream inputStream, String fileName) throws IOException { Workbook workbook = null; if (fileName.endsWith(".xls")) { workbook = new HSSFWorkbook(inputStream); } else if (fileName.endsWith(".xlsx")) { workbook = new XSSFWorkbook(inputStream); } Sheet sheet = workbook.getSheetAt(0); List<Map<String, Object>> list = new ArrayList<>(); List<String> titles = new ArrayList<>(); for (int i = 0; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) { continue; } Map<String, Object> map = new HashMap<>(); for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); if (cell == null) { continue; } if (i == 0) { titles.add(cell.getStringCellValue()); } else { map.put(titles.get(j), getCellValue(cell)); } } if (i != 0) { list.add(map); } } return list; } private static Object getCellValue(Cell cell) { Object value = null; switch (cell.getCellType()) { case STRING: value = cell.getStringCellValue(); break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { value = cell.getDateCellValue(); } else { value = cell.getNumericCellValue(); } break; case BOOLEAN: value = cell.getBooleanCellValue(); break; case FORMULA: value = cell.getCellFormula(); break; default: break; } return value; } } ``` 3. 在 Controller 中使用 ExcelReader 读取 Excel 文件: ```java @PostMapping("/import") public String importExcel(@RequestParam("file") MultipartFile file) throws IOException { List<Map<String, Object>> list = ExcelReader.readExcel(file.getInputStream(), file.getOriginalFilename()); // 处理 Excel 数据 return "success"; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值