Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26322996
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-06-09 00:22:18

package cn.job;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class TyUtils {
    public static void main(String[] args) {
        try {
            diffExcels("E:/date.xls","E:/dates.xls");
        } catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
    }
    /**
     * 判断两份EXCEL表格的内容差异
     * @param 目标EXCEL    (即最新的EXCEL文档)
     * @param 需要对比的EXCEL
     * */

    private static void diffExcels(String muexcel,String oldexcel) throws Exception {
        Workbook rwb = null;
        try {
            /**
             * 将目标的EXCEL导入到一个ArrayList内
             * 存储的格式为new ArrayList
             * */

            InputStream is = new FileInputStream(muexcel);
            rwb = Workbook.getWorkbook(is);
            //判断当前的工作簿中有多少个工作表

            String[] sheets = rwb.getSheetNames();
            //遍历全部的工作表

            int sheetsLength = sheets.length;
            ArrayList<ArrayList<String>> arrayLists = new ArrayList<ArrayList<String>>();
            ArrayList<String> results = new ArrayList<String>();
            ArrayList<String> mydiff = new ArrayList<String>();
            for (int i = 0; i < sheetsLength; i++) {
                //处理第I个数据表的数据

                ArrayList<String> sheetArray = new ArrayList<String>();
                Sheet sheet = rwb.getSheet(i);
                int rows = sheet.getRows();
                for (int j = 1; j < rows; j++) {
                    Cell c00 = sheet.getCell(0, j);//第一个数据表里面的第一行第一列的元素值!

                    String strc00 = c00.getContents();
                    if (strc00.equals("")) {
                        continue;
                    }
                    sheetArray.add(strc00.trim());
                }
                arrayLists.add(sheetArray);
            }
            //以下代码将实现将要对比的EXCEL解析过来

            is = new FileInputStream(oldexcel);
            rwb = Workbook.getWorkbook(is);
            //判断当前的工作簿中有多少个工作表

            sheets = rwb.getSheetNames();
            //遍历全部的工作表

            sheetsLength = sheets.length;
            ArrayList<ArrayList<String>> arrayList = new ArrayList<ArrayList<String>>();
            for (int i = 0; i < sheetsLength; i++) {
                //处理第I个数据表的数据

                ArrayList<String> sheetArray = new ArrayList<String>();
                Sheet sheet = rwb.getSheet(i);
                int rows = sheet.getRows();
                for (int j = 1; j < rows; j++) {
                    Cell c00 = sheet.getCell(0, j);//第一个数据表里面的第一行第一列的元素值!

                    String strc00 = c00.getContents();
                    if (strc00.equals("")) {
                        continue;
                    }
                    sheetArray.add(strc00.trim());
                }
                arrayList.add(sheetArray);
            }
            //对比

            for (int i = 0; i < arrayLists.size(); i++) {
                //遍历当前的ARRAYLIST里面的元素

                ArrayList<String> tmp = arrayLists.get(i);
                int length = tmp.size();
                for (int j = 0; j < length; j++) {
                    String string = tmp.get(j);                    
                    if (!arrayList.get(i).contains(string.trim())) {
                        System.out.println("=========");
                        try {
                            results.add(string);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        
                    }
                }
                
                tmp = arrayList.get(i);
                length = tmp.size();
                for (int j = 0; j < length; j++) {
                    String string = tmp.get(j);                    
                    if (!arrayLists.get(i).contains(string.trim())) {
                        mydiff.add(string);
                    }
                }
                
            }
            System.out.println("****************");
            System.out.println(results);
            System.out.println(mydiff);
        } catch (Exception e) {
            throw new Exception();
        } finally {
            rwb.close();
        }
    }
}


阅读(900) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~