List of usage examples for org.apache.commons.csv CSVFormat EXCEL
CSVFormat EXCEL
To view the source code for org.apache.commons.csv CSVFormat EXCEL.
Click Source Link
From source file:com.example.sihan.restaurantrecommendation.Function.FileResource.java
public CSVParser getCSVParser(boolean withHeader, String delimiter) { if (delimiter == null || delimiter.length() != 1) { throw new ResourceException("FileResource: CSV delimiter must be a single character: " + delimiter); }//from www . j av a 2 s . c o m try { char delim = delimiter.charAt(0); Reader input = new StringReader(mySource); if (withHeader) { return new CSVParser(input, CSVFormat.EXCEL.withHeader().withDelimiter(delim)); } else { return new CSVParser(input, CSVFormat.EXCEL.withDelimiter(delim)); } } catch (Exception e) { throw new ResourceException("FileResource: cannot read " + myPath + " as a CSV file."); } }
From source file:com.house365.build.util.CsvUtil.java
/** * ?CSV.//from w ww. j av a 2s . c o m * * @param channelsFile * @param headerRow * @param isPrint ??.. * @return ?Csv? * @throws FileNotFoundException */ public static ArrayList<LinkedHashMap<String, String>> readCsvChannels(File channelsFile, int headerRow, boolean isPrint) throws Exception { try { ArrayList<LinkedHashMap<String, String>> csvContent = new ArrayList<>(); LinkedHashMap<String, String> rowContent; ArrayList<String> headerNames = null; if (headerRow > 0) { String[] csvHeader = getCsvHeader(channelsFile, headerRow); List<String> list = Arrays.asList(csvHeader); headerNames = new ArrayList<>(list); } AutoDetectReader reader = null; try { reader = new AutoDetectReader(new FileInputStream(channelsFile)); Iterable<CSVRecord> csvRecords = CSVFormat.EXCEL.parse(reader); int i = 0; for (CSVRecord record : csvRecords) { i++; if (i <= headerRow) { continue; } if (headerNames == null) { headerNames = new ArrayList<>(); for (i = 0; i < record.size(); i++) { headerNames.add(i + ""); } } rowContent = new LinkedHashMap<>(); for (i = 0; i < record.size(); i++) { rowContent.put(headerNames.get(i), record.get(i)); } csvContent.add(rowContent); } } finally { try { if (reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (isPrint) { printlnCsv(csvContent); } return csvContent; } catch (IOException e) { throw e; } catch (TikaException e) { throw e; } }
From source file:com.spectralogic.ds3cli.views.csv.CsvOutput.java
public String toString() { try (final StringWriter writer = new StringWriter(); final CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL)) { csvPrinter.printRecord(headers); for (final E content : csvContents) { csvPrinter.printRecord(contentFormatter.format(content)); }//from w ww. j a v a 2 s .c o m return writer.toString(); } catch (final IOException e) { throw new RuntimeException("Failed to write csv", e); } }
From source file:com.ibm.util.merge.directive.provider.ProviderCsv.java
/** * Retrieve the data (superclass HTTP Provider) and parse the CSV data * @param cf/*from w w w . j av a2 s.c o m*/ */ @Override public void getData(MergeContext rtc) throws MergeException { // Get the data super.getData(rtc); DataTable newTable = new DataTable(); CSVParser parser; try { parser = new CSVParser(new StringReader(getFetchedData()), CSVFormat.EXCEL.withHeader()); for (String colName : parser.getHeaderMap().keySet()) { newTable.addCol(colName); } for (CSVRecord record : parser) { ArrayList<String> row = newTable.addNewRow(); for (String field : record) { row.add(field); } } parser.close(); } catch (IOException e) { throw new MergeException(this, e, "CSV Parser Stringreader IO Exception", getFetchedData()); } if (newTable.size() > 0) { getTables().add(newTable); } }
From source file:br.edimarmanica.weir2.rule.type.RulesDataTypeController.java
/** * * @param site//from ww w. j a v a 2s . c o m * @return the type of the rules of the site that was persisted */ public static Map<String, DataType> load(Site site) { Map<String, DataType> ruleType = new HashMap<>(); try (Reader in = new FileReader(new File(Paths.PATH_WEIR_V2 + "/" + site.getPath() + "/types.csv"))) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) { for (CSVRecord record : parser) { //para cada value String rule = record.get("RULE"); String type = record.get("TYPE"); ruleType.put(rule, DataType.valueOf(type)); } } } catch (FileNotFoundException ex) { Logger.getLogger(RulesDataTypeController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(RulesDataTypeController.class.getName()).log(Level.SEVERE, null, ex); } return ruleType; }
From source file:io.heming.accountbook.util.Importer.java
public int importRecords(File file) throws Exception { Iterable<CSVRecord> csvRecords; List<Record> records = new ArrayList<>(); try (Reader in = new FileReader(file)) { csvRecords = CSVFormat.EXCEL.withQuote('"') .withHeader("ID", "CATEGORY", "PRICE", "PHONE", "DATE", "NOTE").parse(in); DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // ??/* www .j ava 2 s . c o m*/ categoryFacade.clear(); int i = 0; for (CSVRecord csvRecord : csvRecords) { Integer id = Integer.parseInt(csvRecord.get("ID")); Category category = new Category(csvRecord.get("CATEGORY")); Double price = Double.parseDouble(csvRecord.get("PRICE")); String phone = csvRecord.get("PHONE"); Date date = new Date(format.parse(csvRecord.get("DATE")).getTime()); String note = csvRecord.get("NOTE"); // category? if (!categoryFacade.list().contains(category)) { categoryFacade.add(category); } Record record = new Record(category, price, phone, date); record.setId(id); record.setNote(note); records.add(record); } } // ?? recordFacade.clear(); int i = 0; int total = records.size(); for (Record record : records) { recordFacade.add(record); } return records.size(); }
From source file:br.edimarmanica.trinity.intrasitemapping.manual.OffsetToRule.java
private void readMappings() { try (Reader in = new FileReader(Paths.PATH_TRINITY + site.getPath() + "/mappings.csv")) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) { for (CSVRecord record : parser) { if (mappings.containsKey(record.get("OFFSET"))) { mappings.get(record.get("OFFSET")).put(record.get("ATTRIBUTE"), Integer.parseInt(record.get("GROUP"))); } else { Map<String, Integer> map = new HashMap<>(); map.put(record.get("ATTRIBUTE"), Integer.parseInt(record.get("GROUP"))); mappings.put(record.get("OFFSET"), map); }/* w w w . j av a2 s .co m*/ } } } catch (FileNotFoundException ex) { Logger.getLogger(OffsetToRule.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(OffsetToRule.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:br.edimarmanica.weir2.rule.Loader.java
/** * * @param site//from w w w . j a v a2 s. c om * @return Map<Page,Entity> */ public static Map<String, String> loadEntityID(Site site) { Map<String, String> ids = new HashMap<>(); try (Reader in = new FileReader(Paths.PATH_BASE + site.getEntityPath())) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) { for (CSVRecord record : parser) { String url = formatURL(record.get("url")); ids.put(url, record.get("entityID")); } } } catch (FileNotFoundException ex) { Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex); } return ids; }
From source file:br.edimarmanica.trinity.intrasitemapping.manual.Mapping.java
private List<Map<String, String>> readOffset(File offsetFile) { List<Map<String, String>> offset = new ArrayList<>(); //cada arquivo um offset try (Reader in = new FileReader(offsetFile)) { 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 = Formatter.formatValue(Preprocessing.filter(record.get(nrRegra))); } catch (InvalidValue ex) { value = ""; }/*from w ww . jav a 2 s. c om*/ if (nrRegistro == 0) { Map<String, String> regra = new HashMap<>(); regra.put(Formatter.formatURL(record.get(0)), value); offset.add(regra); } else { offset.get(nrRegra).put(Formatter.formatURL(record.get(0)), value); } } nrRegistro++; } } } catch (FileNotFoundException ex) { Logger.getLogger(Mapping.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Mapping.class.getName()).log(Level.SEVERE, null, ex); } return offset; }
From source file:biz.itcons.wsdm.hw2_2.NaiveBayes.java
/** * Fills NaiveBayes with data for classification. * * @param file a CSV file with documents that are classified. * @param classifierPos a position in CSV file where classification is * stored (zero based indexing)/*from w w w . j ava 2 s . c o m*/ * @param documentPos a position in CSV file where document body is stored * (zero based indexing) * @throws FileNotFoundException * @throws IOException */ public void trainingDataSet(String file, int classifierPos, int documentPos) throws FileNotFoundException, IOException { try (Reader in = new FileReader(file)) { LOGGER.debug("Opening training data set: " + file); globalTrainingCount = 0; for (CSVRecord record : CSVFormat.EXCEL.parse(in)) { String classifier = record.get(classifierPos); globalTrainingCount++; if (parsedEntries.containsKey(classifier)) { parsedEntries.get(classifier).addDocument(record.get(documentPos)); } else { ClassificationItem ci = new ClassificationItem(classifier); ci.addDocument(record.get(documentPos)); parsedEntries.put(classifier, ci); } } LOGGER.trace("Read " + globalTrainingCount + " from training set"); } }