---
title: "POI解析EXCEL"
author: "aazham"
date: "2012-01-13T17:54:40Z"
tags: []
categories: ["研发管理"]
slug: "poi-excel"
draft: false
lastmod: "2012-01-18T09:29:52Z"
id: 1345016
url: "https://aazham.iteye.com/blog/1345016"
---

Java代码 [![复制代码](http://zhoupuyue.iteye.com/images/icon_copy.gif)](http://zhoupuyue.iteye.com/blog/1136255 "复制代码") [![收藏代码](http://zhoupuyue.iteye.com/images/icon_star.png)![](http://zhoupuyue.iteye.com/images/spinner.gif)]()

01. /\**
02. \* 获取合并单元格的值
03. \* @param sheet
04. \* @param row
05. \* @param column
06. \* @return
07. \*/
08. **public** String getMergedRegionValue(Sheet sheet ,**int** row , **int** column){
09. **int** sheetMergeCount = sheet.getNumMergedRegions();
10. **for**(**int** i = 0 ; i &lt; 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 &gt;= firstRow && row &lt;= lastRow){
17. **if**(column &gt;= firstColumn && column &lt;= 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 &lt; 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 &gt;= firstRow && row &lt;= lastRow){
42. **if**(column &gt;= firstColumn && column &lt;= 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. }