I'm very new in this blog and also in IT field. I've a problem in copy the values from a csv file to existing a xls file. I am using Apache POI. I've two csv files and two existing xls file output0.xls and output1.xls for the respective csv file.I am trying to copy the values from 1st csv file to output0.xls and 2nd csv file to output1.xls.I ve made the code for that.and it works fine
But the problem is that after putting the value of 1st csv file to output0.xls when from the 2nd csv file I am trying to copy the values to output1.xls, it appends the values of the first csv file with the 2nd csv file and writes the values of both the files to the output1.xls.
I am not able to find out where is the main problem.Please anybody help me as soon as possible.... otherwise I ll face a big problem.
The code have used is
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class Demo {
// FileOutputStream fileOut=null;
//FileInputStream fis=null;
public static void main(String[] args) throws IOException {
/*File f=new File("c:\\");
// return boolean-->System.out.println(f.isDirectory());
File[]files=f.listFiles();*/
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
String libRoot=new File(".").getAbsolutePath();
libRoot=libRoot.replaceAll("\\\\", "/");
File f=new File(libRoot+"/result");
FilenameFilter filter = new FilenameFilter()
{
@Override public boolean accept(File dir, String name)
{
return name.endsWith(".csv");
}
};
File file[]=f.listFiles(filter);
for(int r=0;r<file.length;r++){
File currentFile=file[r];
FileInputStream fis = new FileInputStream(currentFile);
//DataInputStream myInput = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((thisline = br.readLine()) != null) {
al = new ArrayList<String>();
String strar[] = thisline.split(",");
for (int j = 0; j < strar.length; j++) {
al.add(strar[j]);
}
arlist.add(al);
//i++;
}
fis.close();
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet"+r);
for (int k = 0; k < arlist.size(); k++) {
ArrayList<String> ardata = (ArrayList<String>) arlist.get(k);
HSSFRow row = sheet.createRow((short) k);
for (int p = 0; p < ardata.size(); p++) {
//System.out.print(ardata.get(p));
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
FileOutputStream fileOut = new FileOutputStream(libRoot+"/result/output"+r+".xls");
hwb.write(fileOut);
fileOut.flush();
fileOut.close();
br.close();
//hwb=null;
}
}
}