JXL读写Excel

前几天由于需要写了个jxl读写excel类,示例代码如下:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pm.commons.CommdbUtil;
import com.pm.commons.FinalData;
import com.pm.commons.Format;
import com.pm.entity.DaifaVO;
import com.pm.hibernate.po.PayCashInfo;
import com.pm.hibernate.po.Payjnls;
import com.pm.hibernate.po2.AcctxnBook;
import com.pm.report.bean.LiquidationBean;
import com.pm.structure.dao.AcctxnBookDao;
import com.pm.structure.dao.PayCashInfoDao;

/**
 * Excel工具类
 *
 */
public class ExcelUtil {
	
	private static Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
	/** 手机号提款 */
	public static final String SERVCODE_100017 = "100017";
	/** 转账 */
	public static final String SERVCODE_100037 = "100037";
	/** 普通交易 */
	public static final String COMMON_0000000000 = "0000000000";
	/** 快速交易 */
	public static final String QUICK_0000000001 = "0000000001";
	
	/** 生成报表列名称  */
	public static final String[] REPORT_COLUMN_NAME = new String[]{
        "服务代码(服务名称)",
        "交易编码",
        "交易时间",
        "客户编码",
        "手机号码",
        "交易主账号",
        "主账号发卡行",
        "交易账号2",
        "PSAM卡号",
        "交易金额",
        "交易手续费",
        "交易状态",
        "交易标记",
        "本地交易流水",
        "清算日期",
        "交易渠道",
        "终端编码",
        "业务受理方",
        "原始业务流水",
        "原始业务参考号",
        "原始业务时间",
        "原始业务结算日期",
        "发送方业务流水",
        "发送发业务时间",
        "服务提供商",
        "SP商户编码",
        "SP终端编码",
        "SP结算日期",
        "SP交易流水",
        "SP交易时间",
        "SP参考号",
        "SP授权码",
        "主卡发卡行",
        "第二账号发卡行",
        "交易返回码",
        "SP返回码",
        "交易返回信息",
        "交易校验码",
        "交易备注"
    };
	
	/**
	 * 初始化报表标题Label
	 * @param sheet
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	private void initLabel(WritableSheet sheet) throws RowsExceededException, WriteException{
		for (int x = 0; x < REPORT_COLUMN_NAME.length; x++) {
			sheet.addCell(new Label(x, 0, REPORT_COLUMN_NAME[x]));
        }
	}
	
	/** 清算对比列名称 */
	public static final String[] LIQUIDATION_COLUMN_NAME = new String[]{
        "服务代码(服务名称)",
        "交易编码",
        "交易时间",
        "交易流水",
        "客户编码",
        "手机号码",
        "交易账号",
        "交易金额",
        "交易状态",
        "交易标记"
    };
	
	/** 代发对比列名称 */
	public static final String[] DAIFA_COLUMN_NAME = new String[]{
        "服务代码(服务名称)",
        "交易日期",
        "交易时间",
        "本地交易流水",
        "手机号码",
        "交易主账号",
        "交易账号2",
        "清算金额",
        "交易手续费",
        "交易金额",
        "姓名",
        "行号",
        "开户行",
        "状态",
        "接入渠道",
        "延迟",
        "返回码"
    };
	
