package com.boying.util;
|
|
import com.boying.entity.DongHuanBaoJing;
|
import com.boying.entity.DongHuanZhuangTai;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* @author kdq
|
* @version 1.0.0
|
* @ClassName ExcelUtils.java
|
* @Description TODO
|
* @createTime 2023年06月19日 08:58:00
|
*/
|
public class ExcelUtils {
|
public static List<DongHuanZhuangTai> excelToShopIdList(InputStream inputStream,Integer type) throws IOException {
|
Workbook workbook = WorkbookFactory.create(inputStream);
|
inputStream.close();
|
// 在工作簿获取目标工作表
|
Sheet sheet = workbook.getSheetAt(0);
|
// 获取到最后一行
|
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
|
// 该集合用来储存行对象
|
ArrayList<DongHuanZhuangTai> detaileds = new ArrayList<>();
|
// 遍历整张表,从第二行开始,第一行的表头不要,循环次数不大于最后一行的值
|
for (int i = 1; i < physicalNumberOfRows; i++) {
|
// 该对象用来储存行数据
|
DongHuanZhuangTai detailed = new DongHuanZhuangTai();
|
// 获取当前行数据
|
Row row = sheet.getRow(i);
|
// 获取目标单元格的值并存进对象中
|
detailed.setOid(row.getCell(1).getStringCellValue());
|
detailed.setDeviceName(row.getCell(2).getStringCellValue());
|
detailed.setBianLiangName(row.getCell(3).getStringCellValue());
|
detailed.setValue(row.getCell(4).getStringCellValue());
|
detailed.setType(type);
|
// 把对象放到集合里
|
detaileds.add(detailed);
|
System.out.println("获取到的当前行数据:" + detailed);
|
}
|
return detaileds;
|
}
|
|
public static List<DongHuanBaoJing> excelToShopIdList2(InputStream inputStream, Integer type) throws IOException {
|
Workbook workbook = WorkbookFactory.create(inputStream);
|
inputStream.close();
|
// 在工作簿获取目标工作表
|
Sheet sheet = workbook.getSheetAt(0);
|
// 获取到最后一行
|
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
|
// 该集合用来储存行对象
|
ArrayList<DongHuanBaoJing> detaileds = new ArrayList<>();
|
// 遍历整张表,从第二行开始,第一行的表头不要,循环次数不大于最后一行的值
|
for (int i = 1; i < physicalNumberOfRows; i++) {
|
// 该对象用来储存行数据
|
DongHuanBaoJing detailed = new DongHuanBaoJing();
|
// 获取当前行数据
|
Row row = sheet.getRow(i);
|
// 获取目标单元格的值并存进对象中
|
detailed.setOid(row.getCell(1).getStringCellValue());
|
detailed.setDeviceName(row.getCell(2).getStringCellValue());
|
detailed.setBianLiangName(row.getCell(3).getStringCellValue());
|
detailed.setValue(row.getCell(4).getStringCellValue());
|
detailed.setType(type);
|
// 把对象放到集合里
|
detaileds.add(detailed);
|
System.out.println("获取到的当前行数据:" + detailed);
|
}
|
return detaileds;
|
}
|
}
|