Example usage for org.apache.commons.csv CSVFormat EXCEL

List of usage examples for org.apache.commons.csv CSVFormat EXCEL

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat EXCEL.

Prototype

CSVFormat EXCEL

To view the source code for org.apache.commons.csv CSVFormat EXCEL.

Click Source Link

Document

Excel file format (using a comma as the value delimiter).

Usage

From source file:br.edimarmanica.trinity.intrasitemapping.auto.MergeOffsets.java

private void executeOffset(int indexOffset) {
    File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset");
    try (Reader in = new FileReader(dir.getAbsoluteFile() + "/result_" + indexOffset + ".csv")) {
        List<List<String>> lines = new ArrayList<>();
        try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL)) {

            int indexRegistro = 0;
            for (CSVRecord record : parser) {
                if (indexOffset != 0 && indexRegistro < Extract.NR_SHARED_PAGES) { //seno vai extrair repetido
                    indexRegistro++;//  ww  w  .  j  av  a 2 s  . com
                    continue;
                }
                List<String> line = new ArrayList<>();
                for (int nrRegra = 0; nrRegra < record.size(); nrRegra++) {
                    try {
                        line.add(Preprocessing.filter(record.get(nrRegra)));
                    } catch (InvalidValue ex) {
                        line.add("");
                    }
                }
                lines.add(line);
                indexRegistro++;
            }

            print(indexOffset, lines);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MergeOffsets.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MergeOffsets.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:cma.fa.tc.impl.utils.files.SimpleCsvReader.java

@Override
public List<List<String>> read() {
    List<List<String>> result = new ArrayList<>();

    try (BufferedReader in = new BufferedReader(
            new InputStreamReader(this.getClass().getResourceAsStream(this.path), this.charset))) {
        Iterable<CSVRecord> parser = CSVFormat.EXCEL.parse(in);
        for (CSVRecord record : parser) {
            result.add(Lists.newArrayList(record.iterator()));

        }//from   w  w  w .  j ava2 s.c o m

    } catch (Exception ex) {
        log.error("Cannot read {}", ex);
    }

    return result;
}

From source file:br.edimarmanica.trinity.intrasitemapping.auto.MappingController.java

private void reading() {
    /**//from w w  w  .  j av  a2  s .co m
     * Lendos os Run02.NR_SHARED_PAGES primeiros elementos de cada offset
     */
    File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset");

    for (int nrOffset = 0; nrOffset < dir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".csv");
        }
    }).length; nrOffset++) {
        List<List<String>> offset = new ArrayList<>(); //cada arquivo  um offset

        try (Reader in = new FileReader(dir.getAbsoluteFile() + "/result_" + nrOffset + ".csv")) {
            try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL)) {
                int nrRegistro = 0;
                for (CSVRecord record : parser) {
                    if (nrRegistro >= Extract.NR_SHARED_PAGES) {
                        break;
                    }

                    for (int nrRegra = 0; nrRegra < record.size(); nrRegra++) {
                        if (nrRegistro == 0) {
                            List<String> regra = new ArrayList<>();
                            try {
                                regra.add(Preprocessing.filter(record.get(nrRegra)));
                            } catch (InvalidValue ex) {
                                regra.add("");
                            }
                            offset.add(regra);
                        } else {
                            try {
                                offset.get(nrRegra).add(Preprocessing.filter(record.get(nrRegra)));
                            } catch (InvalidValue ex) {
                                offset.get(nrRegra).add("");
                            }
                        }
                    }
                    nrRegistro++;
                }
            }
            offsets.add(offset);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MappingController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MappingController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Mostrando a leitura
     */
    /*for (int i = 1; i < offsets.size(); i++) {
    for (int j = 0; j < 5; j++) {
        System.out.print(offsets.get(i).get(0).get(j) + " - ");
    }
    System.out.println("");
    }*/
}

From source file:br.edimarmanica.trinity.intrasitemapping.manual.AllMappings.java

private void print(List<String> dataRecord) {
    /**//from  www .  j  a  v  a  2  s  .com
     * ********************** results ******************
     */
    File dir = new File(Paths.PATH_TRINITY + "/" + site.getPath());
    dir.mkdirs();

    File file = new File(dir.getAbsolutePath() + "/mappings.csv");
    CSVFormat format;
    if (append) {
        format = CSVFormat.EXCEL;
    } else {
        format = CSVFormat.EXCEL.withHeader(header);
    }

    try (Writer out = new FileWriter(file, append)) {
        try (CSVPrinter csvFilePrinter = new CSVPrinter(out, format)) {
            csvFilePrinter.printRecord(dataRecord);
        }
    } catch (IOException ex) {
        Logger.getLogger(Printer.class.getName()).log(Level.SEVERE, null, ex);
    }

    append = true;
}

From source file:com.nordpos.device.csv.FileCSVInputOutput.java

public FileCSVInputOutput(File inFile, File outFile) throws DeviceInputOutputException {
    format = CSVFormat.EXCEL.withHeader(PRODUCT_CODE, PRODUCT_NAME, PRODUCT_QUANTITY, PRODUCT_PRICESELL)
            .withDelimiter(SEPARATOR).withIgnoreEmptyLines().withQuote(QUOTE).withSkipHeaderRecord();
    this.inFile = inFile;
    this.outFile = outFile;
}

From source file:edu.uiowa.icts.bluebutton.dao.LabTestRangeHome.java

