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:edu.uiowa.icts.bluebutton.dao.LabTestHome.java

@Override
public void importCSV(InputStream fileInputStream) throws IOException {
    Reader in = new BufferedReader(new InputStreamReader(fileInputStream));
    Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader("LAB_TEST_ID", "LAB_TEST_NAME", "TEST_DESCRIPTION",
            "UNITS", "LOINC_NUM", "DATETIME_CREATED", "DATETIME_LASTMODIFIED").withSkipHeaderRecord(true)
            .parse(in);/*from www.j  a v  a 2s . c  o  m*/
    for (CSVRecord record : records) {
        LabTest labTest = new LabTest();
        labTest.setLabTestId(new Integer(record.get("LAB_TEST_ID")));
        labTest.setName(record.get("LAB_TEST_NAME"));
        labTest.setDescription(record.get("TEST_DESCRIPTION"));
        labTest.setUnits(record.get("UNITS"));
        labTest.setLoincCode(record.get("LOINC_NUM"));
        labTest.setDateCreated(record.get("DATETIME_CREATED"));
        labTest.setDateUpdated(record.get("DATETIME_LASTMODIFIED"));

        this.sessionFactory.getCurrentSession().save(labTest);
    }

}

From source file:co.cask.hydrator.transforms.ParseCSV.java

@Override
public void initialize(TransformContext context) throws Exception {
    super.initialize(context);

    String csvFormatString = config.format.toLowerCase();
    switch (csvFormatString) {
    case "default":
        csvFormat = CSVFormat.DEFAULT;/*  w  w w.java 2 s . c  o  m*/
        break;

    case "excel":
        csvFormat = CSVFormat.EXCEL;
        break;

    case "mysql":
        csvFormat = CSVFormat.MYSQL;
        break;

    case "rfc4180":
        csvFormat = CSVFormat.RFC4180;
        break;

    case "tdf":
        csvFormat = CSVFormat.TDF;
        break;

    default:
        throw new IllegalArgumentException(
                "Format {} specified is not one of the allowed format. Allowed formats are"
                        + "DEFAULT, EXCEL, MYSQL, RFC4180 and TDF");
    }

    try {
        outSchema = Schema.parseJson(config.schema);
        fields = outSchema.getFields();
    } catch (IOException e) {
        throw new IllegalArgumentException("Format of schema specified is invalid. Please check the format.");
    }
}

From source file:com.itemanalysis.jmetrik.data.Scorer.java

private void readScoringFile(String fileName) throws IOException {
    File f = new File(fileName);
    CSVParser parser = null;//from   ww w.  j  a va 2s.c  o m
    Reader reader = null;
    GenericItemScoring itemScoring = null;
    SpecialDataCodes specialCodes = null;
    String name = "";
    String option = "";
    int score = 0;
    try {
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(f)), "UTF-8");
        parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());

        for (CSVRecord csvRecord : parser) {
            name = csvRecord.get("name");
            option = csvRecord.get("option");
            score = Integer.parseInt(csvRecord.get("score"));

            itemScoring = new GenericItemScoring(name);
            itemScoring.addCategory(option, score);

            specialCodes = new SpecialDataCodes();

            if (csvRecord.isMapped("missing"))
                specialCodes.setMissingDataCode(csvRecord.get("missing"));
            if (csvRecord.isMapped("missing score"))
                specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("missing score")));

            if (csvRecord.isMapped("notreached"))
                specialCodes.setMissingDataCode(csvRecord.get("notreached"));
            if (csvRecord.isMapped("notreached score"))
                specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("notreached score")));

            if (csvRecord.isMapped("omitted"))
                specialCodes.setMissingDataCode(csvRecord.get("omitted"));
            if (csvRecord.isMapped("omitted score"))
                specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("omitted score")));

            scoring.put(name, itemScoring);
        }
    } catch (IOException ex) {
        throw (ex);
    } finally {
        parser.close();
        reader.close();
    }

}

From source file:edu.clemson.lph.civet.addons.VspsCviFile.java

private void saveme(Window parent, File fIn) {
    try {//  w  w w. j  a  va2 s.co  m
        File fOut = fixCSV(fIn);
        CSVParser parserIn = new CSVParser(new FileReader(fOut), CSVFormat.EXCEL);
        parser = new LabeledCSVParser(parserIn);
        aCols = parser.getNext();
    } catch (FileNotFoundException e) {
        logger.error(e.getMessage() + "\nCould not read file: " + fIn.getName());
    } catch (IOException e) {
        logger.error(e.getMessage() + "\nCould not read file: " + fIn.getName());
    }
    InsertVspsCviThread thread = new InsertVspsCviThread(parent, this);
    thread.start();
}

From source file:br.edimarmanica.weir2.check.CheckDistanceExpectedMapping.java

