POI读写excel和pojo互转,兼容office的1997和2010

本文介绍了使用Java POI库进行Excel读写操作,并展示了如何将Excel内容转换为POJO对象,同时也支持不同版本的Office文件格式,包括1997和2010。
package tool.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.RichTextString;
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;

/**
 * excel 1997 2010读写
 * 
 * @author 陈小稳1983 qq33881270
 *
 */
public class ExcelTool {

	/**
	 * excel行转换为 列名、属性名
	 * 
	 * @param line
	 * @return
	 */
	public static String[] lineToTitles(String line) {
		String[] arr = line.split("\t");
		return arr;
	}

	/**
	 * 读取excel文件为相应的pojo对象列表
	 * 
	 * @param path
	 *            文件路径
	 * @param sheetName
	 *            表格名称。表格名称、表格索引 任选其一。如果此项为null则会根据“表格索引”读取
	 * @param sheetIndex
	 *            表格索引。表格名称、表格索引 任选其一。如果此项为null则会根据“表格索引”=0读取
	 * @param titles
	 *            要读取的表头的列的标题。必填。据此决定读取哪些列
	 * @param titleRowNum
	 *            表头所在行。null表示默认从0(也就是第一行)开始
	 * @param ignoralAllBlankLineData
	 *            忽略取值列全空的数据。null表示默认true
	 * @param cls
	 *            excel的数据要转换为的类
	 * @param attrs
	 *            对应列标题的cls类的属性名称
	 * @return
	 * @throws IOException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static <E> List<E> readAllFormatExcelFileToBeanList(String path, String sheetName, Integer sheetIndex,
			String[] titles, Integer titleRowNum, Boolean ignoralAllBlankLineData, Class<E> cls, String[] attrs) {
		if (titles.length != attrs.length) {
			throw new RuntimeException("the size of titles and attrs not same !");
		}
		Field[] fields = cls.getFields();
		for (int i = 0; i < attrs.length; i++) {
			String attr = attrs[i];
			try {
				checkAttrExists(cls, fields, attr);
			} catch (NoSuchMethodException e) {
				throw new RuntimeException("attr \"" + attr + "\" not exists in class: " + cls.getName());
			}
		}

		ArrayList<HashMap<String, String>> rows = ExcelTool.readAllFormatExcelFile(path, sheetName, sheetIndex, titles,
				titleRowNum, ignoralAllBlankLineData);
		List<E> beans = ExcelTool.toBeanList(rows, cls, titles, attrs);
		return beans;
	}

	/**
	 * 检查类是否有某个属性
	 * 
	 * @param cls
	 *            类
	 * @param fields
	 *            类的字段(公共的)
	 * @param attr
	 *            待检查属性名
	 * @throws NoSuchMethodException
	 */
	private static <E> void checkAttrExists(Class<E> cls, Field[] fields, String attr) throws NoSuchMethodException {
		Boolean has = false;
		for (int j = 0; j < fields.length; j++) {
			String name = fields[j].getName();
			if (attr.equals(name)) {
				has = true;
				break;
			}
		}
		if (!has) {
			String name = (attr.charAt(0) + "").toUpperCase() + attr.substring(1);
			try {
				String methodName = "get" + name;
				cls.getMethod(methodName);
			} catch (NoSuchMethodException e) {
				try {
					String methodName = "is" + name;
					cls.getMethod(methodName);
				} catch (NoSuchMethodException e2) {
					throw e2;
				}
			}
		}
	}

	/**
	 * 读取的结果转换为bean
	 * 
	 * @param rows
	 *            读取的excel结果
	 * @param cls
	 *            结果包括bean的类型
	 * @param titles
	 *            excel标题s
	 * @param attrs
	 *            excel标题对应的属性s,必须和 titles 一致
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static <E> List<E> toBeanList(ArrayList<HashMap<String, String>> rows, Class<E> cls, String[] titles,
			String[] attrs) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值