poi操作excell

// 对于word和excell的不同操作,可以只需要上传部分的jar,操作excell上传的jar比较多
package com.zte.xh.fund.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.mysql.jdbc.StringUtils;

/**
 * 读取excell的工具类
 * 
 * @author Jay_Lee
 *
 */
public class ExcellUtil {
	private static XSSFWorkbook xssfWorkbook;
	private static HSSFWorkbook hssfWorkbook;

	/**
	 * 向外提供的调用接口,根据后缀名判断不同的excell文件调用不同方法
	 * 
	 * @param path
	 * @throws IOException
	 */
	public static List<Map<String, String>> startRead(String path)
			throws IOException {
		String fileType = path.substring(path.lastIndexOf(".") + 1,
				path.length());
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		if (fileType.equalsIgnoreCase("XLSX")) {
			result = readXlsx(path, findXlsxStartEnd(path));
		} else {
			result = readXls(path, findXlsStartEnd(path));
		}
		return result;
	}

	/**
	 * 找到xls文件的excell中有效数据的起始位置
	 * 
	 * @param path
	 * @throws IOException
	 */
	private static Map<String, Integer> findXlsStartEnd(String path)
			throws IOException {
		InputStream is = new FileInputStream("path");
		hssfWorkbook = new HSSFWorkbook(is);
		Map<String, Integer> resultNum = new HashMap<String, Integer>();
		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
			if (hssfSheet == null) {
				continue;
			}

			// 循环行Row
			for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
				HSSFRow hssfRow = hssfSheet.getRow(rowNum);
				if (hssfRow == null) {
					continue;
				}

				// 循环列Cell
				for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) {
					HSSFCell hssfCell = hssfRow.getCell(cellNum);
					if (hssfCell == null) {
						continue;
					}

					String value = getValueHssf(hssfCell).trim();
					// 如果这里读取出了账号,那么记录它的cell在每行的位置,以后直接读取下一行的此处
					if (value.equals("账号")) {
						resultNum.put("startRow", rowNum + 1);
						resultNum.put("startCell", cellNum);
					} else if (value.equals("说明")) {
						resultNum.put("endCell", cellNum);
						return resultNum;
					}
				}
			}
		}
		return null;
	}

	/**
	 * 读取xls的excell文件
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	private static List<Map<String, String>> readXls(String path, Map startMap)
			throws IOException {
		List<Map<String, String>> accounts = new ArrayList<Map<String, String>>();
		Map<String, String> tempMap = null;
		int startRow = (int) startMap.get("startRow");
		int startCell = (int) startMap.get("startCell");
		int endCell = (int) startMap.get("endCell");
		InputStream is = new FileInputStream("path");
		hssfWorkbook = new HSSFWorkbook(is);
		Map<String, Integer> resultNum = new HashMap<String, Integer>();
		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
			if (hssfSheet == null) {
				continue;
			}

			// 循环行Row
			for (int rowNum = startRow; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
				HSSFRow hssfRow = hssfSheet.getRow(rowNum);
				if (hssfRow == null) {
					continue;
				}
				tempMap = new HashMap<String, String>();
				// 循环列Cell
				for (int cellNum = startCell; cellNum <= endCell; cellNum++) {
					HSSFCell hssfCell = hssfRow.getCell(cellNum);
					String value = getValueHssf(hssfCell).trim();
					tempMap.put(String.valueOf(cellNum),
							StringUtils.isNullOrEmpty(value) ? "" : value);
				}
				accounts.add(tempMap);
			}
		}
		return accounts;
	}

	/**
	 * 读取cell中的不同类型的数据
	 * 
	 * @param hssfCell
	 * @return
	 */
	private static String getValueHssf(HSSFCell hssfCell) {
		if (hssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(hssfCell.getBooleanCellValue());
		} else if (hssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
			return String.valueOf(hssfCell.getNumericCellValue());
		} else {
			return String.valueOf(hssfCell.getStringCellValue());
		}
	}

	/**
	 * 找到Xlsx文件的excell中有效数据的起始位置
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	private static Map<String, Integer> findXlsxStartEnd(String path)
			throws IOException {
		xssfWorkbook = new XSSFWorkbook(path);
		Map<String, Integer> resultNum = new HashMap<String, Integer>();
		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
			XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
			if (xssfSheet == null) {
				continue;
			}

			// 循环行Row
			for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
				XSSFRow xssfRow = xssfSheet.getRow(rowNum);
				if (xssfRow == null) {
					continue;
				}

				// 循环列Cell
				for (int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++) {
					XSSFCell xssfCell = xssfRow.getCell(cellNum);
					if (xssfCell == null) {
						continue;
					}
					String value = getValueXssf(xssfCell).trim();
					// 如果这里读取出了账号,那么记录它的cell在每行的位置,以后直接读取下一行的此处
					if (value.equals("账号")) {
						resultNum.put("startRow", rowNum + 1);
						resultNum.put("startCell", cellNum);
					} else if (value.equals("说明")) {
						resultNum.put("endCell", cellNum);
						return resultNum;
					}
				}
			}
		}
		return null;
	}

	/**
	 * 通过起始位置读取xlsx中的数据
	 * 
	 * @param path
	 * @param startMap
	 * @return
	 * @throws IOException
	 */
	private static List<Map<String, String>> readXlsx(String path, Map startMap)
			throws IOException {
		List<Map<String, String>> accounts = new ArrayList<Map<String, String>>();
		Map<String, String> tempMap = null;
		InputStream is = new FileInputStream(path);
		xssfWorkbook = new XSSFWorkbook(path);
		int startRow = (int) startMap.get("startRow");
		int startCell = (int) startMap.get("startCell");
		int endCell = (int) startMap.get("endCell");
		// 循环工作表Sheet
		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
			XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
			if (xssfSheet == null) {
				continue;
			}
			// 循环行Row
			for (int rowNum = startRow; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
				XSSFRow xssfRow = xssfSheet.getRow(rowNum);
				if (xssfRow == null) {
					continue;
				}
				tempMap = new HashMap<String, String>();
				// 循环列Cell
				for (int cellNum = startCell; cellNum <= endCell; cellNum++) {
					XSSFCell xssfCell = xssfRow.getCell(cellNum);
					String value = getValueXssf(xssfCell).trim();
					tempMap.put(String.valueOf(cellNum),
							StringUtils.isNullOrEmpty(value) ? "" : value);
				}
				accounts.add(tempMap);
			}
		}
		return accounts;
	}

	@SuppressWarnings("static-access")
	private static String getValueXssf(XSSFCell xssfCell) {
		if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(xssfCell.getBooleanCellValue());
		} else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
			return String.valueOf(xssfCell.getNumericCellValue());
		} else {
			return String.valueOf(xssfCell.getStringCellValue());
		}
	}

	/**
	 * 从Excel中读取管理员信息的格式化方法
	 * 
	 * @param xssfCell
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static String getValueXssfFormat(XSSFCell xssfCell) {
		DecimalFormat df2 = new DecimalFormat("#");
		if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
			return String.valueOf(xssfCell.getBooleanCellValue());
		} else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
			return String.valueOf(df2.format(xssfCell.getNumericCellValue()));
		} else {
			return String.valueOf(xssfCell.getStringCellValue());
		}
	}

	/**
	 * 从Excel中读取管理员信息
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static List<String> readXlsxFormat(String path) throws IOException {
		List<String> list = new ArrayList<String>();
		XSSFWorkbook xssfWorkbook = new XSSFWorkbook(path);
		// 循环工作表Sheet
		for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
			XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
			if (xssfSheet == null) {
				continue;
			}

			// 循环行Row
			for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
				XSSFRow xssfRow = xssfSheet.getRow(rowNum);
				if (xssfRow == null) {
					continue;
				}

				// 循环列Cell
				for (int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++) {
					XSSFCell xssfCell = xssfRow.getCell(cellNum);
					if (xssfCell == null) {
						continue;
					}
					list.add(getValueXssfFormat(xssfCell));
					System.out.print("   " + getValueXssfFormat(xssfCell));
				}
				System.out.println();
			}
		}
		return list;
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String path = "f:" + File.separator + "checkAccount.xlsx";
		List<Map<String, String>> lists = startRead(path);
		Map<String, String> tem = lists.get(0);
		System.out.println(tem.toString());
		System.out.println(lists.size());
	}
}

你可能感兴趣的:(Excel)