Aapche POI java excel 操作工具包入门

excel 系列

Excel Export 踩坑注意点+导出方案设计

基于 hutool 的 EXCEL 优化实现

iexcel-excel 大文件读取和写入,解决 excel OOM 问题-01-入门介绍

iexcel-excel 大文件读取和写入-02-Excel 引导类简介

iexcel-excel 大文件读取和写入-03-@ExcelField 注解介绍

iexcel-excel 大文件读取和写入-04-order 指定列顺序

iexcel-excel 大文件读取和写入-05-file bytes 获取文件字节信息

Aapche POI java excel 操作工具包入门

Hello World

maven 导入

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <verison>${poi.version}</verison>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <verison>${poi-ooxml.version}</verison>
</dependency>

获取Excel第一个Sheet

实现如下:

/**
* 获取Excel第一个Sheet
* @param file excel文件
* @param fileSuffix  excel类型 xls/xlsx
*/
public static Sheet getFirstSheet(File file, String fileSuffix) throws IOException {
    InputStream stream = new FileInputStream(file);
    Workbook wb = null;
    if (fileSuffix.equals("xls")) {
      wb = new HSSFWorkbook(stream);
    } else if (fileSuffix.equals("xlsx")) {
      wb = new XSSFWorkbook(stream);
    }
    return wb.getSheetAt(0);
}
  • get cell value
/**
* 根据列类型,获得对应的String类型
* @return 不存在/不支持的类型,则返回""
*/
public static String getCellValueStr(Cell cell, String dateFormatStr) {
    String cellValueStr = "";
    if (null != cell) {
      Object cellValue = null;
      switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
          cellValueStr = cell.getRichStringCellValue().getString();
          break;
        case Cell.CELL_TYPE_NUMERIC:
          if (DateUtil.isCellDateFormatted(cell)) {
            cellValue= cell.getDateCellValue();
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormatStr);
            cellValueStr = formatter.format(cellValue);
          } else {
            cellValue=cell.getNumericCellValue();
            cellValueStr = String.valueOf(cellValue);
          }
          break;
        case Cell.CELL_TYPE_BOOLEAN:
          cellValue = cell.getBooleanCellValue();
          cellValueStr = String.valueOf(cellValue);
          break;
        case Cell.CELL_TYPE_FORMULA:
          cellValue = cell.getCellFormula();
          cellValueStr = String.valueOf(cellValue);
          break;
        default:
          System.out.println("不支持的excel单元格类型");
      }
    }
    return cellValueStr;
}

获取 Excel 的内容

获取 Excel 的内容,并且处理为 CSV

/**
* 获取Excel工作区的文件内容 - 字符串形式
* - 需要置换excel每列的数据(除了每行的结束)以外所有换行符 "\n"
* - 所有CEll都视为String类型
*/
public static String getSheetContent(Sheet sheet, String charset) throws UnsupportedEncodingException {
    StringBuffer stringBuffer = new StringBuffer();
    String dateTimeFormatStr = "yyyy-MM-dd HH:mm:ss";
    String lineSeparator = System.getProperty("line.separator", "\n");  //换行符

    for(Row row : sheet) {
      for(Cell cell : row) {
        cell.setCellType(Cell.CELL_TYPE_STRING);  //全部以String类型读取
        String cellStr = new String(getCellValueStr(cell, dateTimeFormatStr).getBytes(), charset);
        String trimCellStr = cellStr.replaceAll(lineSeparator, StringUtils.EMPTY);
        stringBuffer.append(trimCellStr).append(",");
      }

      //此行有内容
      if(row.getFirstCellNum() != CommonConstant.INVALID_NUMBER) {
        stringBuffer.deleteCharAt(stringBuffer.lastIndexOf(","));  //最后一个“,”
        stringBuffer.append(lineSeparator);
      }
    }

    return stringBuffer.toString();
}

参考资料

poi

quick-start

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值