• Aapche POI java excel 操作工具包入门


    POI

    Apache POI - the Java API for Microsoft Documents

    poi

    quick-start

    Hello World

    • jar
    
        org.apache.poi
        poi
        ${poi.version}
    
    
        org.apache.poi
        poi-ooxml
        ${poi-ooxml.version}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • get first 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);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 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;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • get excel content --> 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();
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    本文由博客一文多发平台 OpenWrite 发布!

  • 相关阅读:
    使用tc命令模拟linux网络延迟环境
    Spring中的注解01
    OD_2024_C卷_200分_1、爱吃蟠桃的孙悟空【JAVA】【二分法】
    飞书即时消息无需API开发连接Cohere,打造飞书AI智能问答助手
    基于图的 Affinity Propagation 聚类计算公式详解和代码示例
    pnpm的安装与使用
    【工程数学】笔记1:复变函数和积分变换
    Linux 常用命令
    机器学习(一)
    SaaSBase:什么是SAP(思爱普) ERP?
  • 原文地址:https://blog.csdn.net/ryo1060732496/article/details/136293065