0

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;
            }


            }


   }

2 Answers 2

5

The simple problem with your code is that the you are not reinitializing arlist.

Because of which, the data of CSV1 and well as CSV2 both getting copied into output1.xls.

You need to add arlist.clear(); as mentioned in the code below.

The update code would be:

for(int r=0;r<file.length;r++){
    arlist.clear();
    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;
        }


        }

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank u very much Sandeep an Nubsis..........it works with arlist.clear().Thanks u so much.........take care guys....ta ta
4

The problem is that every file csv file you parse in the while-loop adds appends data to "arlist". In the next loop you iterate over the entire "arlist" again and thus get the values from all the previous files you've parsed. I'd suggest you add the row:

arlist = new ArrayList<ArrayList<String>>();

In the beginning of the main loop so it would look something like this:

for(int r=0;r<file.length;r++){
    File currentFile=file[r];
    arlist = new ArrayList<ArrayList<String>>();

Edit: Or you could do arlist.Clear(); Might even be better...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.