All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.memfactory.utils.ExcelUtil Maven / Gradle / Ivy

There is a newer version: 0.0.25
Show newest version
package com.memfactory.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

/**
 * Excel工具类
 * 
 * @author zhairp createDate: 2018-09-13
 */
public final class ExcelUtil {
	private ExcelUtil() {
	};

	/**
	 * 
	 * 读取Excel表格数据 createDate: 2018-09-13 
	 * 从代码不难发现其处理逻辑: 
	 * 1.先用InputStream获取excel文件的io流
	 * 2.然后穿件一个内存中的excel文件HSSFWorkbook类型对象,这个对象表示了整个excel文件。
	 * 3.对这个excel文件的每页做循环处理
	 * 4.对每页中每行做循环处理 
	 * 5.对每行中的每个单元格做处理,获取这个单元格的值 
	 * 6.把这行的结果添加到一个List数组中
	 * 7.把每行的结果添加到最后的总结果中 
	 * 8.解析完返回结果对象
	 * @param path Excel文件类路径
	 * @return Excel表格数据
	 */
	public static List> readXls(String path) {
		try {
			//InputStream is = new FileInputStream(path);//本地操作
			InputStream is = ExcelUtil.class.getClassLoader().getResourceAsStream(path);
			if (is == null) {
				is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
			}
			// HSSFWorkbook 标识整个excel
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
			List> result = new ArrayList>();
			int size = hssfWorkbook.getNumberOfSheets();
			// 循环每一页,并处理当前循环页
			for (int numSheet = 0; numSheet < size; numSheet++) {
				// HSSFSheet 标识某一页
				HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
				if (hssfSheet == null) {
					continue;
				}
				// 处理当前页,循环读取每一行
				for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
					// HSSFRow表示行
					HSSFRow hssfRow = hssfSheet.getRow(rowNum);
					int minColIx = hssfRow.getFirstCellNum();
					int maxColIx = hssfRow.getLastCellNum();
					List rowList = new ArrayList();
					// 遍历改行,获取处理每个cell元素
					for (int colIx = minColIx; colIx < maxColIx; colIx++) {
						// HSSFCell 表示单元格
						HSSFCell cell = hssfRow.getCell(colIx);
						if (cell == null) {
							continue;
						}
						rowList.add(getStringVal(cell));
					}
					result.add(rowList);
				}
			}
			return result;
		}catch(IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 *  createDate: 2018-09-13
	 * 改造poi默认的toString()方法如下: 
	 * 1.对于不熟悉的类型,或者为空则返回""控制串
	 * 2.如果是数字,则修改单元格类型为String,然后返回String,这样就保证数字不被格式化了
	 * 
	 * @param cell
	 * @return
	 */
	private static String getStringVal(HSSFCell cell) {
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_BOOLEAN:
			return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
		case Cell.CELL_TYPE_FORMULA:
			return cell.getCellFormula();
		case Cell.CELL_TYPE_NUMERIC:
			cell.setCellType(Cell.CELL_TYPE_STRING);
			return cell.getStringCellValue();
		case Cell.CELL_TYPE_STRING:
			return cell.getStringCellValue();
		default:
			return "";
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy