skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

  • Home
 
  • RSS
  • Twitter
Thursday, November 1, 2012

Excel Comparator xls or xlsx in java

Posted by Raju Gupta at 10:30 AM – 0 comments
 

we can compare any two excels of extension either xls or xlsx   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
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.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Excelcomparison {
 ArrayList<String> listOfTerms =null;
 ArrayList<String> al =new ArrayList<String>();
 ArrayList<String> finallist = new ArrayList<String>();
 public  Workbook checkFileFormat(String fileName){
  Workbook hwb=null;
   
  FileInputStream checkFis=null;
  try{
    checkFis=new FileInputStream(fileName);
    //Instantiate the Workbook using HSSFWorkbook
    hwb=(Workbook)new HSSFWorkbook(checkFis);
    Sheet sheet=hwb.getSheetAt(0);
    Iterator<Row> rows=sheet.rowIterator();
    
    Iterator<Cell> cells=null;
    Row row=null;
    Cell cell=null;
    int check=0;
    //Read the file as HSSFWorkbook
    while(rows.hasNext()){
    check++;
    row=(HSSFRow)rows.next();
    cells=row.cellIterator();
    while(cells.hasNext()){
     cell=(HSSFCell)cells.next();
    }
    if(check==2)
     break;
   }
   //Return HSSFWorkbook type object if there is no exception in reading the file using HSSFWorkbook
   return hwb;
  }catch(ClassCastException ce){  //Instantiate the Workbook using XSSFWorkbook in case of class cast exception
   Workbook xwb=null;
   try{
   xwb=new XSSFWorkbook(checkFis); 
   checkFis.close();
   }catch(IOException e){   
   e.printStackTrace();
     }
  return xwb; }
  catch(Exception e){ //Instantiate the Workbook using XSSFWorkbook in case of Exception while reading file through HSSFWorkbook
  Workbook xwb=null;
  try{
   if(checkFis!=null)
  checkFis.close();
  checkFis=null;
  checkFis=new FileInputStream(fileName);
  xwb=new XSSFWorkbook(checkFis);
  checkFis.close();
  }catch(Exception ie){
    
 return null;
  }
   
 return xwb;
 }
 }
 public void readExcel()
 {
  try
  {
   Workbook workbook = checkFileFormat("EXCEL1.xlsx");
  listOfTerms =new ArrayList<String>();
  Sheet sheet = workbook.getSheetAt(0);
  int rows  = sheet.getPhysicalNumberOfRows();
  System.out.println("here "+rows+"   "+sheet.getSheetName());
  for (int r = 0; r < rows; r++)
  {
       Row row   = sheet.getRow(r);
       Cell cell =row.getCell((short)0);
       String cellValue = cell.getStringCellValue().trim();
       listOfTerms.add(cellValue);
    }
      System.out.println(listOfTerms);
      Workbook workbook1 = checkFileFormat("EXCEL2.xls");
      Sheet sheet1 = workbook1.getSheetAt(0);
      int rows1  = sheet1.getPhysicalNumberOfRows();
      System.out.println("here "+rows1+"   "+sheet.getSheetName());
      for (int k = 0; k < rows1; k++)
      {
         Row row1   = sheet.getRow(k);
        Cell cell =row1.getCell((short)0);
         String cellValue2 = cell.getStringCellValue();
         al.add(cellValue2);
        
      }
        System.out.println(al);
        for(int i=0; i<al.size(); i++)
        {
         
          if(!listOfTerms.contains(al.get(i)))
           {
             finallist.add(al.get(i));
           }
        }
        for(int i=0; i<listOfTerms.size(); i++)
        {
          if(!al.contains(listOfTerms.get(i)))
          {
            finallist.add(listOfTerms.get(i));
          }
        }
        System.out.println("Elements which are different: " +finallist);
        System.out.println("");
 createSheet();
  }catch(Exception e)
  {
   e.getStackTrace();
   System.out.println(e.getMessage());
  }
 }
 private void createSheet() {
  XSSFWorkbook bookout = new XSSFWorkbook();
  try
  {
   int j=0;
   XSSFSheet sheet1= bookout.createSheet();
   bookout.setSheetName(0,"Document Handles");
    for(int m=0;m<finallist.size();m++)
    {
     XSSFRow row=sheet1.createRow(j++);
     row.createCell((short) 0).setCellValue(finallist.get(m));
      
    }
    FileOutputStream fout = new FileOutputStream("OutputExcel.xlsx");
    bookout.write(fout);
    fout.close();
  System.out.println("file completed");
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
  }
 }
  public static void main(String args[]){
  Excelcomparison e = new Excelcomparison();
  e.readExcel();
 }
}

