【Java】--Comparison of import and export of POI, EasyExcel, etc.

I. Introduction

In many old projects, we often use the POI framework to import and export Excel data, but this method has major shortcomings: the POI framework is complex to use, the sax parsing mode is not easy to operate, and large amounts of data are prone to OOM, POI There are problems such as concurrent error reporting.
EasyExce, an open source excel processing framework from Alibaba, is known for its simplicity of use and memory saving. This project implements the import of list data, export of paginated lists, supports custom formats, supports templates and mixed export of templates and lists. Her advantage is that it solves the problems of the POI method: it is loaded by decompressing the file, loading line by line, and discarding unimportant data such as style fonts and reducing memory usage. .

2. POI import and export

1. Code case

jar package reference dependencies

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

Import tool class

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.

Guess you like

Origin blog.csdn.net/xunmengyou1990/article/details/130179273