Java实现操作excel表格(基于poi.jar)

本文介绍使用Java操作Excel的方法,包括读取、修改、插入和删除数据等核心功能,并提供具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

XSSFSheet sheet=null;
XSSFWorkbook book=null;

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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表格中某一单元格的内容)

?
1
2
3
4
5
6
7
8
9
10
11
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表格中插入一行内容到指定位置)

?
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
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]); // 设置值
  }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
// 删除一行数据(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表格中的内容不同,有字符型的,有整数型的等等,必须进行判断其类型才能进行输出)

?
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
36
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值