@Override
public void importCSV(InputStream fileInputStream) throws IOException {
    Reader in = new BufferedReader(new InputStreamReader(fileInputStream));
    Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader("LAB_TEST_RANGES_UID", "LAB_TEST_ID", "SEX",
            "MIN_AGE_YEARS", "MAX_AGE_YEARS", "MIN_NORMAL", "MAX_NORMAL").withSkipHeaderRecord(true).parse(in);
    for (CSVRecord record : records) {
        LabTestRange labTestRange = new LabTestRange();
        labTestRange.setLabTestRangeId(new Integer(record.get("LAB_TEST_RANGES_UID")));

        LabTest labTest = (LabTest) this.sessionFactory.getCurrentSession().get(LabTest.class,
                new Integer(record.get("LAB_TEST_ID")));
        labTestRange.setLabTest(labTest);
        //         labTest.getLabTestRanges().add(labTestRange);
        //         this.sessionFactory.getCurrentSession().save(labTest);   

        labTestRange.setSex(record.get("SEX"));
        labTestRange.setMinAgeYears(new Double(record.get("MIN_AGE_YEARS")));
        labTestRange.setMaxAgeYears(new Double(record.get("MAX_AGE_YEARS")));
        labTestRange.setMinNormal(new Double(record.get("MIN_NORMAL")));
        labTestRange.setMaxNormal(new Double(record.get("MAX_NORMAL")));

        this.sessionFactory.getCurrentSession().save(labTestRange);
        this.sessionFactory.getCurrentSession().flush();
        this.sessionFactory.getCurrentSession().clear();
    }//  w  ww .jav  a 2  s.c o  m

}

From source file:com.apkcategorychecker.json.DefaultJsCSVBuilder.java

@Override
public void BuildJs(ArrayList<JsElement> jsList, String _destinationPath, String time) {

    try {//from  ww  w.  j av  a2s.  c  o m

        /*Check if _destinationPath is an APK or a Directory in order to
        *get the right destination path
        */

        File Destination = new File(_destinationPath);
        if (!Destination.exists()) {
            Destination.mkdir();
        }
        if (Destination.isDirectory()) {
            this._destPath = _destinationPath;
        } else if (Destination.isFile()) {
            this._destPath = _destinationPath.substring(0, _destinationPath.length() - 4);
        }

        /*--Create the CSVFormat object--*/

        CSVFormat format = CSVFormat.EXCEL.withHeader().withDelimiter(',');

        /*--Writing in a CSV file--*/

        File _fileCSV = new File(_destPath + "/Results-JS FILES_" + time + ".csv");
        FileWriter _out = new FileWriter(_fileCSV);
        CSVPrinter printer;
        printer = new CSVPrinter(_out, format.withDelimiter('#'));
        System.out.println("Creating " + "Results-JS FILES_" + time + ".csv ....");
        try {
            printer.printRecord("appID", "jsFiles");
        } catch (IOException ex) {
            Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
        }

        /*--Retrieve APKResult and Write in file--*/

        @SuppressWarnings("unused")
        int i = 0;
        Iterator<JsElement> it = jsList.iterator();
        while (it.hasNext()) {
            JsElement _resultElement = it.next();
            List<String> resultData = new ArrayList<>();
            resultData.add(_resultElement.get_Id());
            resultData.add(_resultElement.get_JsFiles());
            i++;
            printer.printRecord(resultData);
        }

        /*--Close the printer--*/
        printer.close();
        System.out.println("Results-JS FILES_" + time + ".csv created");

    } catch (IOException ex) {
        Logger.getLogger(WriterCSV.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:io.viewserver.adapters.csv.CsvDataAdapter.java

protected CSVFormat getCsvFormat() {
    return CSVFormat.EXCEL.withHeader();
}

From source file:loternik.MyService.java

public ArrayList<CSVRecord> getRecords(String teryt, String okreg) throws FileNotFoundException, IOException {
    FileReader file = new FileReader("kandydaci.csv");
    CSVParser parser = new CSVParser(file, CSVFormat.EXCEL.withHeader().withDelimiter(';'));
    ArrayList<CSVRecord> output = new ArrayList<CSVRecord>();
    for (CSVRecord csvRecord : parser) {
        if (teryt.equals(csvRecord.get("teryt")) && okreg.equals(csvRecord.get("Okreg"))) {
            output.add(csvRecord);//  ww  w .j  a  va 2 s  . c  o m
        }
    }
    return output;
}

From source file:edu.uiowa.icts.bluebutton.dao.LoincCodeCategoryHome.java

@Override
public void importCSV(InputStream fileInputStream) throws IOException {
    Reader in = new BufferedReader(new InputStreamReader(fileInputStream));
    Iterable<CSVRecord> records = CSVFormat.EXCEL
            .withHeader("PATH_TO_ROOT", "SEQUENCE", "IMMEDIATE_PARENT", "CODE", "CODE_TEXT")
            .withSkipHeaderRecord(true).parse(in);
    Map<String, String> codeTextMap = new HashMap<String, String>();
    for (CSVRecord record : records) {
        LoincCodeCategory lcc = new LoincCodeCategory();
        lcc.setLoincCode(record.get("CODE"));
        lcc.setName(record.get("CODE_TEXT"));
        codeTextMap.put(record.get("CODE"), record.get("CODE_TEXT"));
        if (!record.get("PATH_TO_ROOT").equals("")) {
            String[] rootName = record.get("PATH_TO_ROOT").split("\\.");
            lcc.setRootCategoryName(codeTextMap.get(rootName[0]));
            if (rootName.length > 1) {
                lcc.setSubrootCategoryName(codeTextMap.get(rootName[1]));
            }//from   w w w.  j  ava  2  s.c o m
        }
        this.sessionFactory.getCurrentSession().save(lcc);
    }

}