poi-ooxml 导入Excle

这篇博客介绍了如何利用Apache POI的poi-ooxml库读取Excel文件。首先,文章提到了需要导入对应的Maven依赖。接着,展示了如何获取指定目录下Excel文件的名称。最后,详细阐述了一个Java方法,该方法遍历Excel表格的每一行,获取并处理单元格数据,用于展示或进一步处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入依赖

```

<!-- 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;
     }

```

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值