java解析Excel文件

踩了很多坑就不说了,上干货

首先引入这两个依赖:
只需要这两个依赖就可以解析xls和xlsx文件

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>4.0.5</version>
</dependency>

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.16</version>
</dependency>

接收前端传过来的文件,前端怎么传可以看我另一篇博文:angularJS上传文件.

解析excel:
这边是这样的,alias这个map的键为上传的表格的标题行,值为自定义实体类的属性名,将标题行与属性一一对应,最后excelReader.readAll(ExcelOrderDTO.class)将表格每一行数据封装进结果集合中
ps:ExcelOrderDTO是用来存放表格内容的实体类

@ResponseBody
@PostMapping("/attachmentUpload")
public String parseExcel(MultipartFile file){
    try {
        InputStream fileInputStream = file.getInputStream();
        ExcelReader excelReader = ExcelUtil.getReader(fileInputStream);
        //自定义标题别名
        Map<String, String> alias = Maps.newLinkedHashMap();
        alias.put(ExcelConstants.NUM, "num");
        alias.put(ExcelConstants.PROPOSERNAME, "proposerName");
        alias.put(ExcelConstants.HOUSEHOLDREGION, "houseHoldName");
        alias.put(ExcelConstants.LOCALITYREGION, "localityName");
        alias.put(ExcelConstants.CARDNUM, "cardNum");
        alias.put(ExcelConstants.NOTARY, "notary");
        alias.put(ExcelConstants.USE_COUNTRY, "use_country");
        alias.put(ExcelConstants.LANGUAGE, "language");
        alias.put(ExcelConstants.AUTH_NUM, "auth_num");
        alias.put(ExcelConstants.USE, "use");
        alias.put(ExcelConstants.REMARK, "remark");
        excelReader.setHeaderAlias(alias);
        List<ExcelOrderDTO> list = excelReader.readAll(ExcelOrderDTO.class);
        return "success";
    } catch (IOException e) {
            SYS_LOGGER.error("excel解析异常",e);
            return "failure";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用Apache POI库来解析Excel文件。Apache POI是一个开源的Java库,提供了读取、写入和操作Microsoft Office格式文件(如Excel、Word和PowerPoint)的功能。 要解析Excel文件,首先需要导入Apache POI库的相关依赖。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 接下来,可以使用以下代码示例来解析Excel文件: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; public class ExcelParser { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("path/to/excel/file.xlsx"); Workbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { System.out.print(cell.getStringCellValue() + "\t"); } else if (cellType == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } else if (cellType == CellType.BOOLEAN) { System.out.print(cell.getBooleanCellValue() + "\t"); } } System.out.println(); } workbook.close(); file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码使用FileInputStream来读取Excel文件,然后创建XSSFWorkbook对象表示整个Excel文件。通过getSheetAt方法获取第一个Sheet,然后使用两个嵌套的循环遍历每一行和每一个单元格。根据单元格的类型,可以使用getCellType方法获取单元格的值。 请注意,上述代码示例假设Excel文件的第一个Sheet是要解析的目标。如果需要解析其他Sheet,可以使用getSheet方法指定Sheet的名称或索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值