星空网 > 软件开发 > Java

Java操作excel表格

  最近老师布置了个任务,用Javaexcel后缀名为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;
}




原标题:Java操作excel表格

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

中东返校季,沙特文具消费额达13.3亿美元:https://www.kjdsnews.com/a/541659.html
​9月如何规划亚马逊旺季?奉上3点建议:https://www.kjdsnews.com/a/541660.html
亚马逊卖家涌入东南亚,会带来什么变化?:https://www.kjdsnews.com/a/541661.html
Shopify红人营销怎么做,Shopify营销流程介绍:https://www.kjdsnews.com/a/541662.html
韩国——触手可及的跨境市场:https://www.kjdsnews.com/a/541663.html
你的物流成本为什么居高不下?试试这7个技巧!:https://www.kjdsnews.com/a/541664.html
海陵岛马尾岛景点介绍 海陵马尾岛图片:https://www.vstour.cn/a/363177.html
无锡旅游景点竹海 - 无锡的竹海:https://www.vstour.cn/a/363178.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流