	/**
	 * 初始化报表标题Label
	 * @param sheet
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	private void initLabel_2(WritableSheet sheet) throws RowsExceededException, WriteException{
		for (int x = 0; x < LIQUIDATION_COLUMN_NAME.length; x++) {
			sheet.addCell(new Label(x, 0, LIQUIDATION_COLUMN_NAME[x]));
        }
	}
	
	/**
	 * 初始化报表标题Label
	 * @param sheet
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	private void initLabel_3(WritableSheet sheet) throws RowsExceededException, WriteException{
		for (int x = 0; x < DAIFA_COLUMN_NAME.length; x++) {
			sheet.addCell(new Label(x, 0, DAIFA_COLUMN_NAME[x]));
        }
	}
	
	/**
	 * 添加单元格内容
	 * @param sheet
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	public void addCell(WritableSheet sheet,List<Payjnls> list) throws RowsExceededException, WriteException{
		Label lab;
		//写数据到excel单元格中
		for (int i = 0; i < list.size(); i++) {
			String bankName = "";//主卡
            String fee = "";//收的手续费
			Payjnls bean = list.get(i);
			if (bean.getId().getServcode().equals("100017") && StringUtils.isNotBlank(bean.getAccount())) {
                //手机提款显示银行信息
                PayCashInfo cashInfo = PayCashInfoDao.loadPayCashInfo(bean.getId().getLocaldate(),
                		bean.getLocaltime(), bean.getId().getLocallogno(), bean.getAccount().trim());
                if (StringUtils.isNotBlank(cashInfo.getBankName())) {
                    bankName = cashInfo.getBankProvince().trim() + "," + cashInfo.getBankCity().trim() + ","
                            + CommdbUtil.BANK_MAP.get(cashInfo.getBankId()) + "," + cashInfo.getBankName().trim() + ","
                            + cashInfo.getCustomerName();
                }
            }
            //TODO 以下服务编号,在交易系统流水表不计算手续费,是在后面的账务系统进行计算的
            if (bean.getId().getServcode().equals("100032") || bean.getId().getServcode().equals("100033")
                    || bean.getId().getServcode().equals("100034")) {
                AcctxnBook acctxnBook = new AcctxnBook();
                acctxnBook.setTxndate(bean.getId().getLocaldate());
                acctxnBook.setTxntime(bean.getLocaltime());
                acctxnBook.setTxnlogno(bean.getId().getLocallogno());
                acctxnBook = AcctxnBookDao.getAcctxnBook(acctxnBook);
                if (acctxnBook != null) {
                    fee = String.valueOf(acctxnBook.getTxnfee2());//收手续费
                }
            } else {
                fee = Format.fty(String.valueOf(bean.getFee()));
            }
            
			//服务代码(服务名称)
			lab = new Label(0, i+1, bean.getId().getServcode() + "(" + CommdbUtil.SERVICE_MAP.get(bean.getId().getServcode()) + ")");
			sheet.addCell(lab);
            //交易编码(交易名称)
			lab = new Label(1, i+1, bean.getId().getTradecode() + "(" + CommdbUtil.TRADE_MAP.get(bean.getId().getTradecode()) + ")");
			sheet.addCell(lab);
            //交易时间(日期+时间)
            lab = new Label(2, i+1, bean.getId().getLocaldate() + bean.getLocaltime());
            sheet.addCell(lab);
            //客户编码
            lab = new Label(3, i+1, bean.getCustomerid());
            sheet.addCell(lab);
            //手机号码
            lab = new Label(4, i+1, bean.getMobileNo());
            sheet.addCell(lab);
            //交易主账号
            lab = new Label(5, i+1, bean.getAccount());
            sheet.addCell(lab);
            //交易主账号发卡银行
            lab = new Label(6, i+1, bankName);
            sheet.addCell(lab);
            //交易主账号2
            lab = new Label(7, i+1, bean.getAccount2());
            sheet.addCell(lab);
            //PASM卡号
            lab = new Label(8, i+1, bean.getPsamid());
            sheet.addCell(lab);
            //交易金额
            lab = new Label(9, i+1, Format.fty(String.valueOf(bean.getAmount())));
            sheet.addCell(lab);
			//交易手续费
			lab = new Label(10, i+1, fee);
			sheet.addCell(lab);
            //交易状态
            String status = bean.getStatus();
            if (status.equals(FinalData.TRADE_STATUS_0.getValue())) {
                status = FinalData.TRADE_STATUS_0.getDesc();
            }
            if (status.equals(FinalData.TRADE_STATUS_1.getValue())) {
                status = FinalData.TRADE_STATUS_1.getDesc();
            }
            if (status.equals(FinalData.TRADE_STATUS_2.getValue())) {
                status = FinalData.TRADE_STATUS_2.getDesc();
            }
            lab = new Label(11, i+1, status);
            sheet.addCell(lab);
            //交易标记
            String payTag = bean.getPaytag();
            if (payTag.equals(FinalData.TRADE_FLAG_0.getValue())) {
				payTag = FinalData.TRADE_FLAG_0.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_1.getValue())) {
				payTag = FinalData.TRADE_FLAG_1.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_2.getValue())) {
				payTag = FinalData.TRADE_FLAG_2.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_3.getValue())) {
				payTag = FinalData.TRADE_FLAG_3.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_9.getValue())) {
				payTag = FinalData.TRADE_FLAG_9.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_R.getValue())) {
				payTag = FinalData.TRADE_FLAG_R.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_F.getValue())) {
				payTag = FinalData.TRADE_FLAG_F.getDesc();
			}
            lab = new Label(12, i+1, payTag);
            sheet.addCell(lab);
            //本地交易流水
            lab = new Label(13, i+1, bean.getId().getLocallogno());
            sheet.addCell(lab);
            //清算日期
            lab = new Label(14, i+1, bean.getLocalsettlmtdate());
            sheet.addCell(lab);
            //交易渠道
            String cum = bean.getChannelid();
            if (cum.equals(FinalData.TRADE_CHANNEL_0001.getValue())) {
                cum = FinalData.TRADE_CHANNEL_0001.getDesc();
            }
            if (cum.equals(FinalData.TRADE_CHANNEL_0002.getValue())) {
                cum = FinalData.TRADE_CHANNEL_0002.getDesc();
            }
            if (cum.equals(FinalData.TRADE_CHANNEL_0003.getValue())) {
                cum = FinalData.TRADE_CHANNEL_0003.getDesc();
            }
            lab = new Label(15, i+1, cum);
            sheet.addCell(lab);
            //终端编码
            lab = new Label(16, i+1, bean.getTermid());
            sheet.addCell(lab);
            //业务受理方
            lab = new Label(17, i+1, bean.getBranchid());
            sheet.addCell(lab);
            //原始业务流水
            lab = new Label(18, i+1, bean.getOrgtxlogno());
            sheet.addCell(lab);
            //原始业务参考号
            lab = new Label(19, i+1, bean.getOrgtxrefno());
            sheet.addCell(lab);
            //原始业务时间(日期+时间)
            lab = new Label(20, i+1, bean.getOrgtxdate() + bean.getOrgtxtime());
            sheet.addCell(lab);
            //原始业务结算日期
            lab = new Label(21, i+1, bean.getOrgsettlmtdate());
            sheet.addCell(lab);
            //发送方业务流水
            lab = new Label(22, i+1, bean.getSndtxlogno());
            sheet.addCell(lab);
            //发送发业务时间(日期+时间)	
            lab = new Label(23, i+1, bean.getSndtxdate() + bean.getSndtxtime());
            sheet.addCell(lab);
            //服务提供商
            lab = new Label(24, i+1, bean.getHostbranchid());
            sheet.addCell(lab);
            //SP商户编码
            lab = new Label(25, i+1, bean.getHostmerchid());
            sheet.addCell(lab);
            //SP终端编码
            lab = new Label(26, i+1, bean.getHosttermid());
            sheet.addCell(lab);
            //SP结算日期	
            lab = new Label(27, i+1, bean.getHostsettlmtdate());
            sheet.addCell(lab);
            //SP交易流水
            lab = new Label(28, i+1, bean.getHostlogno());
            sheet.addCell(lab);
            //SP交易时间(日期+时间)	
            lab = new Label(29, i+1, bean.getHostdate() + bean.getHosttime());
            sheet.addCell(lab);
            //SP参考号
            lab = new Label(30, i+1, bean.getHostrefno());
            sheet.addCell(lab);
            //SP授权码	
            lab = new Label(31, i+1, bean.getHostauthcode());
            sheet.addCell(lab);
            //主卡发卡行
            lab = new Label(32, i+1, bean.getIssuer());
            sheet.addCell(lab);
            //第二账号发卡行
            lab = new Label(33, i+1, bean.getIssuer2());
            sheet.addCell(lab);
            //交易返回码
            lab = new Label(34, i+1, bean.getMsgcode());
            sheet.addCell(lab);
            //SP返回码	
            lab = new Label(35, i+1, bean.getHostmsgcode());
            sheet.addCell(lab);
            //交易返回信息
            lab = new Label(36, i+1, bean.getMsgtext());
            sheet.addCell(lab);
            //交易校验码
            lab = new Label(37, i+1, bean.getCheckcode());
            sheet.addCell(lab);
            //交易备注
            lab = new Label(38, i+1, bean.getRemark());
            sheet.addCell(lab);
		}
	}
	
	/**
	 * 添加单元格内容
	 * @param sheet
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	public void addCell_2(WritableSheet sheet,List<LiquidationBean> list) throws RowsExceededException, WriteException{
		Label lab;
		//写数据到excel单元格中
		for (int i = 0; i < list.size(); i++) {
			LiquidationBean bean = list.get(i);
			//服务代码(服务名称)
//			lab = new Label(0, i+1, bean.getServcode() + "(" + CommdbUtil.SERVICE_MAP.get(bean.getServcode()) + ")");
			lab = new Label(0, i+1, bean.getServcode());
			sheet.addCell(lab);
            //交易编码(交易名称)
//			lab = new Label(1, i+1, bean.getTradecode() + "(" + CommdbUtil.TRADE_MAP.get(bean.getTradecode()) + ")");
			lab = new Label(1, i+1, bean.getTradecode());
			sheet.addCell(lab);
            //交易时间(日期+时间)
            lab = new Label(2, i+1, bean.getLocaldate());
            sheet.addCell(lab);
            //本地交易流水
            lab = new Label(3, i+1, bean.getLocallogno());
            sheet.addCell(lab);
            //客户编码
            lab = new Label(4, i+1, bean.getCustomerid());
            sheet.addCell(lab);
            //手机号码
            lab = new Label(5, i+1, bean.getMobileNo());
            sheet.addCell(lab);
            //交易主账号
            lab = new Label(6, i+1, bean.getAccount());
            sheet.addCell(lab);
            //交易金额
            if (null != bean.getAmount()) {
            	lab = new Label(7, i+1, Format.fty(String.valueOf(bean.getAmount())));
            	sheet.addCell(lab);
			}else if(StringUtils.isNotBlank(bean.getAmountStr())){
				lab = new Label(7, i+1, bean.getAmountStr());
				sheet.addCell(lab);
			}
            //交易状态
            String status = bean.getStatus();
            if (status.equals(FinalData.TRADE_STATUS_0.getValue())) {
                status = FinalData.TRADE_STATUS_0.getDesc();
            }
            if (status.equals(FinalData.TRADE_STATUS_1.getValue())) {
                status = FinalData.TRADE_STATUS_1.getDesc();
            }
            if (status.equals(FinalData.TRADE_STATUS_2.getValue())) {
                status = FinalData.TRADE_STATUS_2.getDesc();
            }
            lab = new Label(8, i+1, status);
            sheet.addCell(lab);
            //交易标记
            String payTag = bean.getPaytag();
            if (payTag.equals(FinalData.TRADE_FLAG_0.getValue())) {
				payTag = FinalData.TRADE_FLAG_0.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_1.getValue())) {
				payTag = FinalData.TRADE_FLAG_1.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_2.getValue())) {
				payTag = FinalData.TRADE_FLAG_2.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_3.getValue())) {
				payTag = FinalData.TRADE_FLAG_3.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_9.getValue())) {
				payTag = FinalData.TRADE_FLAG_9.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_R.getValue())) {
				payTag = FinalData.TRADE_FLAG_R.getDesc();
			}
            if (payTag.equals(FinalData.TRADE_FLAG_F.getValue())) {
				payTag = FinalData.TRADE_FLAG_F.getDesc();
			}
            lab = new Label(9, i+1, payTag);
            sheet.addCell(lab);
		}
	}
	
	/**
	 * 添加单元格内容
	 * @param sheet
	 * @throws WriteException 
	 * @throws RowsExceededException 
	 */
	public void addCell_3(WritableSheet sheet,List<DaifaVO> list) throws RowsExceededException, WriteException{
		Label lab;
		//写数据到excel单元格中
		for (int i = 0; i < list.size(); i++) {
            DaifaVO bean = list.get(i);
			//服务代码(服务名称)
			lab = new Label(0, i+1, bean.getServcode() + "(" + CommdbUtil.SERVICE_MAP.get(bean.getServcode()) + ")");
			sheet.addCell(lab);
            //交易日期
			lab = new Label(1, i+1, bean.getLocaldate());
			sheet.addCell(lab);
            //交易时间
            lab = new Label(2, i+1, bean.getLocaltime());
            sheet.addCell(lab);
            //本地交易流水
            lab = new Label(3, i+1, bean.getLocallogno());
            sheet.addCell(lab);
            //手机号码
            lab = new Label(4, i+1, bean.getMobileno());
            sheet.addCell(lab);
            //交易主账号
            lab = new Label(5, i+1, bean.getAccount());
            sheet.addCell(lab);
            //交易账号2
            lab = new Label(6, i+1, bean.getAccount2());
            sheet.addCell(lab);
            //清算金额
            lab = new Label(7, i+1, Format.fty(String.valueOf(bean.getAmount())));
            sheet.addCell(lab);
            //交易手续费
            lab = new Label(8, i+1, Format.fty(String.valueOf(bean.getFee())));
            sheet.addCell(lab);
            //交易金额
            lab = new Label(9, i+1, Format.fty(String.valueOf(bean.getTradeAmount())));
            sheet.addCell(lab);
			//姓名
			lab = new Label(10, i+1, bean.getName());
			sheet.addCell(lab);
            //行号
            lab = new Label(11, i+1, bean.getBankno());
            sheet.addCell(lab);
            //开户行
            lab = new Label(12, i+1, bean.getBankname());
            sheet.addCell(lab);
            //状态
            lab = new Label(13, i+1, bean.getStatus());
            sheet.addCell(lab);
            //接入渠道
            lab = new Label(14, i+1, bean.getInchannel());
            sheet.addCell(lab);
            //延迟
            lab = new Label(15, i+1, bean.getTradedelay());
            sheet.addCell(lab);
            //返回码
            lab = new Label(16, i+1, bean.getRetcode());
            sheet.addCell(lab);
		}
	}
	
