我能够使用以下java代码为一个Excel工作簿中的文件夹中存在的不同.csv文件创建工作表但是我将所有csv文件数据的数据输入到单个工作表中。 我无法将原始.csv文件中的数据与指定的工作表
相同public class CreateExlFile22{
public static void main(String[]args){
try{
HSSFWorkbook workbook=new HSSFWorkbook();
File folder = new File("F:/csvfiles/");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
System.out.println(file.getName());
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
HSSFSheet sheet = workbook.createSheet(file.getName());
FilenameFilter filter = new FilenameFilter()
{
@Override public boolean accept(File dir, String name)
{
return name.endsWith(".csv");
}
};
File file1[]=folder.listFiles(filter);
for(int r=0;r<file1.length;r++){
File currentFile=file1[r];
FileInputStream fis = new FileInputStream(currentFile);
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();
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++) {
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
FileOutputStream fileOut = new FileOutputStream("F://NewWBFile.xls");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
br.close();
}
}
}
System.out.println("Your excel file has been generated!");
} catch ( Exception ex ) {
System.out.println(ex);
}
}
}
答案 0 :(得分:2)
public class csvxls{
public static void main(String[]args){
try{
File folder = new File("F:/csvfiles/");
File[] listOfFiles = folder.listFiles();
HSSFWorkbook workbook=new HSSFWorkbook();
for (File file : listOfFiles) {
if (file.isFile()) {
String thisline;
ArrayList<String> al = null;
ArrayList<ArrayList<String>> arlist = new ArrayList<ArrayList<String>>();
HSSFSheet sheet = workbook.createSheet(file.getName());
FileInputStream fis = new FileInputStream(file);
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++) {
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++) {
HSSFCell cell = row.createCell((short) p);
cell.setCellValue(ardata.get(p).toString());
}
}
al.add(strar[j]);
}
arlist.add(al);
}
fis.close();
FileOutputStream fileOut = new FileOutputStream("F://NewWBFile.xls");
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
br.close();
}
}
System.out.println("Your excel file has been generated!");
} catch ( Exception ex ) {
System.out.println(ex);
}
}
}
答案 1 :(得分:1)
此逻辑导致多次循环迭代,并且跳过了输入CSV的最后一行,更好地单独准备内容,然后准备excel内容
您还可能要替换不推荐使用的方法
'_original_b'