首页 > 代码库 > java解析EXCEL
java解析EXCEL
http://www.cnblogs.com/zhangfei/p/4171014.html
用的是POI的JAR包,兼容EXCEL2003及2007+版本的EXCEL
所需要的JAR包:
poi-3.8.jar
poi-ooxml.jar
poi-ooxml-schemas.jar
xmlbeans.jar
代码如下:
1 package main.java.commUtils; 2 3 import org.apache.poi.ss.usermodel.*; 4 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.IOException; 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 14 public class ExcelReader { 15 private String filePath; 16 private String sheetName; 17 private Workbook workBook; 18 private Sheet sheet; 19 private List<String> columnHeaderList; 20 private List<List<String>> listData; 21 private List<Map<String, String>> mapData; 22 private boolean flag; 23 24 public ExcelReader(String filePath, String sheetName) { 25 this.filePath = filePath; 26 this.sheetName = sheetName; 27 this.flag = false; 28 this.load(); 29 } 30 31 private void load() { 32 FileInputStream inStream = null; 33 try { 34 inStream = new FileInputStream(new File(filePath)); 35 workBook = WorkbookFactory.create(inStream); 36 sheet = workBook.getSheet(sheetName); 37 } catch (Exception e) { 38 e.printStackTrace(); 39 } finally { 40 try { 41 if (inStream != null) { 42 inStream.close(); 43 } 44 } catch (IOException e) { 45 e.printStackTrace(); 46 } 47 } 48 } 49 50 private String getCellValue(Cell cell) { 51 String cellValuehttp://www.mamicode.com/= ""; 52 DataFormatter formatter = new DataFormatter(); 53 if (cell != null) { 54 switch (cell.getCellType()) { 55 case Cell.CELL_TYPE_NUMERIC: 56 if (DateUtil.isCellDateFormatted(cell)) { 57 cellValue =http://www.mamicode.com/ formatter.formatCellValue(cell); 58 } else { 59 double value =http://www.mamicode.com/ cell.getNumericCellValue(); 60 int intValue = http://www.mamicode.com/(int) value; 61 cellValue = http://www.mamicode.com/value - intValue == 0 ? String.valueOf(intValue) : String.valueOf(value); 62 } 63 break; 64 case Cell.CELL_TYPE_STRING: 65 cellValue =http://www.mamicode.com/ cell.getStringCellValue(); 66 break; 67 case Cell.CELL_TYPE_BOOLEAN: 68 cellValue =http://www.mamicode.com/ String.valueOf(cell.getBooleanCellValue()); 69 break; 70 case Cell.CELL_TYPE_FORMULA: 71 cellValue =http://www.mamicode.com/ String.valueOf(cell.getCellFormula()); 72 break; 73 case Cell.CELL_TYPE_BLANK: 74 cellValuehttp://www.mamicode.com/= ""; 75 break; 76 case Cell.CELL_TYPE_ERROR: 77 cellValuehttp://www.mamicode.com/= ""; 78 break; 79 default: 80 cellValue =http://www.mamicode.com/ cell.toString().trim(); 81 break; 82 } 83 } 84 return cellValue.trim(); 85 } 86 87 private void getSheetData() { 88 listData = http://www.mamicode.com/new ArrayList<List<String>>(); 89 mapData = http://www.mamicode.com/new ArrayList<Map<String, String>>(); 90 columnHeaderList = new ArrayList<String>(); 91 int numOfRows = sheet.getLastRowNum() + 1; 92 for (int i = 0; i < numOfRows; i++) { 93 Row row = sheet.getRow(i); 94 Map<String, String> map = new HashMap<String, String>(); 95 List<String> list = new ArrayList<String>(); 96 if (row != null) { 97 for (int j = 0; j < row.getLastCellNum(); j++) { 98 Cell cell = row.getCell(j); 99 if (i == 0) { 100 columnHeaderList.add(getCellValue(cell)); 101 } else { 102 map.put(columnHeaderList.get(j), this.getCellValue(cell)); 103 } 104 list.add(this.getCellValue(cell)); 105 } 106 } 107 if (i > 0) { 108 mapData.add(map); 109 } 110 listData.add(list); 111 112 } 113 flag = true; 114 } 115 116 public String getCellData(int row, int col) { 117 if (row <= 0 || col <= 0) { 118 return null; 119 } 120 if (!flag) { 121 this.getSheetData(); 122 } 123 if (listData.size() >= row && listData.get(row - 1).size() >= col) { 124 return listData.get(row - 1).get(col - 1); 125 } else { 126 return null; 127 } 128 } 129 130 public String getCellData(int row, String headerName) { 131 if (row <= 0) { 132 return null; 133 } 134 if (!flag) { 135 this.getSheetData(); 136 } 137 if (mapData.size() >= row && mapData.get(row - 1).containsKey(headerName)) { 138 return mapData.get(row - 1).get(headerName); 139 } else { 140 return null; 141 } 142 } 143 144 145 public static void main(String[] args) { 146 ExcelReader eh = new ExcelReader("C:\\Users\\ty\\Documents\\工作簿1.xlsx", "Sheet2"); 147 System.out.println(eh.getCellData(1, 1)); 148 // System.out.println(eh.getCellData(1, "test1")); 149 System.out.println(eh.getCellData(1, 1)); 150 eh.getSheetData(); 151 152 } 153 }
java解析EXCEL
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。