zoukankan      html  css  js  c++  java
  • Java操作excel表格

      用Java对excel后缀名为xlsx的文件进行简单的增,删,改,查操作。

      首先导入了一个poi.jar 网上有很多这个资源可以下载

     

     XSSFSheet sheet=null;
     XSSFWorkbook book=null;

    一:查  (查找本地指定位置的excel表格,在控制台输出) 

    public void print_excel(){
    
      //获取excel表格的行数
      int lastrownumber = sheet.getLastRowNum();
      String ret=" ";
      //获取数据
      for(a=0;a<lastrownumber;a++){
        XSSFRow row=sheet.getRow(a);
        //获取excel表格的列数
        int lastcellnum=row.getLastCellNum();
        for(b=0;b<lastcellnum;b++){
    
          XSSFCell cell =row.getCell(b);
    
          //判断cell返回的类型并赋值给ret
          ret=excel_operation.getExcelCellValue(cell);
          System.out.print(ret+" ");
        }
      System.out.println();
      } 
    }

    二:改 (修改excel表格中某一单元格的内容)

    public void set_excelcell(int i,int j,String str){
      //获取行的信息
      XSSFRow row=sheet.getRow(i-1);
      //获取列的信息
      XSSFCell cell =row.getCell(j-1);
      //获取被修改单元格的内容
      String string = excel_operation.getExcelCellValue(cell);
      //修改单元格的内容为str
      cell.setCellValue(str);
      System.out.println("已将"+string+"改为"+str);
    }

    三:增 (在excel表格中插入一行内容到指定位置)

      

    public  void insert(int rowIndex, String[] objs) { 
      if(rowIndex == 0) { 
        throw new IllegalArgumentException("不能插在第0行,第0行是用来定义的!"); 
      } 
      if(rowIndex > sheet.getLastRowNum() + 1) { 
        throw new IllegalArgumentException("最多只能插入在最后一行的后面。"); 
      } 
      int referRowIndex = -1; //参考行的行号。 
      if(sheet.getPhysicalNumberOfRows() <= 1) { 
    
        referRowIndex = rowIndex - 1; 
      } else { 
        referRowIndex = rowIndex - 1; 
      if(rowIndex == sheet.getLastRowNum() + 1) { //是插入最后一行 
        //不做任何处理 
      } else { 
    
        //往下移动一位 
        sheet.shiftRows(rowIndex, sheet.getLastRowNum(), 1, true, false); 
        } 
      } 
      Row targetRow = sheet.createRow(rowIndex); 
      Row referRow = sheet.getRow(referRowIndex); // 参考行 
      Cell targetCell, referCell; 
    
      for (int i = 0; i < objs.length; i++) { 
      targetCell = targetRow.createCell(i); 
      referCell = referRow.getCell(i); 
    
      targetCell.setCellStyle(referCell.getCellStyle()); 
      targetCell.setCellType(referCell.getCellType()); 
    
      targetCell.setCellValue(objs[i]);// 设置值 
      } 
    }

    四: 删 (删除指定行的内容)

    // 删除一行数据(Excel表中,行是从0起算的) 
      public  void delete(int rowIndex) {
    
      //删除的是最后一行  
      if(rowIndex == sheet.getLastRowNum()) { 
        sheet.removeRow(sheet.getRow(sheet.getLastRowNum()));
    
      //删除的不是最后一行  
      } else { 
        sheet.shiftRows(rowIndex + 1, sheet.getLastRowNum(), -1, true, false); 
        sheet.removeRow(sheet.getRow(sheet.getLastRowNum() + 1)); 
      } 
    }

    五: 判断返回类型  (因为excel表格中的内容不同,有字符型的,有整数型的等等,必须进行判断其类型才能进行输出)

    private static String getExcelCellValue(XSSFCell cell) {
      String ret=" ";
      try {
        //当返回值的类型为空返回空格
        if (cell == null) {
          ret = " "; 
        //当返回值的类型为字符串类型
        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
          ret = cell.getStringCellValue();
    
        //当返回值的类型为数值类型 
        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
          ret = "" + cell.getNumericCellValue();
    
        //当返回值的类型为表达式类型
        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
          ret = cell.getCellFormula();
    
        //当返回值的类型为异常类型
        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
          ret = " " + cell.getErrorCellValue();
    
        //当返回值的类型为布尔类型
         } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
          ret = " " + cell.getBooleanCellValue();
    
        //当返回值的类型为空的时候
        } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
        ret = " ";
        }
      } catch (Exception ex) {
        ex.printStackTrace();
      ret = " ";
      }
      return ret;
    }
  • 相关阅读:
    用AVIFile函数制做AVI文件基本步骤
    RHEL5下源码安装Mysql
    RHEL 6.2/i686配置使用CentOS YUM源
    教你选择最稳定的 MySQL 版本
    RHEL 6.2/i686桌面版解决风扇狂转的问题 安装官方闭源ATI显卡驱动
    Ubuntu 11.10下解决JUK播放MP3乱码的方法
    Ubuntu 10.04下SVN+Apache安装、配置与使用
    Ubuntu 11.10安装(卸载)ATI闭源驱动导致黑屏进不了系统的解决办法
    ubuntu 11.10下创建eclipse桌面快捷方式
    Ubuntu 11.10与Windows双系统的硬盘安装方法
  • 原文地址:https://www.cnblogs.com/0427mybirthday/p/4862740.html
Copyright © 2011-2022 走看看