【Java】--POI、EasyExcel等导入导出对比

一、前言

在很多老项目中,我们经常通过POI框架来进行Excel数据的导入和导出,但这种方式存在很大的缺点:poi框架使用复杂,sax解析模式不容易操作,数据量大起来容易OOM,POI存在并发报错等问题。
现有阿里巴巴开源的一个excel处理框架EasyExce,以使用简单、节省内存著称。该项目实现了列表数据的导入,分页列表导出,支持自定义格式,支持模版以及模板和列表的混合导出。她的优点是解决了POI方式存在的问题:通过解压文件的方式加载,一行一行地加载,并且抛弃样式字体等不重要的数据,降低内存的占用。。

二、POI方式导入导出

1、代码案例

jar包引用依赖

	<!-- poi依赖 -->
	<dependency>
		<groupId>org.apache.poi</groupId>
		<artifactId>poi</artifactId>
		<version>3.14</version>
	</dependency>

导入工具类

public class ImportExcel {
   
    
    
	private POIFSFileSystem fs;
	private HSSFWorkbook wb;
	private HSSFSheet sheet;
	private HSSFRow row;

	/**
	 * 读取Excel表格表头的内容
	 * 
	 * @param is
	 * @return String 表头内容的数组
	 */
	public String[] readExcelTitle(InputStream is) {
   
    
    
		try {
   
    
    
			fs = new POIFSFileSystem(is);
			wb = new HSSFWorkbook(fs);
		} catch (IOException e) {
   
    
    
			e.printStackTrace();
		}
		sheet = wb.getSheetAt(0);
		// 得到首行的row
		row = sheet.getRow(0);
		// 标题总列数
		int colNum = row.getPhysicalNumberOfCells();
		String[] title = new String[colNum];
		for (int i = 0; i < colNum; i++) {
   
    
    
			title[i] = getCellFormatValue(row.getCell((short) i));
		}
		return title;
	}

	/**
	 * 读取Excel数据内容
	 * 
	 * @param is
	 * @return Map 包含单元格数据内容的Map对象
	 */
	public Map<Integer, String> readExcelContent(InputStream is) {
   
    
    
		Map<Integer, String> content = new HashMap<Integer, String>();
		String str = "";
		try {
   
    
    
			fs = new POIFSFileSystem(is);
			wb = new HSSFWorkbook(fs);
		} catch (IOException e) {
   
    
    
			e.printStackTrace();
		}
		sheet = wb.getSheetAt(0);
		// 得到总行数
		int rowNum = sheet.getLastRowNum();
		//
		row = sheet.getRow(1);
		int colNum = row.getPhysicalNumberOfCells();
		// 正文内容应该从第二行开始,第一行为表头的标题
		for (int i = 1; i <= rowNum; i++) {
   
    
    
			row = sheet.getRow(i);
			int j = 0;
			while (j < colNum) {
   
    
    
				str += getCellFormatValue(row.getCell((short) j)).trim() + "+";
				j++;
			}
			content.put(i, str);
			str = "";
		}
		return content;
	}

	/**
	 * 获取单元格数据内容为字符串类型的数据
	 *
	 * @param cell
	 *            Excel单元格
	 * @return String 单元格数据内容
	 */
	private String getStringCellValue(HSSFCell cell) {
   
    
    
		String strCell = "";
		switch (cell.getCellType()) {
   
    
    
		case HSSFCell.CELL_TYPE_STRING:
			strCell = cell.getStringCellValue();
			break;
		case HSSFCell.CELL_TYPE_NUMERIC:
			strCell = String.valueOf(cell.getNumericCellValue());
			break;
		case HSSFCell.CELL_TYPE_BOOLEAN:
			strCell = String.valueOf(cell.getBooleanCellValue());
			break;
		case HSSFCell.CELL_TYPE_BLANK:
			strCell = "";
			break;
		default:
			strCell = "";
			break;
		}
		if (strCell.equals("") || strCell == null) {
   
    
    
			return "";
		}
		if (cell == null) {
   
    
    
			return "";
		}
		return strCell;
	}

	/**
	 * 获取单元格数据内容为日期类型的数据
	 *
	 * @param cell
	 *            Excel单元格
	 * @return String 单元格数据内容
	 */
	private String getDateCellValue(HSSFCell cell) {
   
    
    
		String result = "";
		try {
   
    
    
			int cellType = cell.getCellType();
			if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
   
    
    
				Date date = cell.getDateCellValue();
				result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate();
			} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
   
    
    
				String date = getStringCellValue(cell);
				result = date.replaceAll("[年月]", "-").replace("日", "").trim();
			} else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
   
    
    
				result = "";
			}
		} catch (Exception e) {
   
    
    
			System.out.println("日期格式不正确!");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 根据HSSFCell类型设置数据
	 * 
	 * @param cell
	 * @return
	 */
	private String getCellFormatValue(HSSFCell cell) {
   
    
    
		String cellvalue = "";
		if (cell != null) {
   
    
    
			// 判断当前Cell的Type
			switch (cell.

猜你喜欢

转载自blog.csdn.net/xunmengyou1990/article/details/130179273