0% found this document useful (0 votes)
292 views

Code1 To Read XML Using JXL Api

This Java program reads data from two Excel files, compares the records, and writes matching records to a CSV file. It extracts data from multiple columns in both Excel files into ArrayLists. The program then loops through the ArrayLists to find matching records based on comparing multiple fields, and writes the matched records to a CSV file with four columns: PromoId, Quantity, Discount, and SourceId.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
292 views

Code1 To Read XML Using JXL Api

This Java program reads data from two Excel files, compares the records, and writes matching records to a CSV file. It extracts data from multiple columns in both Excel files into ArrayLists. The program then loops through the ArrayLists to find matching records based on comparing multiple fields, and writes the matched records to a CSV file with four columns: PromoId, Quantity, Discount, and SourceId.

Uploaded by

Pawan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

/*

* To change this license header, choose License Headers in Project Properti


es.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication4;
/**
*
* @author india
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import jxl.*;
import jxl.write.*;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.read.biff.BiffException;
import
import
import
import
import

jxl.write.Label;
jxl.write.Number;
jxl.write.WritableSheet;
jxl.write.WritableWorkbook;
jxl.write.WriteException;

public class XlsReader1 {


public static void main(String[] args)
throws BiffException, IOException, WriteException
{
//System.out.println("in");
Workbook dataWorkbook = Workbook.getWorkbook(new File("data.xls"));//xsl
workbook
Sheet dataSheet = dataWorkbook.getSheet(0);
Cell cpromoId = dataSheet.getCell(8,1);//promo id
String promoId = cpromoId.getContents();
//System.out.println("str1 "+promoId);
Workbook checkWorkbook = Workbook.getWorkbook(new File("check.xls"));//x
ml workbook
Sheet checkSheet = checkWorkbook.getSheet(0);
Cell ccheckCell = checkSheet.getCell(13,1);//chk sheet id5 col.. promo i
d match
String checkCell = ccheckCell.getContents();
//System.out.println("str3 "+checkCell);
//****** records Adding **************//

//** records from data sheet**//


//** records of promo id**//
ArrayList<String> arrPid = new ArrayList<String>(20000);//arraylist to s
tore Promo id values
Cell[] cellColumnPid = dataSheet.getColumn(8);//cell col array
int cellCountpid = cellColumnPid.length;
cellCountpid--;
//System.out.println("cells in col: " + cellCountpid);
int cellCount2 = cellCountpid;
int i = 1;
while(cellCountpid != 0)
{
//Sheet sh4 = wb1.getSheet(0);
Cell cell4 = dataSheet.getCell(8,i);//to get the promo id values in cell
String str4 = cell4.getContents();
//System.out.println(str4);
arrPid.add(str4);
cellCountpid--;
i++;
}
//** records of th.**//
ArrayList<String> arrTh = new ArrayList<String>(20000);//arraylist to st
ore Promo id values
Cell[] cellColumnTh = dataSheet.getColumn(5);//cell col array
int cellCountTh = cellColumnTh.length;
cellCountTh--;
//System.out.println("cells in col: " + cellCountTh);
int cellCount5 = cellCountTh;
int i1 = 1;
while(cellCountTh != 0)
{
Cell cell5 = dataSheet.getCell(5,i1);//to get the promo id values in cel
l
String str5 = cell5.getContents();
//System.out.println(str5);
arrTh.add(str5);
cellCountTh--;
i1++;
}
//** records of offer**//
ArrayList<String> arrOff = new ArrayList<String>(20000);//arraylist to s
tore Promo id values
Cell[] cellColumnOff = dataSheet.getColumn(6);//cell col array
int cellCountOff = cellColumnOff.length;
cellCountOff--;
//System.out.println("cells in col: " + cellCountOff);
int cellCount6 = cellCountOff;
int i2 = 1;
while(cellCountOff != 0)
{
Cell cell6 = dataSheet.getCell(6,i2);//to get the promo id values in cel
l
String str6 = cell6.getContents();
// System.out.println(str6);
arrOff.add(str6);
cellCountOff--;

i2++;
}
//*** records from check xml**//
//**adding records from ID5**//
//System.out.println("printing id5");
ArrayList<String> arrId5 = new ArrayList<String>(50000);//id5 is col nam
e
Cell[] cellColumnid5 = checkSheet.getColumn(13);//cell col array
int cellCountid5 = cellColumnid5.length;
cellCountid5--;
//System.out.println("cells in col: " + cellCountid5);
int cellCount3 = cellCountid5;
int j = 1;
while(cellCountid5 != 0)
{
Cell cell5 = checkSheet.getCell(13,j);//to get the promo id values in ce
ll
String str5 = cell5.getContents();
// System.out.println(str5);
arrId5.add(str5);
cellCountid5--;
j++;
}
//**adding records from QTY8**//
// System.out.println("printing qty8");
ArrayList<String> arrQty = new ArrayList<String>(50000);//id5 is col nam
e
Cell[] cellColumnQty = checkSheet.getColumn(28);//cell col array
int cellCountQty = cellColumnQty.length;
cellCountQty--;
//System.out.println("cells in col: " + cellCountQty);
int cellCount7 = cellCountQty;
int j1 = 1;
while(cellCountQty != 0)
{
// System.out.println("m here");
Cell cell7 = checkSheet.getCell(28,j1);//to get the promo id values in c
ell
String str7 = cell7.getContents();
// System.out.println(str7);
arrQty.add(str7);
cellCountQty--;
j1++;
}
//**adding records from Discount Percent**//
//System.out.println("printing disc");
ArrayList<String> arrDisc = new ArrayList<String>(50000);//id5 is col na
me
Cell[] cellColumnDisc = checkSheet.getColumn(29);//cell col array
int cellCountDisc = cellColumnDisc.length;
cellCountDisc--;
//System.out.println("cells in col: " + cellCountDisc);

