-
Java and Excel
I've managed to write Data into Excel using output stream from a Text File. but i'd like to keep the header and format of the existing Excel sheet the same and import the data from the text file to replace the rest of the data, can someone help please?!
Following is part of my code:
public void actionPerformed(ActionEvent e){
try{
File fset = new File("Settings.txt");
File f = new File(txtlstDrp.getSelectedValue().toString());
File fxcl = new File(xcllstDrp.getSelectedValue().toString());
FileInputStream fin = new FileInputStream(f);
DataInputStream din = new DataInputStream(fin);
FileInputStream finxcl = new FileInputStream(fxcl);
DataInputStream dinxcl = new DataInputStream(finxcl);
FileOutputStream fout = new FileOutputStream(fxcl);
DataOutputStream dout = new DataOutputStream(fout);
FileInputStream finset = new FileInputStream(fset);
DataInputStream dinset = new DataInputStream(finset);
boolean fnfound = false;
String Targfile=null;
String improw = null;
while (finset.available()!=0){
String lnfnm =dinset.readLine();
StringTokenizer tok = new StringTokenizer(lnfnm, ";");
while (tok.hasMoreTokens()){
String fnm = tok.nextToken();
if(txtlstDrp.getSelectedValue().toString().contain s(fnm)){
Targfile=tok.nextToken();
improw=tok.nextToken();
fnfound=true;
break;
}
}
if(fnfound){
break;
}
}
int lnnum = Integer.parseInt(improw);
for(int i=0; i<lnnum ; i++){
}
while(fin.available()!=0){
/*String line = din.readLine();
StringTokenizer tok = new StringTokenizer(line,";");
while (tok.hasMoreTokens()){
//write into excel sheet
String xclstr = tok.nextToken();
dout.writeChars(xclstr+"\t");
System.out.println(xclstr+"\t");
}*/
String line = din.readLine();
dout.writeChars(line.replace(';', '\t'));
//new row
dout.writeChars("\n");
}
din.close();
fin.close();
fout.close();
dout.close();
}catch(IOException ex){
JOptionPane.showMessageDialog(null, "Couldn't Open the Selected File!");
}
}
-
You could use the library jxl.jar to read and write directly from and to Excel file format.
-
This is a tutorial to learn how to read and write Excel with the jxl.jar library: Excel and Java - Read and Write Excel with Java