版权声明:内容记录学习过成文章,仅供参考 https://blog.csdn.net/qq_40195958/article/details/82841557
导入依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
获得目录下的文件名
File file = new File("//localhost/Middle_Dir/bomtoerp/"+request.getParameter("filename"));
list = getAllExcel(file);//这里调用函数来获取每个目录下的Excel中数据
复制java代码
// 遍历获取Excel表格中的数据
public List<List<String>> getAllExcel(File file) throws FileNotFoundException, IOException, ClassNotFoundException,
InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException, POIXMLException {
List<List<String>> listt = new ArrayList<List<String>>();
try {
FileInputStream fis = new FileInputStream(file);
Workbook workbook = null;
workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
int skey = -1;
int pkey = -1;
for (int j = 2; j < sheet.getPhysicalNumberOfRows(); j++){ // 获取每行
List<String> list = new ArrayList<String>();
XSSFRow row = (XSSFRow) sheet.getRow(j);
if(row!=null){
boolean torf=false;
for (int k = 0; k < sheet.getRow(0).getPhysicalNumberOfCells(); k++){ // 获取每个单元格
Cell cell = row.getCell(k);
if (cell == null){
list.add("<td></td>");
continue;
}
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING:
String cellval = cell.getRichStringCellValue().getString();
if(0 == k){
list.add("<td>"+cellval+"</td>");
String str = PlmtoErpService.selectcinvinfo(cellval);//通过具体行数据查询
list.add("<td><span id='textclass"+(j-2)+'-'+k+"'>"+str+"</span></td>");
}else if(6 == k){
list.add("<td>"+cellval+"</td>");
String str2 = PlmtoErpService.selectcinvinfo(cellval);//通过规格获得编码
list.add("<td><span id='textclass"+(j-2)+'-'+(k+1)+"'>"+str2+"</span></td>");
}else {
list.add("<td>"+cellval+"</td>");
}
torf=true;
break;
default:list.add("<td></td>");
break;
}
}
if(torf){
listt.add(list);
}
}
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
return listt;
}