像这样的excel格式,我导入excel时,怎么去解析这个文件?
如果利用正常的方式去读取单元格,只能读取合并行中的第一行的数据,其他行为空。
所以必须要知道合并行是从哪行开始,哪行结束,这些行的值都去读取这个合并行中的第一行数据。
采取的方式如下:
/** * 合并单元格处理,获取合并行 * @param sheet * @return List<CellRangeAddress> */ public List<CellRangeAddress> getCombineCell(Sheet sheet) { List<CellRangeAddress> list = new ArrayList<CellRangeAddress>(); //获得一个 sheet 中合并单元格的数量 int sheetmergerCount = sheet.getNumMergedRegions(); //遍历合并单元格 for(int i = 0; i<sheetmergerCount;i++) { //获得合并单元格加入list中 CellRangeAddress ca = sheet.getMergedRegion(i); list.add(ca); } return list; } /** * 判断单元格是否为合并单元格,是的话则将单元格的值返回 * @param listCombineCell 存放合并单元格的list * @param cell 需要判断的单元格 * @param sheet sheet * @return */ public String isCombineCell(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet) throws Exception { int firstC = 0; int lastC = 0; int firstR = 0; int lastR = 0; String cellValue = null; for(CellRangeAddress ca:listCombineCell) { //获得合并单元格的起始行, 结束行, 起始列, 结束列 firstC = ca.getFirstColumn(); lastC = ca.getLastColumn(); firstR = ca.getFirstRow(); lastR = ca.getLastRow(); if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR) { if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC) { Row fRow = sheet.getRow(firstR); Cell fCell = fRow.getCell(firstC); cellValue = getCellValue(fCell); break; } } else { cellValue = ""; } } return cellValue; }
利用POI插件导入excel怎样读取合并行数据?,布布扣,bubuko.com
原文:http://blog.csdn.net/sunling_sz/article/details/21705671