博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java分享第十七天-02(封装操作excel类)
阅读量:6487 次
发布时间:2019-06-24

本文共 4060 字,大约阅读时间需要 13 分钟。

 java解析EXCEL

用的是POI的JAR包,兼容EXCEL2003及2007+版本的EXCEL
所需要的JAR包:
poi-3.8.jar
poi-ooxml.jar
poi-ooxml-schemas.jar
xmlbeans.jar
代码如下:

public class ExcelReader {    private String filePath;    private String sheetName;    private Workbook workBook;        private Sheet sheet;    private List
columnHeaderList; private List
> listData; private List
> mapData; private boolean flag; public ExcelReader(String filePath, String sheetName) { this.filePath = filePath; this.sheetName = sheetName; this.flag = false; this.load(); } private void load() { FileInputStream inStream = null; try { inStream = new FileInputStream(new File(filePath)); workBook = WorkbookFactory.create(inStream); sheet = workBook.getSheet(sheetName); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(inStream!=null){ inStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } private String getCellValue(Cell cell) { String cellValue = ""; DataFormatter formatter = new DataFormatter(); if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { cellValue = formatter.formatCellValue(cell); } else { double value = cell.getNumericCellValue(); int intValue = (int) value; cellValue = value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value); } break; case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: cellValue = String.valueOf(cell.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: cellValue = ""; break; case Cell.CELL_TYPE_ERROR: cellValue = ""; break; default: cellValue = cell.toString().trim(); break; } } return cellValue.trim(); } private void getSheetData() { listData = new ArrayList
>(); mapData = new ArrayList
>(); columnHeaderList = new ArrayList
(); int numOfRows = sheet.getLastRowNum() + 1; for (int i = 0; i < numOfRows; i++) { Row row = sheet.getRow(i); Map
map = new HashMap
(); List
list = new ArrayList
(); if (row != null) { for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); if (i == 0){ columnHeaderList.add(getCellValue(cell)); } else{ map.put(columnHeaderList.get(j), this.getCellValue(cell)); } list.add(this.getCellValue(cell)); } } if (i > 0){ mapData.add(map); } listData.add(list); } flag = true; } public String getCellData(int row, int col){ if(row<=0 || col<=0){ return null; } if(!flag){ this.getSheetData(); } if(listData.size()>=row && listData.get(row-1).size()>=col){ return listData.get(row-1).get(col-1); }else{ return null; } } public String getCellData(int row, String headerName){ if(row<=0){ return null; } if(!flag){ this.getSheetData(); } if(mapData.size()>=row && mapData.get(row-1).containsKey(headerName)){ return mapData.get(row-1).get(headerName); }else{ return null; } } public static void main(String[] args) { ExcelReader eh = new ExcelReader("E:\\workspace\\test.xls","Sheet1"); System.out.println(eh.getCellData(1, 1)); System.out.println(eh.getCellData(1, "test1")); } }

 

转载于:https://www.cnblogs.com/tiancy/p/6065468.html

你可能感兴趣的文章
Java文件清单列表
查看>>
[LeetCode] Reverse String 翻转字符串
查看>>
学习iOS【3】数组、词典和集合
查看>>
Hessian 原理分析--转
查看>>
转: 基于netty+ protobuf +spring + hibernate + jgroups开发的游戏服务端
查看>>
easyui传入map的数据前台展示出tree格式数据
查看>>
悲观的思考,乐观的生活.我们既需要思考的深度,也需要生活的温度!
查看>>
Vitamio中文API文档(4)—— VitamioInstaller
查看>>
yii框架常用url地址
查看>>
python3.4学习笔记(十六) windows下面安装easy_install和pip教程
查看>>
MyGUI 解析
查看>>
Linux中的ls命令详细使用
查看>>
graph-tool文档(一)- 快速开始使用Graph-tool - 2.属性映射、图的IO和Price网络
查看>>
GraphicsLab Project之辉光(Glare,Glow)效果 【转】
查看>>
Linux Curl命令
查看>>
-27979 LoadRunner 错误27979 找不到请求表单 Action.c(73): Error -27979: Requested form not found...
查看>>
[LeetCode] Minimum Depth of Binary Tree
查看>>
,net运行框架
查看>>
Java 中 Emoji 的正则表达式
查看>>
Mixin Network第一届开发者大赛作品介绍- dodice, diceos和Fox.one luckycoin
查看>>