java读取含有合并单元格的Excel 您所在的位置:网站首页 java导出合并单元格的内容 java读取含有合并单元格的Excel

java读取含有合并单元格的Excel

#java读取含有合并单元格的Excel| 来源: 网络整理| 查看: 265

Excel如下:

工具类:

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.*; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.util.CellRangeAddress; /** * Created by handsome on 2016/7/11. */ public class ExcelUtil { public static void main(String[] args){ ExcelUtil excelUtil = new ExcelUtil(); //读取excel数据 List result = excelUtil.readExcelToObj("C:\\Users\\miracle\\Desktop\\合并单元格.xlsx"); for(Map map:result){ System.out.println(map); } } /** * 读取excel数据 * @param path */ private List readExcelToObj(String path) { Workbook wb = null; List result = null; try { wb = WorkbookFactory.create(new File(path)); result = readExcel(wb, 0, 1, 0); } catch (InvalidFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 读取excel文件 * @param wb * @param sheetIndex sheet页下标:从0开始 * @param startReadLine 开始读取的行:从0开始 * @param tailLine 去除最后读取的行 */ private List readExcel(Workbook wb,int sheetIndex, int startReadLine, int tailLine) { Sheet sheet = wb.getSheetAt(sheetIndex); Row row = null; List result = new ArrayList(); for(int i=startReadLine; i= firstRow && row = firstColumn && column = firstColumn && column = firstRow && row = firstColumn && column 0 ? true : false; } /** * 合并单元格 * @param sheet * @param firstRow 开始行 * @param lastRow 结束行 * @param firstCol 开始列 * @param lastCol 结束列 */ private void mergeRegion(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) { sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); } /** * 获取单元格的值 * @param cell * @return */ public String getCellValue(Cell cell){ if(cell == null) return ""; if(cell.getCellType() == Cell.CELL_TYPE_STRING){ return cell.getStringCellValue(); }else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){ return String.valueOf(cell.getBooleanCellValue()); }else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){ return cell.getCellFormula() ; }else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ return String.valueOf(cell.getNumericCellValue()); } return ""; } /** * 从excel读取内容 */ public static void readContent(String fileName) { boolean isE2007 = false; //判断是否是excel2007格式 if(fileName.endsWith("xlsx")) isE2007 = true; try { InputStream input = new FileInputStream(fileName); //建立输入流 Workbook wb = null; //根据文件格式(2003或者2007)来初始化 if(isE2007) wb = new XSSFWorkbook(input); else wb = new HSSFWorkbook(input); Sheet sheet = wb.getSheetAt(0); //获得第一个表单 Iterator rows = sheet.rowIterator(); //获得第一个表单的迭代器 while (rows.hasNext()) { Row row = rows.next(); //获得行数据 System.out.println("Row #" + row.getRowNum()); //获得行号从0开始 Iterator cells = row.cellIterator(); //获得第一行的迭代器 while (cells.hasNext()) { Cell cell = cells.next(); System.out.println("Cell #" + cell.getColumnIndex()); switch (cell.getCellType()) { //根据cell中的类型来输出数据 case HSSFCell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: System.out.println(cell.getCellFormula()); break; default: System.out.println("unsuported sell type======="+cell.getCellType()); break; } } } } catch (IOException ex) { ex.printStackTrace(); } } }

执行结果如下:

{score=10, student=张三, course=语文, class=一(1)班} {score=20, student=李四, course=语文, class=一(1)班} {score=30, student=王五, course=语文, class=一(1)班} {score=40, student=赵六, course=数学, class=一(1)班} {score=50, student=田七, course=数学, class=一(1)班} {score=60, student=周扒皮, course=数学, class=一(1)班}

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有