private String getMasterRule(Site site, Attribute attribute) {
    try (Reader in = new FileReader(Paths.PATH_INTRASITE + "/" + site.getPath() + "/result.csv")) {
        try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) {

            for (CSVRecord record : parser) {
                if (record.get("ATTRIBUTE").equals(attribute.getAttributeID())) {
                    if (record.get("RULE").equals("Attribute not found")) {
                        return null;
                    }/*from  w  ww .  j ava2 s. c o  m*/
                    return record.get("RULE");
                }
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CheckDistanceExpectedMapping.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CheckDistanceExpectedMapping.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:br.edimarmanica.trinity.check.CheckAttributeNotFound.java

private void readOffSets() {
    /**/*ww  w  . j  a  va  2  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<Map<String, 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) {

                    for (int nrRegra = 0; nrRegra < record.size(); nrRegra++) {
                        String value;
                        try {
                            value = formatValue(Preprocessing.filter(record.get(nrRegra)));
                        } catch (InvalidValue ex) {
                            value = "";
                        }

                        if (nrRegistro == 0) {
                            Map<String, String> regra = new HashMap<>();
                            regra.put(record.get(0), value);
                            offset.add(regra);
                        } else {
                            offset.get(nrRegra).put(record.get(0), value);
                        }
                    }
                    nrRegistro++;
                }
            }
            offsets.add(offset);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CheckAttributeNotFound.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CheckAttributeNotFound.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:de.tynne.benchmarksuite.Main.java

private static void runBenchmarks(Args args, BenchmarkProducer benchmarkProducer) throws IOException {
    BackupHelper.backupIfNeeded(args.getOutput());

    // this looks like NOT comma seperated values, but excel and libreoffice load this automatically
    final CSVFormat format = CSVFormat.EXCEL.withDelimiter(';').withHeader("#", "ID", "Name", "Min [ns]",
            "Avg [ns]", "Max [ns]", "Chart Pos", "Best Increase [%]", "Iterations");
    try (CSVPrinter printer = new CSVPrinter(
            new OutputStreamWriter(new FileOutputStream(args.getOutput()), Charset.forName(args.getCharset())),
            format)) {//from w w  w.  j  a v a2s  . c  om
        List<Benchmark> benchmarks = benchmarkProducer.get();
        List<Benchmark> matching = benchmarks.stream()
                .filter(b -> args.getExecute().matcher(b.getId()).matches()).collect(Collectors.toList());
        BenchmarkRunner benchmarkRunner = new BenchmarkRunner(matching,
                BenchmarkRunner.SEC_IN_NANOS * args.getWarumUpTime(),
                BenchmarkRunner.SEC_IN_NANOS * args.getRunTime());
        benchmarkRunner.run();
        Chart chart = Chart.of(matching);

        for (Benchmark b : matching) {
            try {
                StatRecord statRecord = chart.getStats().get(b);
                printer.print(matching.indexOf(b));
                printer.print(b.getId());
                printer.print(b.getName());
                printer.print(format(args, statRecord.getMin()));
                printer.print(format(args, statRecord.getAverage()));
                printer.print(format(args, statRecord.getMax()));
                printer.print(chart.getChart().get(b).chartPosition);
                double bestAvg = chart.getStats().get(chart.getPerformanceChart().get(0)).getAverage();
                double thisAvg = statRecord.getAverage();

                printer.print(format(args, 100. * (thisAvg - bestAvg) / bestAvg));
                printer.print(statRecord.getCount());
                printer.println();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

From source file:br.edimarmanica.fazenda.control.LoadBBControl.java

private boolean load(Pessoa pessoa, String file) {

    EntityManager em = Conexao.getEntityManager();
    em.getTransaction().begin();//from   w w w  .ja  v  a  2s. c  o m

    boolean error = false;

    try (CSVParser parser = CSVParser.parse(new File(file), Charset.forName("ISO-8859-1"),
            CSVFormat.EXCEL.withHeader())) {
        for (CSVRecord record : parser) {
            TipoCaixa tc = new TipoCaixa();
            tc.setCdBb(new BigInteger(record.get("Nmero do documento")));

            if (tc.getCdBb() != null) {
                List<TipoCaixa> tcs = daoTipo.search(tc);
                if (tcs != null && tcs.size() == 1) {
                    Caixa caixa = new Caixa();
                    caixa.setCdTipoCaixa(tcs.get(0));
                    caixa.setCdPessoa(pessoa);
                    caixa.setVlCaixa((new BigDecimal(record.get("Valor"))).abs());
                    caixa.setDtPagamento(new Date(record.get("Data")));
                    caixa.setDtVencimento(new Date(record.get("Data")));
                    caixa.setDsCaixa("IMPORTAO BB 02");
                    em.persist(caixa);
                }
            }

        }
    } catch (IOException ex) {
        Logger.getLogger(LoadBBControl.class.getName()).log(Level.SEVERE, null, ex);
        error = true;
    }

    if (!error) {
        em.getTransaction().commit();
    } else {
        em.getTransaction().rollback();
    }
    em.close();
    return !error;
}

From source file:loternik.MyService.java

public ArrayList<CSVRecord> getRecordsByName(String name, 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 (name.equals(csvRecord.get("Kandydat_do")) && okreg.equals(csvRecord.get("Okreg"))) {
            output.add(csvRecord);//w  w  w  . jav a 2 s . c o  m
        }
    }
    return output;
}

From source file:co.cask.hydrator.transforms.CSVParser2.java

@Override
public void initialize(TransformContext context) throws Exception {
    super.initialize(context);

    String csvFormatString = config.format.toLowerCase();
    switch (csvFormatString) {
    case "default":
        csvFormat = CSVFormat.DEFAULT;/*from  w w  w  .  j  ava  2s.  c o m*/
        break;

    case "excel":
        csvFormat = CSVFormat.EXCEL;
        break;

    case "mysql":
        csvFormat = CSVFormat.MYSQL;
        break;

    case "rfc4180":
        csvFormat = CSVFormat.RFC4180;
        break;

    case "tdf":
        csvFormat = CSVFormat.TDF;
        break;

    default:
        throw new IllegalArgumentException(
                "Format {} specified is not one of the allowed format. Allowed formats are"
                        + "DEFAULT, EXCEL, MYSQL, RFC4180 and TDF");
    }

    if (config.field == null || config.field.isEmpty()) {
        throw new IllegalArgumentException("Field for applying transformation is not specified.");
    }

    try {
        outSchema = Schema.parseJson(config.schema);
        fields = outSchema.getFields();
    } catch (IOException e) {
        throw new IllegalArgumentException("Format of schema specified is invalid. Please check the format.");
    }
}