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:core.reporting.ImportFromFile.java
private void validateCSVFile() { Iterable<CSVRecord> inrlst = null; try {/*w ww . j ava 2s .c o m*/ Reader in = new FileReader(csvInputFile); inrlst = (new CSVParser(in, CSVFormat.EXCEL.withHeader()).getRecords()); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } int err = validateRecord(inrlst); // error found, show errors in system log ServiceRequest sr = null; putClientProperty(TConstants.JTABLE_AUTO_RESIZE_MODE, JTable.AUTO_RESIZE_OFF); if (err > 0) { String wc = "t_sluserid = '" + Session.getUserName() + "' AND t_slflag = 'ie'"; sr = new ServiceRequest(ServiceRequest.DB_QUERY, "t_system_log", wc); putClientProperty(TConstants.ICON_PARAMETERS, "0;;t_sltype"); putClientProperty(TConstants.SHOW_COLUMNS, "t_slmessage"); saveAction.setEnabled(false); } else { sr = new ServiceRequest(ServiceRequest.CLIENT_GENERATED_LIST, "", tempBuffer); sr.setParameter(ServiceResponse.RECORD_MODEL, ConnectionManager.getAccessTo(recordModel.getTableName()).getModel()); putClientProperty(TConstants.ICON_PARAMETERS, "-1;aa"); putClientProperty(TConstants.SHOW_COLUMNS, fileColumns); saveAction.setEnabled(true); } setServiceRequest(sr); /* * File f = (File) evt.getNewValue(); Record rcon = * ConnectionManager.getAccessTo("t_connections").exist("t_cnname = 'CsvJdbc'"); String u = (String) * rcon.getFieldValue("t_cnurl"); // rcon.setFieldValue("t_cnurl", u.replace("<filename>", * f.getAbsolutePath())); rcon.setFieldValue("t_cnurl", u.replace("<filename>", f.getParent())); * ConnectionManager.connect(rcon); String tn = "CsvJdbc." + f.getName().split("[.]")[0]; Record rm = * ConnectionManager.getAccessTo(tn).getModel(); ServiceRequest sr = new ServiceRequest(ServiceRequest.DB_QUERY, * tn, null); sr.setParameter(ServiceRequest.ORDER_BY, "ficha"); setServiceRequest(sr); */ }
From source file:com.ibm.util.merge.directive.provider.ProviderTag.java
/** * Reset the table, and if the Tag exists, add a row with the tag name/value * @param cf/*from w w w.j a v a2 s . c o m*/ */ @Override public void getData(MergeContext rtc) throws MergeException { reset(); DataTable table = addNewTable(); Template template = getDirective().getTemplate(); String theTag = Template.wrap(tag); log.info("Getting Tag Data for " + tag); switch (condition) { case ProviderTag.CONDITION_EXISTS: if (!template.hasReplaceKey(theTag)) { log.info("Tag not found for Exists Condition"); return; } break; case ProviderTag.CONDITION_BLANK: if (!template.hasReplaceKey(theTag) || template.hasReplaceValue(theTag)) { log.info("Tag not found or Data found for Blank Condition"); return; } break; case ProviderTag.CONDITION_NONBLANK: if (!template.hasReplaceKey(theTag) || !template.hasReplaceValue(theTag)) { log.info("Tag or Empty Data found for Non-Blank Condition"); return; } break; case ProviderTag.CONDITION_EQUALS: if (!template.hasReplaceKey(theTag) || !template.hasReplaceValue(theTag) || !template.getReplaceValue(theTag).equals(value)) { log.info("Tag not Equals or not found"); return; } break; } // We have a match, so add data String data = template.getReplaceValue(Template.wrap(tag)); log.info("Data Found: " + data); table.addCol(tag); if (isList()) { CSVParser parser; try { parser = new CSVParser(new StringReader(data), CSVFormat.EXCEL.withHeader()); for (String colName : parser.getHeaderMap().keySet()) { ArrayList<String> row = table.addNewRow(); row.add(colName); } parser.close(); } catch (IOException e) { throw new MergeException(this, e, "CSV Parser Stringreader IO Exception", data); } } else { ArrayList<String> row = table.addNewRow(); row.add(data); } }
From source file:com.sapuraglobal.hrms.servlet.UploadEmp.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w.jav a 2 s . c om * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); String action = request.getParameter("action"); System.out.println("action: " + action); if (action == null || action.isEmpty()) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); //PriceDAO priceDAO = new PriceDAO(); try { List<FileItem> fields = upload.parseRequest(request); //out.println("Number of fields: " + fields.size() + "<br/><br/>"); 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>"); 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 = titleBean.getTitleByName(title); RoleDTO roleDto = accessBean.getRole(role); DeptDTO deptDto = deptBean.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. userBean.createUser(user); //assign role userBean.assignRole(user, roleDto); //assign dept deptBean.assignEmployee(user, deptDto); //leave ent LeaveTypeDTO lvtypeDTO = leaveBean.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); leaveBean.addLeaveEnt(annualentDTO); //medical ent LeaveTypeDTO medTypeDTO = leaveBean.getLeaveType("Medical Leave"); LeaveEntDTO medentDTO = new LeaveEntDTO(); medentDTO.setBalance(medTypeDTO.getDays() - Double.parseDouble(med)); medentDTO.setCurrent(medTypeDTO.getDays()); medentDTO.setUser(user); medentDTO.setLeaveType(medTypeDTO); leaveBean.addLeaveEnt(medentDTO); //oil ent LeaveTypeDTO oilTypeDTO = leaveBean.getLeaveType("Off-in-Lieu"); LeaveEntDTO oilentDTO = new LeaveEntDTO(); oilentDTO.setBalance(oilTypeDTO.getDays() - Double.parseDouble(oil)); oilentDTO.setCurrent(0); oilentDTO.setUser(user); oilentDTO.setLeaveType(oilTypeDTO); leaveBean.addLeaveEnt(oilentDTO); //unpaid LeaveTypeDTO unpaidTypeDTO = leaveBean.getLeaveType("Unpaid"); LeaveEntDTO unpaidentDTO = new LeaveEntDTO(); unpaidentDTO.setBalance(unpaidTypeDTO.getDays() - Double.parseDouble(unpaid)); unpaidentDTO.setCurrent(0); unpaidentDTO.setUser(user); unpaidentDTO.setLeaveType(unpaidTypeDTO); leaveBean.addLeaveEnt(unpaidentDTO); //child LeaveTypeDTO childTypeDTO = leaveBean.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); leaveBean.addLeaveEnt(childentDTO); } /* if (stockPrices.size() > 0) { priceDAO.OpenConnection(); priceDAO.updateStockPrice(stockPrices); } */ } } } catch (Exception e) { e.printStackTrace(); } finally { //priceDAO.CloseConnection(); RequestDispatcher dispatcher = request.getRequestDispatcher("/employee"); //request.setAttribute(Constants.TITLE, "Home"); dispatcher.forward(request, response); } } else { RequestDispatcher dispatcher = request.getRequestDispatcher("/uploadEmp.jsp"); //request.setAttribute(Constants.TITLE, "Home"); dispatcher.forward(request, response); } }
From source file:edu.unc.lib.dl.admin.controller.ExportController.java
@RequestMapping(value = "{pid}", method = RequestMethod.GET) public void export(@PathVariable("pid") String pid, HttpServletRequest request, HttpServletResponse response) throws IOException { String filename = pid.replace(":", "_") + ".csv"; response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.addHeader("Content-Type", "text/csv"); SearchRequest searchRequest = generateSearchRequest(request, searchStateFactory.createSearchState()); SearchState searchState = searchRequest.getSearchState(); searchState.setResultFields(Arrays.asList(SearchFieldKeys.ID.name(), SearchFieldKeys.TITLE.name(), SearchFieldKeys.RESOURCE_TYPE.name(), SearchFieldKeys.ANCESTOR_IDS.name(), SearchFieldKeys.STATUS.name(), SearchFieldKeys.DATASTREAM.name(), SearchFieldKeys.ANCESTOR_PATH.name(), SearchFieldKeys.CONTENT_MODEL.name(), SearchFieldKeys.DATE_ADDED.name(), SearchFieldKeys.DATE_UPDATED.name(), SearchFieldKeys.LABEL.name())); searchState.setSortType("export"); searchState.setRowsPerPage(searchSettings.maxPerPage); BriefObjectMetadata container = queryLayer.addSelectedContainer(pid, searchState, false); SearchResultResponse resultResponse = queryLayer.getSearchResults(searchRequest); List<BriefObjectMetadata> objects = resultResponse.getResultList(); objects.add(0, container);//from w w w. jav a2s. c o m queryLayer.getChildrenCounts(objects, searchRequest); try (ServletOutputStream out = response.getOutputStream()) { Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.ISO_8859_1)); try (CSVPrinter printer = CSVFormat.EXCEL.print(writer)) { printHeaders(printer); for (BriefObjectMetadata object : objects) { printObject(printer, object); } } } }
From source file:MasterRoomControllerFx.rooms.charts.RoomChartController.java
public void getTempData() { try {/*from w w w .j a v a2s . com*/ File csvData = new File("tempHistory" + roomRow + roomColumn + ".csv"); if (csvData.exists()) { CSVParser parser = CSVParser.parse(csvData, StandardCharsets.UTF_8, CSVFormat.EXCEL.withDelimiter(';')); for (CSVRecord csvRecord : parser) { for (int i = 0; i < csvRecord.size(); i++) { if (i == 0) temp.add(Float.parseFloat(csvRecord.get(i))); if (i == 1) time.add(csvRecord.get(i)); } } } } catch (IOException ex) { Logger.getLogger(RoomChartController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:biz.itcons.wsdm.hw2_2.NaiveBayes.java
/** * Checks feasibility of classification based on test data. For given set * it runs NaiveBayes classifier and checks the result with one that is * provided for test data.//from w ww . java2 s. c o m * @param file A CSV file with documents that are classified. * @param classifierPos a position in CSV file where classification is * stored (zero based indexing) * @param documentPos a position in CSV file where document body is stored * (zero based indexing) * @return ration of correctly classified documents vs. all test documents. * @throws FileNotFoundException * @throws IOException */ public double testDataSet(String file, int classifierPos, int documentPos) throws FileNotFoundException, IOException { int testSetCount = 0; int correctClass = 0; try (Reader in = new FileReader(file)) { LOGGER.debug("Opening training data set: " + file); for (CSVRecord record : CSVFormat.EXCEL.parse(in)) { testSetCount++; String classification = classifyDocument(record.get(documentPos)); if (record.get(classifierPos).equals(classification)) { correctClass++; } } LOGGER.trace("Read " + testSetCount + " from test set"); LOGGER.info("success ratio: " + correctClass + " of " + testSetCount); return (double) correctClass / testSetCount; } }
From source file:com.house365.build.util.CsvUtil.java
/** * ?CSV.//from ww w . j ava 2s .c om * * @param csvFile * @param headerRow ,1 * @return Csv */ private static String[] getCsvHeader(File csvFile, int headerRow) throws Exception { String[] headNameStrings = null; AutoDetectReader reader = null; try { reader = new AutoDetectReader(new FileInputStream(csvFile)); Iterable<CSVRecord> csvRecords = CSVFormat.EXCEL.parse(reader); int i = 1; for (CSVRecord record : csvRecords) { if (i == headerRow) { headNameStrings = new String[record.size()]; for (int j = 0; j < record.size(); j++) { headNameStrings[j] = record.get(j); } break; } i = i + 1; } } catch (Exception e) { throw e; } finally { try { if (reader != null) reader.close(); } catch (Exception e) { } } return headNameStrings; }
From source file:br.edimarmanica.weir2.integration.ScoredPairs.java
public static List<ScoredPair> loadAndSort(Domain domain) { List<ScoredPair> pairs = new ArrayList<>(); try (Reader in = new FileReader(new File(Paths.PATH_WEIR_V2 + "/" + domain.getPath() + "/scores.csv"))) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) { for (CSVRecord record : parser) { //para cada value Rule rule1 = new Rule(domain.getSiteOf(record.get("SITE1")), record.get("RULE1")); Rule rule2 = new Rule(domain.getSiteOf(record.get("SITE2")), record.get("RULE2")); double score = Double.valueOf(record.get("SCORE")); if (score == 1) { continue; }//w w w . j a v a 2s. c o m ScoredPair pair = new ScoredPair(rule1, rule2, score); pairs.add(pair); } } } 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); } Collections.sort(pairs); return pairs; }
From source file:br.edimarmanica.weir2.integration.ScoredPairs.java
private void persiste(Site site1, String rule1, Site site2, String rule2, double score) { List<String> dataRecord = new ArrayList<>(); dataRecord.add(site1.toString());/*from w ww. ja v a 2 s. co m*/ dataRecord.add(rule1); dataRecord.add(site2.toString()); dataRecord.add(rule2); dataRecord.add(score + ""); File dirOutput = new File(Paths.PATH_WEIR_V2 + "/" + domain.getPath()); dirOutput.mkdirs(); File file = new File(dirOutput.getAbsolutePath() + "/scores.csv"); CSVFormat format; if (append) { format = CSVFormat.EXCEL; } else { String[] HEADER = { "SITE1", "RULE1", "SITE2", "RULE2", "SCORE" }; 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(RulesFilter.class.getName()).log(Level.SEVERE, null, ex); } append = true; }
From source file:com.ge.research.semtk.load.dataset.CSVDataset.java
private CSVParser getParser(Reader reader) throws Exception { // return (CSVFormat.EXCEL.withHeader().withIgnoreHeaderCase(true).parse(reader)); return (CSVFormat.EXCEL.withHeader().withIgnoreHeaderCase(true).withQuote('"').withEscape('\\') .withIgnoreEmptyLines(true).parse(reader)); // changed toward handling quotes in stream. the were breaking // solution suggestion: http://stackoverflow.com/questions/26729799/invalid-char-between-encapsulated-token-and-delimiter-in-apache-commons-csv-libr }