Google Search

Sunday, July 10, 2016

How to compare Two Excel Files - Java Code

package com.seth.excel;
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;

import org.apache.poi.hssf.usermodel.HSSFCell;
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;

public class CompareExcel {

    public static void main(String[] args) {
        try {
            // get input excel files
       
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter File 1");
        String fileA = reader.nextLine();
        System.out.println("Enter File 2");
        String fileB = reader.nextLine();
        System.out.println("Enter WorkSheet Number to Compare (Starts from 0)");
        String SheetNumber = reader.nextLine();
        FileInputStream excellFile1 = new FileInputStream(new File(
                    fileA));
            FileInputStream excellFile2 = new FileInputStream(new File(
                    fileB));

            // Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook1 = new XSSFWorkbook(excellFile1);
            XSSFWorkbook workbook2 = new XSSFWorkbook(excellFile2);

            // Get first/desired sheet from the workbook
            XSSFSheet sheet1 = workbook1.getSheetAt(Integer.parseInt(SheetNumber));
            XSSFSheet sheet2 = workbook2.getSheetAt(Integer.parseInt(SheetNumber));

            // Compare sheets
            if(compareTwoSheets(sheet1, sheet2)) {
                System.out.println("\n\nThe two excel sheets are Equal");
            } else {
                System.out.println("\n\nThe two excel sheets are Not Equal");
            }
            
            //close files
            excellFile1.close();
            excellFile2.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    
    // Compare Two Sheets
    public static boolean compareTwoSheets(XSSFSheet sheet1, XSSFSheet sheet2) {
        int firstRow1 = sheet1.getFirstRowNum();
        int lastRow1 = sheet1.getLastRowNum();
        boolean equalSheets = true;
        for(int i=firstRow1; i <= lastRow1; i++) {
            
            System.out.println("\n\nComparing Row "+i);
            
            XSSFRow row1 = sheet1.getRow(i);
            XSSFRow row2 = sheet2.getRow(i);
            if(!compareTwoRows(row1, row2)) {
                equalSheets = false;
                System.out.println("Row "+i+" - Not Equal");
                //break;
            } else {
               // System.out.println("Row "+i+" - Equal");
            }
        }
        return equalSheets;
    }

    // Compare Two Rows
    public static boolean compareTwoRows(XSSFRow row1, XSSFRow row2) {
        if((row1 == null) && (row2 == null)) {
            return true;
        } else if((row1 == null) || (row2 == null)) {
            return false;
        }
        
        int firstCell1 = row1.getFirstCellNum();
        int lastCell1 = row1.getLastCellNum();
        boolean equalRows = true;
        
        // Compare all cells in a row
        for(int i=firstCell1; i <= lastCell1; i++) {
            XSSFCell cell1 = row1.getCell(i);
            XSSFCell cell2 = row2.getCell(i);
            if(!compareTwoCells(cell1, cell2)) {
                equalRows = false;
                System.err.println("       Cell "+i+" - Not Equal" +"; Value of Cell 1 is \"" +cell1 + "\" - Value of Cell 2 is \"" +cell2 + "\"");
                //break;
            } else {
                //System.out.println("       Cell "+i+" - Equal");
            }
        }
        return equalRows;
    }

    // Compare Two Cells
    public static boolean compareTwoCells(XSSFCell cell1, XSSFCell cell2) {
        if((cell1 == null) && (cell2 == null)) {
            return true;
        } else if((cell1 == null) || (cell2 == null)) {
            return false;
        }
        
        boolean equalCells = false;
        int type1 = cell1.getCellType();
        int type2 = cell2.getCellType();
        if (type1 == type2) {
            if (cell1.getCellStyle().equals(cell2.getCellStyle())) {
                // Compare cells based on its type
                switch (cell1.getCellType()) {
                case HSSFCell.CELL_TYPE_FORMULA:
                    if (cell1.getCellFormula().equals(cell2.getCellFormula())) {
                        equalCells = true;
                    }
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    if (cell1.getNumericCellValue() == cell2
                            .getNumericCellValue()) {
                        equalCells = true;
                    }
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    if (cell1.getStringCellValue().equals(cell2
                            .getStringCellValue())) {
                        equalCells = true;
                    }
                    break;
                case HSSFCell.CELL_TYPE_BLANK:
                    if (cell2.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                        equalCells = true;
                    }
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    if (cell1.getBooleanCellValue() == cell2
                            .getBooleanCellValue()) {
                        equalCells = true;
                    }
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    if (cell1.getErrorCellValue() == cell2.getErrorCellValue()) {
                        equalCells = true;
                    }
                    break;
                default:
                    if (cell1.getStringCellValue().equals(
                            cell2.getStringCellValue())) {
                        equalCells = true;
                    }
                    break;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
        return equalCells;
    }
}

Sunday, May 29, 2016

Create JPEG Image Graph from CSV File

import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opencsv.CSVReader;

public class LineChart
{


   public static void main( String[ ] args ) throws Exception
   {
  DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset();
  String strFile = "F:\\Java\\Sample.csv";
  int  ColCount=getCols("F:\\Java\\Sample.csv");
 
  FileReader fr;
       fr = new FileReader("F:\\Java\\Sample.csv");
     
     
     
       String[] Headers = getHeaders("F:\\Java\\Sample.csv");
       System.out.println(Headers[0]);
 //Creation of DataSet Starts Here
       for (int k =0; k<ColCount;k++){
   

     CSVReader reader = new CSVReader(new FileReader(strFile));
   
     String [] nextLine;
     int lineNumber = 0;
     nextLine = reader.readNext();
   
   
 

     while ((nextLine = reader.readNext()) != null) {
       lineNumber++;
     
       line_chart_dataset.addValue( Double.parseDouble((nextLine[k])) , Headers[k] , "X" +lineNumber );
   

     }//End of While Loop
   
  }//End of For Loop - k
     
     
      // Chart Creation Starts here
      JFreeChart lineChartObject = ChartFactory.createLineChart(
         "Ececution Stats","Execution Number",
         "Value",
         line_chart_dataset,PlotOrientation.VERTICAL,
         true,true,false);

      int width = 1280; /* Width of the image */
      int height = 480; /* Height of the image */
      File lineChart = new File( "F:\\Java\\LineChart.jpeg" );
      ChartUtilities.saveChartAsJPEG(lineChart ,lineChartObject, width ,height);
   }
 
 
 
   public static int getCols(String xFileLocation) throws FileNotFoundException{

String InputLine = "";
int Rows = 0;
int Cols=0;
String[] InArray = null;

//String xFileLocation = "F:\\Java\\Sample.csv";
Scanner scanIn = new Scanner (new BufferedReader(new FileReader(xFileLocation)));
scanIn.useDelimiter(",");

while(scanIn.hasNextLine()){
InputLine = scanIn.nextLine();
InArray = InputLine.split(",");
Rows++;
Cols = InArray.length;

}

return Cols;
//System.out.println("Count of Rows is : " +Rows +" Count of Cols is "  + Cols);

}

public static int getRows(String xFileLocation) throws FileNotFoundException{

String InputLine = "";
int Rows = 0;
int Cols=0;
String[] InArray = null;

//String xFileLocation = "F:\\Java\\Sample.csv";
Scanner scanIn = new Scanner (new BufferedReader(new FileReader(xFileLocation)));
scanIn.useDelimiter(",");

while(scanIn.hasNextLine()){
InputLine = scanIn.nextLine();
InArray = InputLine.split(",");
Rows++;
Cols = InArray.length;

}

return Rows;
//System.out.println("Count of Rows is : " +Rows +" Count of Cols is "  + Cols);

}

public static String[] getHeaders(String xFileLocation) throws IOException{

FileReader fr;
    fr = new FileReader("F:\\Java\\Sample.csv");
   
   
    BufferedReader br = new BufferedReader(fr);
    String line = br.readLine();
    StringTokenizer st = new StringTokenizer(line, ",");
    String[] Headers = new String[getCols("F:\\Java\\Sample.csv")];
    int i = 0;
    final DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
    //String xLabel = st.nextToken();
   // System.out.println(xLabel);
    while(st.hasMoreTokens()){
    Headers[i] = st.nextToken();
 
   // System.out.println(Headers[i]);
    i=i+1;
    }

return Headers;


}

 
}

Tuesday, June 16, 2015

Triggers in Oracle 11g -> Execution Order in Oracle 11g

Before Oracle 11g, more than trigger can be created on one table but Oracle doesn't guarantee the order of execution of trigger.  Oracle has introduced the feature in Oracle 11g to execute the trigger in any order as per requirements.

Let’s look the example:

Monday, June 15, 2015

Virtual Column in Oracle

Oracle has introduced new feature in version 11 ie. Virtual column. Virtual column values are not stored on disk. They are generated at runtime using their associated expression.

Lets have a look how to create a table with Virtual Column 

CREATE TABLE testvirtual
(
   id1   NUMBER (10)
 , id2   NUMBER (10)
 , id3   NUMBER (10) GENERATED ALWAYS AS (id1 + id2) VIRTUAL
);
Virtual keyword is optional.

Sunday, June 14, 2015

Table Compression in Oracle 11g


Table compression was first introduced in Oracle 9i as a space saver feature only for data warehousing projects. In oracle 11g , this is acceptable as mainstream feature which is acceptable for OLTP Databases. This will result in saving storage space as this will compress the table, increased I/O performance and reduced memory use in the buffer cache. Definitely, this incurs CPU overhead because of compression.

Compression can be done at the tablespace, table or partition level . Options/Features which are available with Compression :