博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EXCEL 读取
阅读量:6511 次
发布时间:2019-06-24

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

  hot3.png

xlsx2007 和 xls2003 两种格式需要使用两种方法XSSF      HSSF大数据excel读取参考 http://www.iteye.com/topic/624969poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.jar xmlbeans-2.3.0.jar小数据excel读取
package com.aibi.cmdc.webService;import java.io.FileInputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ExcelUtil {	public static String getCellValue(Cell cell) {		if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {			// 返回布尔类型的值			return String.valueOf(cell.getBooleanCellValue());		} else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {			// 返回数值类型的值			return String.valueOf(cell.getNumericCellValue());		} else {			// 返回字符串类型的值			return String.valueOf(cell.getStringCellValue());		}	}	public static List
> readExcel(String pathName) { List
> list = new ArrayList
>(); List
keys = new ArrayList
(); try { InputStream is = new FileInputStream(pathName); XSSFWorkbook wk = new XSSFWorkbook(is); XSSFSheet hssfSheet = wk.getSheetAt(0);// 取得 第一个sheet页 XSSFRow titleRow = hssfSheet.getRow(4);// 取得表头 for (int cellIndex = 0; cellIndex < titleRow.getLastCellNum(); cellIndex++) { Cell cell = titleRow.getCell(cellIndex); if (cell != null) { String cellValue = getCellValue(cell); System.out.println(cellValue); keys.add(cellValue); } } // 循环行Row for (int rowNum = 4; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { XSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } Map
rowMap = new HashMap
(); for (int i = 0; i < hssfRow.getLastCellNum(); i++) { Cell cell = hssfRow.getCell(i); if (cell != null) { rowMap.put(keys.get(i), getCellValue(cell)); } } list.add(rowMap); } } catch (Exception e) { e.printStackTrace(); } return list; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // String pathName ="e:\\11.xlsx"; String pathName = "e:\\aa2.xlsx"; List
> list = readExcel(pathName); System.out.println(list); }}

  

转载于:https://my.oschina.net/sbcagf/blog/783000

你可能感兴趣的文章