This code reads the input text file.It scans each line at a time and separates the words by comma.Each token is placed in a single cell in the excel file. Some times these sorts of silly codes are also useful.
The thing to be noted is that if there is duplicate record in consecutive lines it prevents the duplicates too.
Ex:
test,abcd,efgh,ttt in line1 and
test,abcd,ijkl,sss in line2 then
the entry test abcd is inserted in the first row first column,efgh,ttt,sss in the next columns in the same row.
Similarly other records are proceeded.
I encountered such requirement during my work and thought if someone might require it and posted here.
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.biff.RowsExceededException;
public class Test {
public Test() {
}
private static String[] months=new String[]{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
public static void main(String[] args) throws ParseException, GeneralSecurityException, FileNotFoundException, RowsExceededException, Exception {
Test test1 = new Test();
StringBuilder contents = new StringBuilder();
try {
String[] tempArray=new String[5];
String lastUser="";
//use buffering, reading one line at a time
//FileReader always assumes default encoding is OK!
BufferedReader input = new BufferedReader(new FileReader("abcd.txt"));
try {
String line = null; //not declared within while loop
/*
* readLine is a bit quirky :
* it returns the content of a line MINUS the newline.
* it returns null only for the END of the stream.
* it returns an empty String if two newlines appear in a row.
*/
int lastIndex=0;
int secondLastIndex=0;
int i=0;
int rowNum=0;
int colNum=0;
WritableWorkbook w = Workbook.createWorkbook(new BufferedOutputStream(new FileOutputStream("report.xls")));
WritableSheet s = w.createSheet("Demo", 0);
while (( line = input.readLine()) != null){
tempArray=line.split(",");
if(tempArray.length>1){
lastIndex=tempArray.length-1;
secondLastIndex=lastIndex-1;
if(i<1){
lastUser=tempArray[0]+" "+tempArray[1];
}
if(lastUser.equalsIgnoreCase(tempArray[0]+" "+tempArray[1])){
if(i<1){//this is the first attempt
//System.out.println("first case");
contents.append(lastUser);
s.addCell(new Label(rowNum,colNum, lastUser));
//contents.append(System.getProperty("line.separator"));
rowNum++;
s.addCell(new Label(rowNum,colNum, tempArray[secondLastIndex]));
contents.append(","+tempArray[secondLastIndex]+",");
//contents.append(System.getProperty("line.separator"));
rowNum++;
s.addCell(new Label(rowNum,colNum, tempArray[lastIndex]));
contents.append(tempArray[lastIndex]);
}
else{
String lastSkillEntered=s.getCell(rowNum, colNum).getContents().toString();
System.out.println("inside 1:"+rowNum+colNum+lastSkillEntered+","+tempArray[lastIndex]);
s.addCell(new Label(rowNum,colNum,lastSkillEntered+","+tempArray[lastIndex]));
contents.append(":"+tempArray[lastIndex]);
}
}
else{
colNum++;
rowNum=0;
System.out.println("new entry:"+rowNum+colNum);
s.addCell(new Label(rowNum,colNum, tempArray[0]+" "+tempArray[1]));
contents.append(System.getProperty("line.separator"));
contents.append(tempArray[0]+" "+tempArray[1]);
//contents.append(System.getProperty("line.separator"));
rowNum++;
s.addCell(new Label(rowNum,colNum,tempArray[secondLastIndex]));
contents.append(","+tempArray[secondLastIndex]+",");
//contents.append(System.getProperty("line.separator"));
rowNum++;
s.addCell(new Label(rowNum,colNum,tempArray[lastIndex]));
contents.append(tempArray[lastIndex]);
}
}
lastUser=tempArray[0]+" "+tempArray[1];
i++;
}
System.out.println("contents read:\n"+contents.toString());
w.write();
w.close();
System.out.println("done");
}
catch (IOException ex){
ex.printStackTrace();
}
catch(Exception ex){
System.out.println("Exception:"+ex.getMessage());
}
finally {
input.close();
}
}
finally {
// input.close();
}
}
All the functionality is provided by the jar file jxl.jar.So you need to download this jar file and place in the class path/build path accordingly.
Almost all the things are clear and no more explanation is required.
No comments:
Post a Comment