2017-08-18 95 views
0

嗨有谁知道如何通过下面的脚本循环目录中的文件?我曾经尝试过,但我不知道如何给一个变量ASIGN以下行:Java循环通过文件将csv转换为xls

CSVReader reader = new CSVReader(new FileReader(file)); 

它必须是一个字符串至极弹出语法错误。

import java.io.*; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.*; 
import au.com.bytecode.opencsv.CSVReader; 

class Test { 
    public static void main(String[] args) throws IOException { 
     Workbook wb = new HSSFWorkbook(); 
     CreationHelper helper = wb.getCreationHelper(); 
     Sheet sheet = wb.createSheet("new sheet"); 

     CSVReader reader = new CSVReader(new FileReader("data.csv")); 
     String[] line; 
     int r = 0; 
     while ((line = reader.readNext()) != null) { 
      Row row = sheet.createRow((short) r++); 

      for (int i = 0; i < line.length; i++) 
       row.createCell(i) 
        .setCellValue(helper.createRichTextString(line[i])); 
     } 

     // Write the output to a file 
     FileOutputStream fileOut = new FileOutputStream("workbook.xls"); 
     wb.write(fileOut); 
     fileOut.close(); 
    } 
} 
+0

花一些时间学习java –

回答

2

嗨,我不知道我是否正确理解你的问题。 但我会建议尝试这样的: 您可以使用路径和文件类来循环通过一个目录。 然后你可以使用你的代码编辑文件

import java.io.*; 
import java.nio.file.DirectoryStream; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.*; 
import au.com.bytecode.opencsv.CSVReader; 

class Test { 

    public static void main(String[] args) throws IOException { 
// here you enter the path to your directory. 
// for example: Path workDir = Paths.get("c:\\workspace\\csv-files") 

     Path workDir = Paths.get("path/to/dir"); 
// the if checks whether the directory truly exists 
     if (!Files.notExists(workDir)) { 
// this part stores all files withn the directory in a list 
      try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(workDir)) { 
       for (Path path : directoryStream) { 
        Workbook wb = new HSSFWorkbook(); 
        CreationHelper helper = wb.getCreationHelper(); 
        Sheet sheet = wb.createSheet("new sheet"); 
// here you insert the name of the file (stored in the Path object) into your method 
        CSVReader reader = new CSVReader(new FileReader(path.toString())); 
        String[] line; 
        int r = 0; 
        while ((line = reader.readNext()) != null) { 
         Row row = sheet.createRow((short) r++); 

         for (int i = 0; i < line.length; i++) 
          row.createCell(i) 
            .setCellValue(helper.createRichTextString(line[i])); 
        } 

        // Write the output to a file 
        FileOutputStream fileOut = new FileOutputStream(path.getName().replaceAll("csv", "xls")); 
        wb.write(fileOut); 
        fileOut.close(); 
       } 

      } catch (IOException e) { 
       System.out.println(e.getMessage()); 
      } else { 
       System.out.println("directory " + workDir.toString() + " does not exist") 

     } 

    } 
} 
+0

进出口新的Java和不知道如何使用您的解决方案 – euranoo

+0

嗨,我添加了注释说明的部分。你能指定你不明白的部分吗? –

+0

管理让您的代码正常工作。然而,我运行以下错误:C:\ Users \ Kamil \ Desktop \ xlstoxlspython \ Nowy文件夹(2)\ apteka_20170802.xls(找不到路径)我相信它的第一个文件在这个目录中,但是它的csv文件不是xls,所以我不理解这个。 – euranoo