Email This BlogThis! Share to X Share to Facebook

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )

List of Java Programs

  • Java Program to check Greater between the Two Number
  • Java Program to find that given number is Palindrome or not
  • Java Program to Demonstrate the Use of Pre and Post Operator
  • Java Program to Reverse the Given Number
  • Java Program to Print Number in the Given Data Type
  • Program to Demonstrate Skipping using Continue
  • Program to find whether entered character is a vowel or Consonant
  • Java Program to Calculate the Sum of Digits of Given Number
  • How to swap two numbers using only two variables
  • Checking the Given Number is Armstrong or Not
  • Average an Array of Values
  • Display ASCII Code Instead of Character
  • Comparison of Two Variable using If
  • Printing Table In java using While Loop
  • Generate Random Number Using Math.Random Function
  • To Find roots of Quadratic Equation
  • Performing Arithmetic Opration on Two Variable
  • Concatenation of Two String in Java
  • Command Line Argument in JAVA
  • Java Hello World Program
  • Calculate Circle Perimeter | Java Program
  • Calculate the Area of Circle | Java Program
  • To Find Whether Given Year is a Leap Year or not
  • Popular
  • Recent
  • Archives

Total Pageviews

Sparkline 2,163,617

Followers

Popular Posts of This Week

  • Java Class to Calculate the Volume of Box
    Here is a Java Class to Calculate the Volume of Box. class Box { double width; double height; double depth; // This is the con...
  • Merge-Sort Algorithm implementation in JAVA
    Merge-Sort Function void MergeSort(int low, int high) // a[low : high] is a global array to be sorted. // Small(P) is true if there...
  • Compute distance light travels using long variables
    Here is a Java Program to Compute distance light travels using long variables class Light { public static void main(String args[]) { ...
  • Stack implemented as array - Data Structure
    // Stack implemented as array public class ArrayStack<T> { private T[] stack; private int numElements = 0; // points to s...
  • Sort an Integer array with Bucket Sort
    Here is a java program to Sort an Integer array with Bucket Sort class BucketSort { public int[] bucketSort(int[] array) { /...
  • Java program to create a Binary Heap and Perform various operation
    A binary heap (min-heap) is a complete binary tree with elements from a partially ordered set, such that the element at every node is less ...
  • Java Stack Trace at any point for debugging
    This code snippet gives us Stack flow at any point in java code execuition irrespective of any errors. This oudl be really helpful where we ...
  • To Sort an Interger Array using Shell Sort
    To Sort an Interger Array using Shell Sort class ShellSort { public static int[] shellSort(int[] array) { int N = array.length; ...
  • Word Wrap
    Word Wrap has been implemented in Java. The program can be used to wrap text into lines of N characters each and take care of word wrap whi...
  • Validating Password and Confirm Password in JSF
    <html xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns="http://www.w3.org...
Powered by Blogger.

Archives

  • ►  2014 ( 4 )
    • ►  August ( 4 )
  • ►  2013 ( 6 )
    • ►  August ( 1 )
    • ►  April ( 5 )
  • ▼  2012 ( 673 )
    • ▼  November ( 9 )
      • Parse XML file using DOM
      • Listing Files and Directory Using Java
      • To Capture screen shots using Java
      • Extract a zip file in Java
      • Albers Square using JAVA
      • Doubling Stack of Strings using Java
      • Excel Comparator xls or xlsx in java
      • Code to Remove Certain Characters from a Java Input
      • Code to Populate Dropdown from a DB
    • ►  October ( 223 )
    • ►  September ( 272 )
    • ►  August ( 2 )
    • ►  June ( 1 )
    • ►  February ( 67 )
    • ►  January ( 99 )
 

Our Blogs

  • Linux Tutorial
  • C Programming Tutorial

Labels

  • Agile Methodology ( 1 )
  • Algorithm ( 3 )
  • AntiSamy ( 1 )
  • Arithmetic Operation ( 1 )
  • Array Example ( 9 )
  • ArrayList Examples ( 11 )
  • Average an Array of Values ( 1 )
  • Barcode Example ( 1 )
  • Basic Java Programs ( 34 )
  • Bing API Example ( 2 )
  • BitSet Example ( 1 )
  • Boolean Example ( 1 )
  • Bouncy Castle API ( 1 )
  • Break Statement ( 2 )
  • BufferedReader Example ( 2 )
  • Calendar Example ( 1 )
  • Chart Generation Example ( 1 )
  • Command Line Argument ( 1 )
  • Comparator Example ( 1 )
  • Concatenation of String ( 1 )
  • Continue Statement ( 1 )
  • Control Structure ( 1 )
  • Copy File Example ( 1 )
  • CRC Example ( 1 )
  • CSV Example ( 6 )
  • Data Structure ( 5 )
  • Date Example ( 2 )
  • Directory Example ( 1 )
  • Do - While Loop Example ( 1 )
  • Domino Database ( 1 )
  • Email Example ( 8 )
  • Encryption Example ( 3 )
  • Excel Example ( 15 )
  • Factorial Example ( 1 )
  • File Upload Example ( 1 )
  • Find Roots of Quadratic Equation ( 1 )
  • FTP Example ( 2 )
  • Graph Examples ( 1 )
  • Greater between Two Numbers ( 1 )
  • GSON Library ( 1 )
  • HashMap Example ( 1 )
  • HashSet Example ( 1 )
  • Hello World Program ( 1 )
  • If Condition ( 2 )
  • Inner Class Example ( 1 )
  • iText Example ( 3 )
  • JAR File ( 1 )
  • JAVA Applet ( 1 )
  • Java Applications ( 1 )
  • Java AWT Example ( 9 )
  • Java Certification ( 1 )
  • Java Class Examples ( 15 )
  • Java Collection Example ( 1 )
  • Java Command Example ( 4 )
  • Java Constructor Examples ( 1 )
  • Java Currency Example ( 1 )
  • Java Database Example ( 3 )
  • Java Date and Time Example ( 3 )
  • Java DateFormat Example ( 3 )
  • Java Examples ( 2 )
  • Java Exception Example ( 5 )
  • Java File Example ( 22 )
  • Java GUI Example ( 1 )
  • Java Image Examle ( 3 )
  • Java Inheritance Example ( 3 )
  • Java Input Output Example ( 1 )
  • Java IO Example ( 3 )
  • Java Jar Example ( 1 )
  • Java JSON Example ( 3 )
  • Java Mail Examples ( 4 )
  • Java Map Example ( 5 )
  • Java MapReduce Example ( 2 )
  • Java MultiThreading Example ( 7 )
  • Java Network Example ( 9 )
  • Java Package ( 1 )
  • Java Programs ( 1 )
  • Java RMI ( 1 )
  • Java Robot Class Examples ( 2 )
  • Java Runtime Example ( 1 )
  • Java Swing Example ( 9 )
  • Java Util Example ( 1 )
  • Java Vector Example ( 4 )
  • Java Voice Example ( 1 )
  • Java Webservice Example ( 1 )
  • Java XML Example ( 3 )
  • Java Zip Class Examples ( 2 )
  • JDBC ( 9 )
  • JDK Version Comparison ( 1 )
  • JFrame Example ( 3 )
  • JOptionPane Dialog Example ( 1 )
  • JPanel Example ( 1 )
  • JSP Example ( 2 )
  • JSTL Example ( 1 )
  • jUnit Example ( 2 )
  • LinkedList Example ( 2 )
  • List Example ( 1 )
  • Long Variable ( 1 )
  • Lottery Nubmer ( 1 )
  • MD5 Hashing Example ( 3 )
  • Memory Management Example ( 1 )
  • Method Override ( 1 )
  • MIDI Sound ( 8 )
  • Module Operator Example ( 2 )
  • Multiplication Table ( 1 )
  • Observer Interface Example ( 1 )
  • Operator Example ( 5 )
  • Pagination ( 1 )
  • Palindrome Number ( 1 )
  • Pass By Reference Example ( 1 )
  • Pass By Value Example ( 1 )
  • PDF File Example ( 3 )
  • PDF Generation Example ( 4 )
  • Pre and Post Operator ( 2 )
  • Prime Number ( 3 )
  • Progress Bar Example ( 1 )
  • Property List Example ( 2 )
  • Random Function ( 7 )
  • Recursion Example ( 2 )
  • Regex Example ( 2 )
  • Remote Host Example ( 2 )
  • Robot Class ( 4 )
  • Searching Example ( 3 )
  • Slideshow ( 1 )
  • Sorting Example ( 7 )
  • SpringLayout Example ( 1 )
  • Stack Example ( 4 )
  • Static Variable ( 1 )
  • StreamTokenizer Example ( 2 )
  • String Example ( 19 )
  • Struts2 Example ( 1 )
  • Sum of Digits ( 1 )
  • Swap Two Numbers ( 1 )
  • Switch Case ( 3 )
  • Tapestry Components ( 1 )
  • Thumbnail Example ( 2 )
  • TimerTask Example ( 2 )
  • To Calculate Volume ( 1 )
  • To Check Armstrong Number ( 1 )
  • Tree Example ( 1 )
  • TreeMap Example ( 1 )
  • TreeSet Example ( 1 )
  • Two Dimensional Array Example ( 1 )
  • UUID ( 1 )
  • Validation Example ( 2 )
  • Variable Casting ( 1 )
  • While Loop ( 1 )
  • XML Parsing ( 7 )
  • XSS Attacks ( 1 )
  • Zip File ( 15 )

Popular Posts

  • Java Class to Calculate the Volume of Box
    Here is a Java Class to Calculate the Volume of Box. class Box { double width; double height; double depth; // This is the con...
  • Java class that defines an integer stack that can hold 10 values
    Here is a Java class that defines an integer stack that can hold 10 values. class Stack { int stck[] = new int[10]; int tos; // ...
  • Validating Password and Confirm Password in JSF
    <html xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns="http://www.w3.org...
  • Java program to create a Binary Heap and Perform various operation
    A binary heap (min-heap) is a complete binary tree with elements from a partially ordered set, such that the element at every node is less ...
  • Compute distance light travels using long variables
    Here is a Java Program to Compute distance light travels using long variables class Light { public static void main(String args[]) { ...
  • Stack implemented as array - Data Structure
    // Stack implemented as array public class ArrayStack<T> { private T[] stack; private int numElements = 0; // points to s...
  • Java Program to Generate the Lottery Number between 1 to 49
     A lottery requires that you select six different numbers from the integers 1 to 49. Write a program to do this for you and generate five s...
  • Create an Adjacency matrix Graph and perform Add and Remove operation
    import java.util.ArrayList; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import...
  • To Sort an Interger Array using Shell Sort
    To Sort an Interger Array using Shell Sort class ShellSort { public static int[] shellSort(int[] array) { int N = array.length; ...
  • Merge-Sort Algorithm implementation in JAVA
    Merge-Sort Function void MergeSort(int low, int high) // a[low : high] is a global array to be sorted. // Small(P) is true if there...
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger