excel - Java loop through files to convert csv to xls -


hi know how loop through files in directory script below? have tried dont know how asign variable following line:

csvreader reader = new csvreader(new filereader(file)); 

it has string wich pops syntax errors.

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++);              (int = 0; < line.length; i++)                 row.createcell(i)                    .setcellvalue(helper.createrichtextstring(line[i]));         }          // write output file         fileoutputstream fileout = new fileoutputstream("workbook.xls");         wb.write(fileout);         fileout.close();     } } 

hi don't know if correctly understand question. recommend trying this: can use path , files classes loop through directory. can use code editing files

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 enter path directory. // example: path workdir = paths.get("c:\\workspace\\csv-files")          path workdir = paths.get("path/to/dir"); // if checks whether directory exists         if (!files.notexists(workdir )) { // part stores files withn directory in list             try (directorystream<path> directorystream = files.newdirectorystream(workdir )) {                 (path path : directorystream) {                     workbook wb = new hssfworkbook();                     creationhelper helper = wb.getcreationhelper();                     sheet sheet = wb.createsheet("new sheet"); // here insert name of file (stored in path object) 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++);                          (int = 0; < line.length; i++)                             row.createcell(i)                                     .setcellvalue(helper.createrichtextstring(line[i]));                     }                      // write output 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() + " not exist" )          }      } } 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -