简体   繁体   English

使用 Apache POI 将多个 csv 复制到 java 中的 xls 文件时出现问题

[英]Issue copying multiple csv to xls files in java using Apache POI

I'm very new in this blog and also in IT field.我是这个博客的新手,也是 IT 领域的新手。 I've a problem in copy the values from a csv file to existing a xls file.我在将 csv 文件中的值复制到现有 xls 文件时遇到问题。 I am using Apache POI.我正在使用 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 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为此编写了代码。它工作正常

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. 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将两个文件的值写入 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;
            }


            }


   }

The simple problem with your code is that the you are not reinitializing arlist.您的代码的简单问题是您没有重新初始化 arlist。

Because of which, the data of CSV1 and well as CSV2 both getting copied into output1.xls.因此,CSV1 和 CSV2 的数据都被复制到 output1.xls 中。

You need to add arlist.clear();您需要添加 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.希望这可以帮助。

The problem is that every file csv file you parse in the while-loop adds appends data to "arlist".问题是您在while循环中解析的每个文件csv文件都将附加数据添加到“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.在下一个循环中,您再次遍历整个“arlist”,从而从您解析的所有先前文件中获取值。 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();编辑:或者你可以做arlist.Clear(); Might even be better...甚至可能会更好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM