栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java poi

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java poi

这里写目录标题
  • 一、java poi依赖引入
  • 二、使用步骤
    • 1.进行写操作
    • 2.进行读操作(简单)
    • 3.批量读取

一、java poi依赖引入
    
      org.apache.poi
      poi
      4.1.0
    
    
      org.apache.poi
      poi-ooxml
      4.1.0
    
    
      org.apache.poi
      poi-ooxml-schemas
      4.1.0
    

    
      joda-time
      joda-time
      2.10.1
    
二、使用步骤 1.进行写操作
  static String PATH="d:\image\";
  public static  void testWriteTest() throws IOException {
    //1.创建一个工作簿 最多65546 写入缓存,最后一次写入磁盘,速度快 03版本
   // Workbook workbook=new HSSFWorkbook();
    //07以后写入速度慢
   // Workbook workbook=new XSSFWorkbook();
    //07版以后的  优化后的 先写入临时文件(最后需要清理临时文件) 大文件读写速度快
    Workbook workbook=new SXSSFWorkbook();
    //2.创建一个工作表
    Sheet sheet=workbook.createSheet("狂神观众统计表");
    //3.创建一个行
    Row row1=sheet.createRow(0);
    //4.创建一个单元格
    Cell cell11=row1.createCell(0);
    cell11.setCellValue("今日新增观众");
    //(1,2)
    Cell cell12=row1.createCell(1);
    cell12.setCellValue("666");

    //第二行(2,1)
    Row row2=sheet.createRow(1);
    Cell cell21=row2.createCell(0);
    cell21.setCellValue("统计时间");

    //(2,1)
    Cell cell22=row2.createCell(1);
    String time=new DateTime().toString("yyyy-MM-dd HH:mm:ss");
    cell22.setCellValue(time);
    FileOutputStream fileOutputStream = null;
    try {
      fileOutputStream=new FileOutputStream(PATH+"统计表.xlsx");

      workbook.write(fileOutputStream);

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if(fileOutputStream!=null){
        fileOutputStream.close();
      }
    }
  }
2.进行读操作(简单)
    //获取文件流
    FileInputStream inputStream=new FileInputStream("d:\image\统计表.xlsx");

    //1.创建一个工作簿,使得excel可以操作
    Workbook workbook1=new XSSFWorkbook(inputStream);
    //得到表
    Sheet sheet=workbook1.getSheetAt(0);
    //得到行
    Row row=sheet.getRow(0);
    //得到(0,0)表格
    Cell cell=row.getCell(0);
    //读取值得时候要注意类型
    System.out.println(cell.getStringCellValue());

    inputStream.close();
3.批量读取

匹配读取到表格内容的东西,然后转化位String返回

  private String matchTheDataType(Cell cell) {
    String cellValue="";
    //匹配数据类型
    if(cell !=null){
      CellType cellType= cell.getCellType();
      switch (cellType){
        case STRING:
          cellValue= cell.getStringCellValue();
          break;
        case _NONE:
          break;
        case BLANK://如果为空
          break;
        case NUMERIC:
          if(HSSFDateUtil.isCellDateFormatted(cell)){
            //如果是日期
            Date date= cell.getDateCellValue();
            cellValue=new DateTime(date).toString();
          }else {
            //普通数字
            cellValue= cell.toString();
          }
        case FORMULA:
          break;
        case BOOLEAN:
          cellValue=String.valueOf(cell.getBooleanCellValue());
          break;
        case ERROR:
          System.out.println("数据类型错误");
          break;

      }
    }
    return cellValue;
  }

批量处理

  public void testCellType() throws Exception{
    //获取文件流
    FileInputStream fileInputStream=new FileInputStream("");
    
    //创建一个工作簿
    Workbook workbook=new HSSFWorkbook(fileInputStream);
    Sheet sheet=workbook.getSheetAt(0);
    //处理表头
    Row rowTitle=sheet.getRow(0);
    if(rowTitle!=null){
      //掌握
      int cellCount=rowTitle.getPhysicalNumberOfCells();
      for (int i = 0; i < cellCount; i++) {
        Cell cell=rowTitle.getCell(i);
        if(cell!=null){
          CellType cellType=cell.getCellType();

        }
      }
    }

    //获取表中得位置
    int rowCount=sheet.getPhysicalNumberOfRows();
    for(int rowNum=1;rowNum 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/581880.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号