controller
前端读取的key值为 @RequestParam("file") 中的参数(本文为file)
//导入模板
@ApiOperation("导入模板")
@PostMapping(value = "/importFile")
@AnonymousAccess
public List<Map<String,Object>> importFile(@RequestParam("file") MultipartFile file) throws IOException {
return fileService.importFile(file);
}
}
service
public List<Map<String,Object>> importFile(MultipartFile file) throws IOException {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = null;
if (fileName != null) {
prefix = fileName.substring(fileName.lastIndexOf("."));
}
// 生成临时文件
File file = null;
if (fileName != null) {
file = File.createTempFile(fileName, prefix);
}
// 将传过来的文件及内容复制到file临时文件中
if (file != null) {
multipartFile.transferTo(file);
}
// 读取数据字节
InputStream inputStream = new FileInputStream(file);
// 读取整个Excel
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
// 获取想要的表单Sheet
//从0开始,i就写i-1
sheets.getSheetAt(i-1)
}
封装成额外的方法
// excel转化流式文件
public InputStream fileConventIO(MultipartFile multipartFile) throws IOException {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = null;
if (fileName != null) {
prefix = fileName.substring(fileName.lastIndexOf("."));
}
// 生成临时文件
File file = null;
if (fileName != null) {
file = File.createTempFile(fileName, prefix);
}
// 将传过来的文件及内容复制到file临时文件中
if (file != null) {
multipartFile.transferTo(file);
}
// 读取数据字节
InputStream inputStream = new FileInputStream(file);
return inputStream;
}
通过行列确定具体值
sheetAt 就是上边的 sheets.getSheetAt(i-1)
sheetAt.getRow(row).getCell(cell) 取到当前页具体某一个数据
//找第几行第几列的方法
//sheetAt为第几个sheet页
//row:行 从0开始
//cell:列 从0开始
public String getSheetValue(XSSFSheet sheetAt, int row, int cell) {
return getStringValue(sheetAt.getRow(row).getCell(cell));
}
//封装值转换成String的方法
public String getStringValue(XSSFCell getCell) {
String value = null;
if (getCell == null || "".equals(getCell.toString())) return value;
switch (getCell.getCellType()) {
case STRING: //string类型
value = getCell.toString();
break;
case NUMERIC: //数字类型
value = NumberToTextConverter.toText(getCell.getNumericCellValue());
break;
default:
break;
}
return value;
}
//封装值转换成BigDecimal的方法 --- 传具体单元格里的值
public BigDecimal getBigDecimalValue(XSSFCell getCell) {
String value;
if (getCell == null || "".equals(getCell.toString())) {
value = "0";
} else {
value = getStringValue(getCell);
}
return new BigDecimal(value);
}
//封装值转换成BigDecimal的方法 --- 传String类型
public BigDecimal getBigDecimalValue(String str) {
String value;
if (str == null || "".equals(str)) {
value = "0";
} else {
value = str;
}
return new BigDecimal(value);
}
//封装值转换成Integer的方法 --- 传具体单元格里的值
public Integer getIntegerValue(XSSFCell getCell){
String value;
if (getCell == null || "".equals(getCell.toString())) {
value = "0";
} else {
value = getStringValue(getCell);
}
return new Integer(value);
}
//封装值转换成Integer的方法 --- 传String类型
public Integer getIntegerValue(String str){
String value;
if (str == null || "".equals(str)) {
value = "0";
} else {
value = str;
}
return new Integer(value);
}
循环取行列
sheetAt.getPhysicalNumberOfRows() 一共多少行
XSSFRow row = sheetAt.getRow(i) 当前行
sheetAt.getRow(0).getLastCellNum() 一共多少列(默认第一行列 >= 其他行)
public String[][] getArrTwo(XSSFSheet sheetAt) {
String[][] arr;
//第一行为空时判断为空表
if (sheetAt.getRow(0) == null) {
return arr= null;
}
//创建二维数组
arr = new String[sheetAt.getPhysicalNumberOfRows()][sheetAt.getRow(0).getLastCellNum()];
// 循环获取每一行数据
for (int i = 0; i < sheetAt.getPhysicalNumberOfRows(); i++) {
XSSFRow row = sheetAt.getRow(i);
//循环获取每一列的值
for (int j = 0; j < row.getLastCellNum(); j++) {
arr[i][j] = getStringValue(row.getCell(j));
}
}
return arr;
}
整体
public List<Map<String,Object>> importBJFL(MultipartFile file) throws IOException {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = null;
if (fileName != null) {
prefix = fileName.substring(fileName.lastIndexOf("."));
}
// 生成临时文件
File file = null;
if (fileName != null) {
file = File.createTempFile(fileName, prefix);
}
// 将传过来的文件及内容复制到file临时文件中
if (file != null) {
multipartFile.transferTo(file);
}
// 读取数据字节
InputStream inputStream = new FileInputStream(file);
// 读取整个Excel
XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
// 创建返回对象
List<Map<String,Object>> list = new ArrayList()<>;
//从excel每个sheet页中获取数据
for (int i = 0; i <= 12; i++) {
//工程概况及编制说明
if (i == 0) list.add(get101(sheets.getSheetAt(i)));
//工程项目标价分离汇总表
if (i == 1) list.add(get113(sheets.getSheetAt(i)));
//指标表
if (i == 2) list.add(get114(sheets.getSheetAt(i)));
}
return list;
}
/**
* 101
*/
public Map<String,Object> get101(XSSFSheet sheetAt) {
Map<String,Object> map = new HashMap<>();
//项目名称
map.put("101",getSheetValue(sheetAt, 0, 1));
//人员编制
map.put("101",getSheetValue(sheetAt, 19, 2));
return map;
}
/**
* 102
*/
public Map<String,Object> get102(XSSFSheet sheetAt) {
Map<String,Object> map = new HashMap<>();
//循环每一行
for (int i = 2; i < sheetAt.getPhysicalNumberOfRows(); i++) {
XSSFRow row = sheetAt.getRow(i);
//合计及序号
map.put("102",getStringValue(row.getCell(0)));
//成本科目
map.put("102",getStringValue(row.getCell(1)));
}
return map;
}
/**
* 103
*/
public Map<String,Object> get103(XSSFSheet sheetAt) {
String[][] arr;
//第一行为空时判断为空表
if (sheetAt.getRow(0) == null) {
return arr = null;
}
//创建二维数组
arr = new String[sheetAt.getPhysicalNumberOfRows()][sheetAt.getRow(0).getLastCellNum()];
// 循环获取每一行数据
for (int i = 0; i < sheetAt.getPhysicalNumberOfRows(); i++) {
XSSFRow row = sheetAt.getRow(i);
//循环获取每一列的值
for (int j = 0; j < row.getLastCellNum(); j++) {
arr[i][j] = getStringValue(row.getCell(j));
}
}
return map.put("103",arr);
}
/**
* 指定单元格查询的方法
*
* @param sheetAt sheet页名称
* @param row 行
* @param cell 列
* @return
*/
public String getSheetValue(XSSFSheet sheetAt, int row, int cell) {
return getStringValue(sheetAt.getRow(row).getCell(cell));
}
//封装值转换成BigDecimal的方法
public BigDecimal getBigDecimalValue(XSSFCell getCell) {
String value;
if (getCell == null || "".equals(getCell.toString())) {
value = "0";
} else {
value = getStringValue(getCell);
}
return new BigDecimal(value);
}
//封装值转换成BigDecimal的方法
public BigDecimal getBigDecimalValue(String str) {
String value;
if (str == null || "".equals(str)) {
value = "0";
} else {
value = str;
}
return new BigDecimal(value);
}
//封装值转换成Integer的方法
public Integer getIntegerValue(XSSFCell getCell){
String value;
if (getCell == null || "".equals(getCell.toString())) {
value = "0";
} else {
value = getStringValue(getCell);
}
return new Integer(value);
}
//封装值转换成Integer的方法
public Integer getIntegerValue(String str){
String value;
if (str == null || "".equals(str)) {
value = "0";
} else {
value = str;
}
return new Integer(value);
}
//封装值转换成String的方法
public String getStringValue(XSSFCell getCell) {
String value = null;
if (getCell == null || "".equals(getCell.toString())) return value;
switch (getCell.getCellType()) {
case STRING:
value = getCell.toString();
break;
case NUMERIC:
value = NumberToTextConverter.toText(getCell.getNumericCellValue());
break;
// case FORMULA:
// value = String.valueOf(getCell.getNumericCellValue());
// break;
default:
break;
}
return value;
}