java代码读取excel文件,同时兼容2003和2007

来源:互联网 发布:oracle数据库主键类型 编辑:程序博客网 时间:2024/06/05 12:01
package org.framework.core.util;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.io.PushbackInputStream;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.poi.POIXMLDocument;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDateUtil;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.openxml4j.opc.OPCPackage;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ExcelRead {public static Workbook create(InputStream inp) throws Exception {if (!inp.markSupported()) {        inp = new PushbackInputStream(inp, 8);    }    if (POIFSFileSystem.hasPOIFSHeader(inp)) {        return new HSSFWorkbook(inp);    }    if (POIXMLDocument.hasOOXMLHeader(inp)) {        return new XSSFWorkbook(OPCPackage.open(inp));    }    throw new IllegalArgumentException("你的excel版本目前poi解析不了");}/** * 通过流读取Excel文件 * @param stream * @return * @throws Exception */public static Map<String, List<List<Map<Integer,String>>>> getExcelDataByStream(String streamPath) throws Exception {try {Map<String, List<List<Map<Integer,String>>>> data = new HashMap<String, List<List<Map<Integer,String>>>>();Workbook book = create(new BufferedInputStream(new FileInputStream(streamPath)));if (null == book.getSheetAt(0)) {return null;}List<Map<Integer,String>> rowData = null;// 每一行的数据List<List<Map<Integer,String>>> sheetData = null;// 每个SHEET的数据String sheetName = null;// 遍历Excel得到SHEETfor (int i = 0; i < book.getNumberOfSheets(); i++) {Sheet sheet = book.getSheetAt(i);// 获取SHEET的名字sheetName = sheet.getSheetName();sheetData = new ArrayList<List<Map<Integer,String>>>();// 遍历SHEET得到每一行for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {Row row = sheet.getRow(rowNum);if (null == row) {continue;}// 再遍历该行的所有列rowData = new ArrayList<Map<Integer,String>>();for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {Cell cell = row.getCell(cellNum);if (null == cell) {continue;}String cellVal = getCellVal(cell);Map<Integer,String> map = new HashMap<Integer,String>();map.put(cellNum, cellVal);rowData.add(map);}// 如上遍历后rowData中填充了每一行的数据,将其加入到sheet中sheetData.add(rowData);}// 有了SHEET名字和SHEET中的数据了,以SHEET名字为键值,加入到MAP中data.put(sheetName, sheetData);}return data;} catch (Exception e) {e.printStackTrace();}return null;}private static String getCellVal(Cell cell) {if (null == cell) {return "";}switch (cell.getCellType()) {// 数字case HSSFCell.CELL_TYPE_NUMERIC:// 日期格式的处理if (HSSFDateUtil.isCellDateFormatted(cell)) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString();}return String.valueOf(cell.getNumericCellValue());// 字符串case HSSFCell.CELL_TYPE_STRING:return cell.getStringCellValue();// 公式case HSSFCell.CELL_TYPE_FORMULA:return cell.getCellFormula();// 空白case HSSFCell.CELL_TYPE_BLANK:return "";case HSSFCell.CELL_TYPE_BOOLEAN:return cell.getBooleanCellValue() + "";// 错误类型case HSSFCell.CELL_TYPE_ERROR:return cell.getErrorCellValue() + "";default:break;}return "";}}

阅读全文
0 0
原创粉丝点击