Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6003379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:06:47+00:00 2026-05-23T01:06:47+00:00

I’m very new in this blog and also in IT field. I’ve a problem

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


            }


   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:06:48+00:00Added an answer on May 23, 2026 at 1:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.