java导入excel表格数据
作者:互联网
package com.tt.rhms.sys.controller;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Author xl
* 导入Excel数据
*/
@RestController
public class DownloadExcel {
@ResponseBody//返回json数据
@RequestMapping(value = "/lz/excelImport", method = RequestMethod.POST)
public String uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
if (file.isEmpty()) {
return "文件为空!";
}
try {
//根据路径获取这个操作excel的实例
HSSFWorkbook sheets = new HSSFWorkbook(file.getInputStream());
//根据页面index获取sheet页
HSSFSheet sheet = sheets.getSheetAt(0);
//实体类集合
HSSFRow row = null;
//循环sesheet页中数据从第二行开始,第一行是标题
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
//获取每一行数据
row = sheet.getRow(i);
String id = row.getCell(0).getStringCellValue();
String name = row.getCell(1).getStringCellValue();
String date = row.getCell(2).getStringCellValue();
String age = row.getCell(3).getStringCellValue();
System.out.println(id+""+name+""+date+""+age+"");
//入库操作
}
} catch (IOException e) {
e.printStackTrace();
return "导入失败";
}
return "导入成功";
}
}
//表格数据
//postman请求
标签:java,String,getCell,excel,导入,org,import,sheet,row 来源: https://www.cnblogs.com/xuhanhan/p/14103139.html