	/**
	 * 创建excel文件
	 * @param filePath 文件路径  如:  E:\\testExcel\\testclb.xls
	 * @return
	 * @throws IOException
	 * @throws BiffException 
	 */
	public WritableWorkbook createWorkbook(String filePath) throws IOException, BiffException{
		WritableWorkbook book = Workbook.createWorkbook(new File(filePath));  
		return book;
	}
	
	/**
	 * 写到excel
	 * @param filePath
	 * @param sheetName
	 * @param list
	 * @return
	 */
	public  boolean write(String filePath,String sheetName,List<Payjnls> list){
		try {
			//创建excel文件
			WritableWorkbook book = this.createWorkbook(filePath);
			//创建excel sheet
			WritableSheet sheet = book.createSheet(sheetName,0); 
			//初始化报表标题Label
			this.initLabel(sheet);
			//写数据到excel单元格中
			this.addCell(sheet, list);
			book.write();//写入数据  
			book.close();//关闭资源
		} catch (RowsExceededException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (WriteException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (BiffException e) {
			logger.error(e.getMessage(), e);
			return false;
		} 
		return true;
	}
	
	/**
	 * 写到excel
	 * @param filePath
	 * @param sheetName
	 * @param list
	 * @return
	 */
	public  boolean write_2(String filePath,String sheetName,List<LiquidationBean> list){
		try {
			//创建excel文件
			WritableWorkbook book = this.createWorkbook(filePath);
			//创建excel sheet
			WritableSheet sheet = book.createSheet(sheetName,0); 
			//初始化报表标题Label
			this.initLabel_2(sheet);
			//写数据到excel单元格中
			this.addCell_2(sheet, list);
			book.write();//写入数据  
			book.close();//关闭资源
		} catch (RowsExceededException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (WriteException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (BiffException e) {
			logger.error(e.getMessage(), e);
			return false;
		} 
		return true;
	}
	
	/**
	 * 写到excel
	 * @param filePath
	 * @param sheetName
	 * @param list
	 * @return
	 */
	public  boolean write_3(String filePath,String sheetName,List<DaifaVO> list){
		try {
			//创建excel文件
			WritableWorkbook book = this.createWorkbook(filePath);
			//创建excel sheet
			WritableSheet sheet = book.createSheet(sheetName,0); 
			//初始化报表标题Label
			this.initLabel_3(sheet);
			//写数据到excel单元格中
			this.addCell_3(sheet, list);
			book.write();//写入数据  
			book.close();//关闭资源
		} catch (RowsExceededException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (WriteException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return false;
		} catch (BiffException e) {
			logger.error(e.getMessage(), e);
			return false;
		} 
		return true;
	}
	
	/**
	 * 读清算对比excel文件
	 * @param filePath 文件路径
	 * @param s 第几个sheet
	 * @return
	 */
	public static List<LiquidationBean> read(String filePath,int s){
		List<LiquidationBean> list = new ArrayList<LiquidationBean>();
		 try {
			//打开文件  
			Workbook book = Workbook.getWorkbook(new File(filePath));  
			//获取第一个工作表  
			Sheet sheet = book.getSheet(s); 
			logger.info("共"+sheet.getRows()+"行!");
			//得到第16列,第0行单元格
			Cell cell_0 = sheet.getCell(16,0);//凭证号
			String result0 = cell_0.getContents();
			logger.info("【清算需匹对的列名为】:"+result0);
			for (int i = 1; i < sheet.getRows(); i++) {
				LiquidationBean bean = new LiquidationBean();
				//得到第16列,第1行单元格(凭证号=交易流水号)
				Cell cell_1 = sheet.getCell(16,i);
				bean.setLocallogno(cell_1.getContents());
				//交易日期
				Cell cell_2 = sheet.getCell(0,i);
				bean.setLocaldate(cell_2.getContents());
				//交易金额
				Cell cell_3 = sheet.getCell(8,i);
//				bean.setAmount(Long.valueOf(cell_3.getContents()));
				bean.setAmountStr(cell_3.getContents());
				//交易状态
				Cell cell_4 = sheet.getCell(9,i);
				bean.setStatus(cell_4.getContents());
				//交易结果
				Cell cell_5 = sheet.getCell(10,i);
				bean.setPaytag(cell_5.getContents());
				
				list.add(bean);
			}
			book.close();//关闭资源
		} catch (BiffException e) {
			logger.error(e.getMessage(), e);
		} catch (IndexOutOfBoundsException e) {
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}  
		return list;
	}
	
	/**
	 * 两数组对比,数据中存放String类型
	 * @param list1
	 * @param list2
	 * @return 匹配后不同的list
	 */
	public static List<String> getDifferentList(List<String> list1, List<String> list2) {
        long st = System.nanoTime();
         List<String> diff = new ArrayList<String>();
         List<String> maxList = list1;
         List<String> minList = list2;
         if(list2.size()>list1.size()){
             maxList = list2;
             minList = list1;
         }
         Map<String,Integer> map = new HashMap<String,Integer>(maxList.size());
         for (String bean : maxList) {
             map.put(bean, 1);
         }
         for (String bean : minList) {
             if(map.get(bean)!=null){
                 map.put(bean, 2);
                 continue;
             }
             diff.add(bean);
         }
         for(Map.Entry<String, Integer> entry:map.entrySet()){
             if(entry.getValue()==1){
                 diff.add(entry.getKey());
             }
         }
        logger.info("getDifferentList total times "+(System.nanoTime()-st));
        return diff;
    }
	
	/**
	 * 根据两数组中TradeCode对比,数据中存放LiquidationBean类型
	 * 如果需匹配多个字段,可在TradeCode字段上拼接字符串,作为key
	 * @param list1
	 * @param list2
	 * @return 匹配后不同的list
	 */
	public static List<LiquidationBean> getDifferentBeanList(List<LiquidationBean> list1, List<LiquidationBean> list2) {
        long st = System.nanoTime();
         List<LiquidationBean> diff = new ArrayList<LiquidationBean>();
         List<LiquidationBean> maxList = list1;
         List<LiquidationBean> minList = list2;
         if(list2.size()>list1.size()){
             maxList = list2;
             minList = list1;
         }
         Map<String,Integer> map = new HashMap<String,Integer>(maxList.size());
         //对象bean以key放到map中
         Map<String,LiquidationBean> beanMap = new HashMap<String,LiquidationBean>(maxList.size());
         //把最大的数组存入map中
         for (LiquidationBean bean : maxList) {
             map.put(bean.getLocallogno(), 1);
             beanMap.put(bean.getLocallogno(), bean);
         }
         //循环小的那个数组,通过从map获得key比较是否异同
         for (LiquidationBean bean : minList) {
             if(map.get(bean.getLocallogno())!=null)
             {
                 map.put(bean.getLocallogno(), 2);
                 continue;
             }
             diff.add(bean);
         }
         for(Map.Entry<String, Integer> entry:map.entrySet()){
             if(entry.getValue()==1){
            	 LiquidationBean bean = beanMap.get(entry.getKey());
            	 diff.add(bean);
             }
         }
        logger.info("getDifferentBeanList total times "+(System.nanoTime()-st));
        return diff;
        
    }
	
	
	public static void main(String[] args) {
//		read("E:\\testExcel\\20140401点数.xls",0);
		
		List<LiquidationBean> excelList = ExcelUtil.read("E:\\wokespace2\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\pm\\upload\\20140401dianshu.xls", 0);
		for (int i = 0; i < excelList.size(); i++) {
			LiquidationBean bean = excelList.get(i);
//			System.out.println(bean.getLocallogno()+","+bean.getLocaldate()+","+bean.get);
		}
		
	}
}

猜你喜欢

转载自wenxin2009.iteye.com/blog/2053195
今日推荐