POI解析EXCEL

本文介绍了如何在Java中通过Google Sheets API获取合并单元格的值,包括判断单元格是否为合并单元格以及获取合并单元格的具体实现。

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

Java代码 复制代码 收藏代码
  1. /**
  2. * 获取合并单元格的值
  3. * @param sheet
  4. * @param row
  5. * @param column
  6. * @return
  7. */
  8. public String getMergedRegionValue(Sheet sheet ,int row , int column){
  9. int sheetMergeCount = sheet.getNumMergedRegions();
  10. for(int i = 0 ; i < sheetMergeCount ; i++){
  11. CellRangeAddress ca = sheet.getMergedRegion(i);
  12. int firstColumn = ca.getFirstColumn();
  13. int lastColumn = ca.getLastColumn();
  14. int firstRow = ca.getFirstRow();
  15. int lastRow = ca.getLastRow();
  16. if(row >= firstRow && row <= lastRow){
  17. if(column >= firstColumn && column <= lastColumn){
  18. Row fRow = sheet.getRow(firstRow);
  19. Cell fCell = fRow.getCell(firstColumn);
  20. return getCellValue(fCell) ;
  21. }
  22. }
  23. }
  24. return null ;
  25. }
  26. /**
  27. * 判断指定的单元格是否是合并单元格
  28. * @param sheet
  29. * @param row
  30. * @param column
  31. * @return
  32. */
  33. public boolean isMergedRegion(Sheet sheet , int row , int column){
  34. int sheetMergeCount = sheet.getNumMergedRegions();
  35. for(int i = 0 ; i < sheetMergeCount ; i++ ){
  36. CellRangeAddress ca = sheet.getMergedRegion(i);
  37. int firstColumn = ca.getFirstColumn();
  38. int lastColumn = ca.getLastColumn();
  39. int firstRow = ca.getFirstRow();
  40. int lastRow = ca.getLastRow();
  41. if(row >= firstRow && row <= lastRow){
  42. if(column >= firstColumn && column <= lastColumn){
  43. return true ;
  44. }
  45. }
  46. }
  47. return false ;
  48. }
  49. /**
  50. * 获取单元格的值
  51. * @param cell
  52. * @return
  53. */
  54. public String getCellValue(Cell cell){
  55. if(cell == null) return "";
  56. if(cell.getCellType() == Cell.CELL_TYPE_STRING){
  57. return cell.getStringCellValue();
  58. }else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
  59. return String.valueOf(cell.getBooleanCellValue());
  60. }else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
  61. return cell.getCellFormula() ;
  62. }else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
  63. return String.valueOf(cell.getNumericCellValue());
  64. }
  65. return "";
  66. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值