List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
From source file:ro.dabuno.office.integration.Data.java
private void readCSVFile(File csvFile) throws IOException { // open file/*from w w w.j a v a2 s . c om*/ // List<String> lines = FileUtils.readLines(file, null); try (Reader reader = new FileReader(csvFile)) { CSVFormat strategy = CSVFormat.DEFAULT.withHeader().withDelimiter(',').withQuote('"') .withCommentMarker((char) 0).withIgnoreEmptyLines().withIgnoreSurroundingSpaces(); try (CSVParser parser = new CSVParser(reader, strategy)) { Map<String, Integer> headerMap = parser.getHeaderMap(); for (Map.Entry<String, Integer> entry : headerMap.entrySet()) { headers.add(entry.getKey()); log.info("Had header '" + entry.getKey() + "' for column " + entry.getValue()); } List<CSVRecord> lines = parser.getRecords(); log.info("Found " + lines.size() + " lines"); for (CSVRecord line : lines) { List<String> data = new ArrayList<>(); for (int pos = 0; pos < headerMap.size(); pos++) { if (line.size() <= pos) { data.add(null); } else { data.add(line.get(pos)); } } values.add(data); } } } }
From source file:ro.pippo.csv.CsvEngine.java
@Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { if (!classOfT.isArray()) { if (Collection.class.isAssignableFrom(classOfT)) { // Collections are NOT supported for deserialization from CSV throw new RuntimeException("Collection types are not supported. Please specify an array[] type."); }/* www .j a v a2 s . co m*/ throw new RuntimeException( String.format("Array[] types are required. Please specify %s[]", classOfT.getName())); } Class<?> objectType = classOfT.getComponentType(); int currentLine = 0; try (CSVParser parser = new CSVParser(new StringReader(content), getCSVFormat().withHeader())) { Set<String> columns = parser.getHeaderMap().keySet(); Map<String, Field> fieldMap = getFieldMap(objectType); Constructor<?> objectConstructor; try { objectConstructor = objectType.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("A default constructor is required for " + objectType.getName()); } List objects = new ArrayList<>(); for (CSVRecord record : parser) { currentLine++; Object o = objectConstructor.newInstance(); for (String column : columns) { Field field = fieldMap.get(caseSensitiveFieldNames ? column : column.toLowerCase()); String value = record.get(column); Object object = objectFromString(value, field.getType()); field.set(o, object); } objects.add(o); } Object array = Array.newInstance(objectType, objects.size()); for (int i = 0; i < objects.size(); i++) { Array.set(array, i, objects.get(i)); } return (T) array; } catch (Exception e) { throw new RuntimeException("Failed to parse CSV near line #" + currentLine, e); } }
From source file:sg.edu.ntu.hrms.service.EmployeeEditService.java
public void uploadEmp(List<FileItem> fields) throws Exception { Session session = sessionFactory.openSession(); Transaction txn = session.beginTransaction(); try {/*w ww. j a v a 2 s .com*/ userDAO.setSession(session); leaveDAO.setSession(session); accessDAO.setSession(session); deptDAO.setSession(session); titleDAO.setSession(session); Iterator<FileItem> it = fields.iterator(); while (it.hasNext()) { FileItem fileItem = it.next(); //store in webserver. String fileName = fileItem.getName(); if (fileName != null) { File file = new File(fileName); fileItem.write(file); System.out.println("File successfully saved as " + file.getAbsolutePath()); //process file Reader in = new FileReader(file.getAbsolutePath()); Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().parse(in); for (CSVRecord record : records) { String name = record.get("<name>"); System.out.println("name: " + name); String login = record.get("<login>"); String title = record.get("<title>"); String email = record.get("<email>"); String role = record.get("<role>"); String dept = record.get("<department>"); String joinDate = record.get("<joinDate>"); String probDate = record.get("<probDate>"); String annLeaveEnt = record.get("<leave_entitlement>"); String annBal = record.get("<leave_bal>"); String annMax = record.get("<leave_max>"); String annCF = record.get("<leave_cf>"); String med = record.get("<med_taken>"); String oil = record.get("<oil_taken>"); String unpaid = record.get("<unpaid_taken>"); String child = record.get("<child_bal>"); TitleDTO titleDto = titleDAO.getTitleByName(title); RoleDTO roleDto = accessDAO.getRole(role); DeptDTO deptDto = deptDAO.getDepartment(dept); //create the user first UserDTO user = new UserDTO(); user.setName(name); user.setLogin(login); user.setTitle(titleDto); user.setEmail(email); user.setDateJoin(Utility.format(joinDate, "dd/MM/yyyy")); user.setProbationDue(Utility.format(probDate, "dd/MM/yyyy")); //store in user table. userDAO.createUser(user); //assign role userDAO.assignRole(user, roleDto); //assign dept deptDAO.assignEmployee(user, deptDto); //leave ent LeaveTypeDTO lvtypeDTO = leaveDAO.getLeaveType("Annual"); LeaveEntDTO annualentDTO = new LeaveEntDTO(); annualentDTO.setCurrent(Double.parseDouble(annLeaveEnt)); annualentDTO.setBalance(Double.parseDouble(annBal)); annualentDTO.setMax(Double.parseDouble(annMax)); annualentDTO.setCarriedOver(Double.parseDouble(annCF)); annualentDTO.setLeaveType(lvtypeDTO); //assign annual leave annualentDTO.setUser(user); leaveDAO.addLeaveEnt(annualentDTO); //medical ent LeaveTypeDTO medTypeDTO = leaveDAO.getLeaveType("Medical Leave"); LeaveEntDTO medentDTO = new LeaveEntDTO(); medentDTO.setBalance(medTypeDTO.getDays() - Double.parseDouble(med)); medentDTO.setCurrent(medTypeDTO.getDays()); medentDTO.setUser(user); medentDTO.setLeaveType(medTypeDTO); leaveDAO.addLeaveEnt(medentDTO); //oil ent LeaveTypeDTO oilTypeDTO = leaveDAO.getLeaveType("Off-in-Lieu"); LeaveEntDTO oilentDTO = new LeaveEntDTO(); oilentDTO.setBalance(oilTypeDTO.getDays() - Double.parseDouble(oil)); oilentDTO.setCurrent(0); oilentDTO.setUser(user); oilentDTO.setLeaveType(oilTypeDTO); leaveDAO.addLeaveEnt(oilentDTO); //unpaid LeaveTypeDTO unpaidTypeDTO = leaveDAO.getLeaveType("Unpaid"); LeaveEntDTO unpaidentDTO = new LeaveEntDTO(); unpaidentDTO.setBalance(unpaidTypeDTO.getDays() - Double.parseDouble(unpaid)); unpaidentDTO.setCurrent(0); unpaidentDTO.setUser(user); unpaidentDTO.setLeaveType(unpaidTypeDTO); leaveDAO.addLeaveEnt(unpaidentDTO); //child LeaveTypeDTO childTypeDTO = leaveDAO.getLeaveType("Child Care"); double cur = childTypeDTO.getDays(); LeaveEntDTO childentDTO = new LeaveEntDTO(); childentDTO.setBalance(cur - Double.parseDouble(child)); childentDTO.setCurrent(cur); childentDTO.setUser(user); childentDTO.setLeaveType(childTypeDTO); leaveDAO.addLeaveEnt(childentDTO); //txn.commit(); } } } txn.commit(); } catch (Exception ex) { txn.rollback(); ex.printStackTrace(); } finally { session.close(); } }
From source file:StatisticLearningProject.DataGrid.java
public DataGrid(File f) { Reader in = null;//from w ww. j a v a 2 s . c o m try { in = new FileReader(f.getAbsolutePath()); } catch (FileNotFoundException e) { loadError = e.getMessage(); return; } try { ArrayList<CSVRecord> records = Lists.newArrayList(CSVFormat.EXCEL.parse(in)); CSVRecord headerRec = records.remove(0); int columns = headerRec.size(); String[] headers = new String[columns]; for (int i = 0; i < columns; i++) { headers[i] = (String) headerRec.get(i); } int rows = records.size(); Object[][] data = new Object[rows][columns]; int count = 0; for (CSVRecord record : records) { for (int i = 0; i < columns; i++) { data[count][i] = record.get(i); } count++; } tableModel = new DefaultTableModel(data, headers); } catch (IOException e) { loadError = e.getMessage(); } }
From source file:stonelake.fleet.FleetDataParser.java
public static void main(String[] args) { try {//from w w w . j a va 2 s . c o m DataSource dataSource = new SimpleDataSource("org.hsqldb.jdbc.JDBCDriver", "jdbc:hsqldb:hsql://localhost/purchasedb", "SA", null); Reader in = new FileReader( "C:/Users/localadmin/ca-project/GreenProject/data/CA_State_Fleet___2011-2014_.csv"); Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().parse(in); //Clear table before re-populating JdbcHelper jdbc = new JdbcHelper(dataSource); //Clear the table /**************88 try { jdbc.holdConnection(); jdbc.execute("delete from ca_state_fleet"); } finally { jdbc.releaseConnection(); } **************/ //Insert new records for (CSVRecord record : records) { //Generate record ID String recordId = UUID.randomUUID().toString(); //Add data elements to database jdbc = new JdbcHelper(dataSource); //Insert new record try { jdbc.holdConnection(); jdbc.execute( "insert into ca_state_fleet (record_id,agency,report_year,disposed,equipment_number,category,model_year ,make_model,vin,license_plate_number,postal_code,vehicle_type_desc,weight_class,passenger_vehicle,payload_rating,shipping_weight,wheel_type,tire_size,fuel_type,engine_configuration,emissions_type,primary_application,secondary_application,acquisition_delivery_date,suv_justification,id4x4_justification,acquisition_method_reason,purchase_price,annul_lease_rate,acquistion_mileage,disposition_date,transferred_to,disposition_method,disposition_reason,disposition_mileage,disposition_sold_amount,fuel_total_fuel_average,total_miles,dec_prior_year ) values" + " (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?)", recordId, record.get("Agency"), record.get("Report Year"), record.get("Disposed"), record.get("Equipment Number"), record.get("Category"), record.get("Model Year "), record.get("Make Model"), record.get("VIN"), record.get("License Plate Number"), record.get("Postal Code"), record.get("Vehicle Type Desc"), record.get("Weight Class"), record.get("Passenger Vehicle On Off Road Owned Leased"), record.get("Payload Rating"), record.get("Shipping Weight"), record.get("Wheel Type"), record.get("Tire Size"), record.get("Fuel Type"), record.get("Engine Configuration"), record.get("Emissions Type"), record.get("Primary Application"), record.get("Secondary Application"), record.get("Acquisition Delivery Date"), record.get("SUV Justification"), record.get("ID4X4 Justification"), record.get("Acquisition Method Reason"), record.get("Purchase Price"), record.get("Annul Lease Rate"), record.get("Acquistion Mileage"), record.get("Disposition Date"), record.get("Transferred To"), record.get("Disposition Method"), record.get("Disposition Reason"), record.get("Disposition Mileage"), record.get("Disposition Sold Amount"), record.get("Fuel Total Fuel Average"), record.get("Total Miles"), record.get("Dec Prior Year ")); } finally { jdbc.releaseConnection(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:strawn.evariant.rainsorter.data.msapop.MSAPopulationLoader.java
/** * Loads and returns a list of all MSAs including their population in 2014 * /*from www .j a v a2 s . c om*/ * @return The list of records. * @throws FileNotFoundException * @throws IOException */ public static List<MSAPopulationRecord> loadRecordsFromDisk() throws FileNotFoundException, IOException { ArrayList<MSAPopulationRecord> toReturn = new ArrayList(); Iterable<CSVRecord> records = getCSVRecords(); for (CSVRecord record : records) { String identifier = record.get("Geography"); identifier = identifier.replace(" Metro Area", ""); String[] indentifierSplit = identifier.split(","); String msaName = indentifierSplit[0].trim(); //String msaState = indentifierSplit[1].trim(); int population = Integer.parseInt(record.get("Population Estimate (as of July 1) - 2014")); toReturn.add(new MSAPopulationRecord(msaName, record.get("Id2"), population)); } return toReturn; }
From source file:strawn.evariant.rainsorter.data.precipitation.PrecipitationLoader.java
/** * Loads and returns all precipitation data for May 2015 * /*from w w w. j a v a 2s.co m*/ * @return The list of precipitation records * @throws FileNotFoundException * @throws IOException */ public static List<PrecipitationRecord> loadRecordsFromDisk() throws FileNotFoundException, IOException { ArrayList<PrecipitationRecord> toReturn = new ArrayList(); Iterable<CSVRecord> records = getCSVRecords(); for (CSVRecord record : records) { int hour = Integer.parseInt(record.get("Hour")); String wban = record.get("Wban"); double inchesOfRain = 0d; boolean isTrace = false; boolean isSuspect = false; String precipitationString = record.get("Precipitation").trim(); if (precipitationString.equals("T")) { isTrace = true; } else if (precipitationString.length() != 0) { inchesOfRain = Double.parseDouble(record.get("Precipitation")); } String noteString = record.get("PrecipitationFlag").trim(); if (noteString.equals("S")) { isSuspect = true; } toReturn.add(new PrecipitationRecord(wban, hour, inchesOfRain, isTrace, isSuspect)); } return toReturn; }
From source file:strawn.evariant.rainsorter.data.qclcdstations.QCWeatherStationLoader.java
/** * Loads and returns a list of all weather stations in the QCLCD data set. * /*from w w w . j a v a2s .c o m*/ * Throws out stations that don't have WBANs (Master file has 7) * * @return List of QCWeatherStationRecord representing the stations * @throws FileNotFoundException * @throws IOException */ public static List<QCWeatherStationRecord> loadRecordsFromDisk() throws FileNotFoundException, IOException { ArrayList<QCWeatherStationRecord> toReturn = new ArrayList(); Iterable<CSVRecord> records = getCSVRecords(); for (CSVRecord record : records) { double latitude = Double.parseDouble(record.get("Latitude")); double longitude = Double.parseDouble(record.get("Longitude")); String wbanString = record.get("WBAN"); if (wbanString.length() > 0) { toReturn.add(new QCWeatherStationRecord(wbanString, latitude, longitude)); } } return toReturn; }
From source file:strawn.evariant.rainsorter.unused.MSACountyLoader.java
public static List<MSACountyRecord> loadRecordsFromDisk() throws FileNotFoundException, IOException { ArrayList<MSACountyRecord> toReturn = new ArrayList(); Iterable<CSVRecord> records = getCSVRecords(); for (CSVRecord record : records) { String countyName = record.get("County/County Equivalent"); String countMSA = record.get("CBSA Title"); String countyState = record.get("State Name"); int cbsaCode = Integer.parseInt(record.get("CBSA Code")); toReturn.add(new MSACountyRecord(countyName, countMSA, countyState, cbsaCode)); }//from w w w. j a v a 2 s .com return toReturn; }
From source file:strawn.evariant.rainsorter.unused.WeatherStationLoader.java
public static List<WeatherStationRecord> loadRecordsFromDisk() throws FileNotFoundException, IOException { ArrayList<WeatherStationRecord> toReturn = new ArrayList(); Iterable<CSVRecord> records = getCSVRecords(); for (CSVRecord record : records) { int wban = Integer.parseInt(record.get("\"WBAN_ID\"").replace("\"", "")); String county = record.get("\"COUNTY\"").replace("\"", ""); String state = record.get("\"STATE_PROVINCE\"").replace("\"", ""); toReturn.add(new WeatherStationRecord(wban, county, state, 0d, 0d)); }/* w w w . j a v a 2 s .co m*/ return toReturn; }