int cellCount8 = cellCountDisc;


int j2 = 1;
while(cellCountDisc != 0)
{
Cell cell8 = checkSheet.getCell(29,j2);//to get the promo id values in c
ell
//

String str8 = cell8.getContents();


System.out.println(str8);
arrDisc.add(str8);
cellCountDisc--;
j2++;
}
//**ended record adding in arraylists
//System.out.println("done");

//to compare the first sheet's records with second sheet. one by one
int x = cellCount3;
int y = 0;
//ArrayList<String> matchId5 = new ArrayList<String>(20000);
ArrayList<String> resultPid = new ArrayList<String>(20000);
int r1 = 0;
String[] arrstr1 = new String[20000];
String[] arrstr2 = new String[20000];
String[] arrstr3 = new String[20000];
String[] arrstr4 = new String[20000];
String[] arrstr5 = new String[20000];
String[] arrstr6 = new String[20000];
String[] arrstr7 = new String[20000];
//to match the records of pid
do
{
cellCount2--;
//
cellCount5--;
//
cellCount6--;
//System.out.println("cellcount2 " + cellCount2);
cellCount3 = x;
do
{
cellCount3--;
//
cellCount7--;
//
cellCount8--;
System.out.println("cellcount3 " + cellCount3);
// && arrTh.get(cellCount2).equals(arrQty.get(cellCount3)) && arrOff.
get(cellCount2).equals(arrDisc.get(cellCount3))
if(arrPid.get(cellCount2).equals(arrId5.get(cellCount3))&& arrTh.get(cel
lCount2).equals(arrQty.get(cellCount3)) && arrOff.get(cellCount2).equals(arrDisc
.get(cellCount3)))
{
//if(arrTh.get(cellCount2).equals(arrQty.get(cellCount3)))
System.out.println("2 if");
{
//!arrDisc.get(cellCount3).isEmpty() &&
//if(arrOff.get(cellCount2).contains(arrDisc.get(cellCount3)))
{
Cell cell9 = checkSheet.getCell(27,cellCount3);//to get the promo id val

ues in cell
String str9 = cell9.getContents();
arrstr1[cellCount2] = str9;
Cell cell12 = dataSheet.getCell(8,cellCount2);//to get the promo id valu
es in cell
String str12 = cell12.getContents();
arrstr2[cellCount2] = str12;
Cell cell13 = dataSheet.getCell(5,cellCount2);//to get the promo id valu
es in cell
String str13 = cell13.getContents();
arrstr3[cellCount2] = str13;
System.out.println("arrstr3");
Cell cell14 = dataSheet.getCell(6,cellCount2);//to get the promo id valu
es in cell
String str14 = cell14.getContents();
arrstr3[cellCount2] = str14;
Cell cell15 = dataSheet.getCell(13,cellCount2);//to get the promo id val
ues in cell
String str15 = cell15.getContents();
arrstr4[cellCount2] = str15;
Cell cell16 = dataSheet.getCell(28,cellCount2);//to get the promo id val
ues in cell
String str16 = cell16.getContents();
arrstr5[cellCount2] = str16;
System.out.println("arrstr6");
Cell cell17 = dataSheet.getCell(29,cellCount2);//to get the promo id val
ues in cell
String str17 = cell17.getContents();
arrstr6[cellCount2] = str17;
//
//

//
//
//
//
//
//

//resultPid.add(str9);
String str7 = arrPid.get(cellCount2);
//System.out.println("str9 "+resultPid.get(r1));
//r1++;
System.out.println("value y" +y);
}
}
}
//System.out.println("out1");
} while(cellCount3 !=0);
System.out.println("out2");
} while(cellCount2 !=0);
System.out.println("out3");
int i10 = 1;
for(String Str11 : resultPid)
{
System.out.println("resultpid "+Str11);
System.out.println("Count "+i10);
i10++;
}
dataWorkbook.close();
checkWorkbook.close();

arrPid.clear();
arrId5.clear();
//adding values in csv file
//BufferedReader addedBuffer = new BufferedReader(new FileReader(add
edVendorFile.getAbsolutePath()));
try{
File outputFile = new File("result.csv");
BufferedWriter newFileBuffer = new BufferedWriter(new FileWriter(out
putFile));
if(outputFile.exists())
{
outputFile.delete();
outputFile.createNewFile();
}
else{
outputFile.createNewFile();
}
}
catch(IOException e)
{
e.printStackTrace();
}
//
//
//
//
//
//
//
//
//
//
//
//
//

for(String str10 : resultPid)


{
newFileBuffer.write(str10);
newFileBuffer.newLine();
}
}
newFileBuffer.close();
}
catch (FileNotFoundException ex) {
System.out.println(ex);
}
System.out.println("arr2length" +arrstr2.length);
try
{ System.out.println("writing");
FileWriter writer = new FileWriter("result.csv");
writer.append("PromoId");
writer.append(',');
writer.append("Quantity");
writer.append(',');
writer.append("Discount");
writer.append(',');
writer.append("SourceId");
writer.append('\n');
for(int count = 0; i<arrstr1.length; count++)
{
System.out.println("arrstr2 "+arrstr2[count]);
writer.append(arrstr2[count]);
writer.append(',');
writer.append(arrstr3[count]);
writer.append(',');
writer.append(arrstr4[count]);

writer.append(',');
writer.append(arrstr1[count]);
writer.append('\n');
}
//
//
//
//
//
//
//

for(String strRead : resultPid)


{
writer.append(strRead);
//writer.append(',');
//writer.append("Age");
writer.append('\n');
}
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
//System.out.println("excel closed");
}
}

You might also like