java解析excel文件处理数字成像_Java处理Excel工具,POI解析,使用InputStream读取文件...

本文介绍了如何使用Java的POI库解析Excel文件,特别是处理数字成像的情况。通过示例代码展示了如何读取Excel的InputStream,获取工作簿,遍历行和单元格,以及根据单元格类型转换数据。示例中,程序从上传的Excel文件中读取工作站的名称、IP和描述,并将数值型单元格转换为字符串。
摘要由CSDN通过智能技术生成

标签:

一、需要导入的jar包

org.apache.poi

poi

3.9

org.apache.poi

poi-ooxml

3.9

二、POIAPI连接

三、代码如下

/**

* 获取上传的excel 解析数据

*

* @param file

* 文件

* @param excelName

* 文件名

* @return

* @throws IOException

* @throws InvalidFormatException

*/

@PostMapping("/readexcel")

public List uploadExcel(@RequestParam MultipartFile file, @RequestParam String excelName) throws IOException, InvalidFormatException {

List dataList = null;

try (InputStream in = file.getInputStream()) {

Workbook wb = WorkbookFactory.create(in);

// 获取第一个sheet

Sheet sheet = wb.getSheetAt(0);

// 获取最大行数

int rownum = sheet.getPhysicalNumberOfRows();

// 获取第一行

Row row = sheet.getRow(0);

// 存放表中的数据

dataList = new ArrayList();

// 循环行

for (int i = 1; i < rownum; i++) {

WorkstationExcel we = new WorkstationExcel();

row = sheet.getRow(i);

if (row != null) {

we.name = getCellFormatValue(row.getCell(0));

we.ip = getCellFormatValue(row.getCell(1));

we.description = getCellFormatValue(row.getCell(2));

} else {

continue;

}

System.err.println("名称:" + we.name + "------" + "IP:" + we.ip + "------" + "描述:" + we.description);

dataList.add(we);

}

}

return dataList;

}

public String getCellFormatValue(Cell cell) {

String cellValue = "";

if (cell == null) {

return cellValue;

}

// 判断cell类型 getCellType()

switch (cell.getCellType()) {

case Cell.CELL_TYPE_NUMERIC: {

// 获取单元格的值作为数字 getNumericCellValue()

cellValue = String.valueOf((int)cell.getNumericCellValue());

break;

}

case Cell.CELL_TYPE_FORMULA: {

// 判断cell是否为日期格式

if (DateUtil.isCellDateFormatted(cell)) {

// 转换为日期格式YYYY-mm-dd

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date d = cell.getDateCellValue();

if (d != null) {

cellValue = sdf.format(d);

}

} else {

// 数字

cellValue = String.valueOf(cell.getNumericCellValue());

}

break;

}

case Cell.CELL_TYPE_STRING: {

cellValue = cell.getRichStringCellValue().getString();

break;

}

}

return cellValue;

}

@SuppressWarnings("unused")

private class WorkstationExcel {

/**

* 工作站名 如 001

*/

public String name;

/**

* 工作站ip

*/

public String ip;

/**

* 描述

*/

public String description;

}

四、Excel表如下

名称

IP

描述

GYG4

127.0.0.33

sfs

DFSA

127.0.46.3

fsd

ADAS

172.26.6.15

dfd

WSFS

153.6.5.23

dgs

五、获取数据结果

120ae0cf976d519eeac3c61e85476833.png

标签:

来源: https://blog.csdn.net/yellow757/article/details/82712855

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值