List of usage examples for org.apache.poi.ss.usermodel Row getPhysicalNumberOfCells
int getPhysicalNumberOfCells();
From source file:com.fingence.slayer.service.impl.AssetLocalServiceImpl.java
License:Open Source License
public void loadDividends(long userId, File excelFile, ServiceContext serviceContext) { System.out.println("inside Load Dividends Data...."); if (Validator.isNull(excelFile)) return;//from ww w . ja v a2 s . co m InputStream is = null; try { is = new FileInputStream(excelFile); } catch (FileNotFoundException e) { //e.printStackTrace(); } if (Validator.isNull(is)) return; // Create Workbook instance holding reference to .xlsx file XSSFWorkbook workbook = null; try { workbook = new XSSFWorkbook(is); } catch (IOException e) { e.printStackTrace(); } if (Validator.isNull(workbook)) return; // Get first/desired sheet from the workbook XSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); Map<Integer, Long> columnNames = new HashMap<Integer, Long>(); int columnCount = 0; while (rowIterator.hasNext()) { Row row = rowIterator.next(); columnCount = row.getPhysicalNumberOfCells(); _log.debug("processing row ==> " + row.getRowNum()); System.out.println("processing row ==> " + row.getRowNum()); if (row.getRowNum() == 0) continue; if (row.getRowNum() == 1) { for (int i = 0; i < columnCount; i++) { Cell cell = row.getCell(i); if (Validator.isNull(cell)) continue; String id_isin = CellUtil.getString(cell); Asset asset = null; try { asset = assetPersistence.fetchByIdISIN(id_isin); } catch (SystemException e) { e.printStackTrace(); } if (Validator.isNull(asset)) continue; columnNames.put(i, asset.getAssetId()); } continue; } for (int i = 0; i < columnCount; i++) { Date declaredDate = CellUtil.getDate(row.getCell(i)); if (Validator.isNull(declaredDate)) continue; long assetId = 0l; try { assetId = columnNames.get(i); } catch (Exception e) { _log.debug(e.getMessage() + ": There is an exception..."); continue; } Date exDate = CellUtil.getDate(row.getCell(++i)); Date recordDate = CellUtil.getDate(row.getCell(++i)); Date payableDate = CellUtil.getDate(row.getCell(++i)); double amount = CellUtil.getDouble(row.getCell(++i)); String frequency = CellUtil.getString(row.getCell(++i)); String type = CellUtil.getString(row.getCell(++i)); Dividend dividend = null; try { dividend = dividendPersistence.fetchByAssetId_DeclaredDate(assetId, declaredDate); _log.debug("dividend record already present..."); } catch (SystemException e) { e.printStackTrace(); } if (Validator.isNull(dividend)) { long recId = 0l; try { recId = counterLocalService.increment(Dividend.class.getName()); } catch (SystemException e) { e.printStackTrace(); } dividend = dividendPersistence.create(recId); } // update the record dividend.setDeclaredDate(declaredDate); dividend.setExDate(exDate); dividend.setAssetId(assetId); dividend.setRecordDate(recordDate); dividend.setPayableDate(payableDate); dividend.setAmount(amount); dividend.setFrequency(frequency); dividend.setType(type); try { dividend = dividendLocalService.updateDividend(dividend); System.out.println("dividend new history records..." + dividend); } catch (SystemException e) { e.printStackTrace(); } } } }
From source file:com.fingence.slayer.service.impl.AssetLocalServiceImpl.java
License:Open Source License
public void importFromExcel(long userId, File excelFile, ServiceContext serviceContext) { if (Validator.isNull(excelFile)) return;//w w w.j a v a2 s . c o m InputStream is = null; try { is = new FileInputStream(excelFile); } catch (FileNotFoundException e) { //e.printStackTrace(); } if (Validator.isNull(is)) return; // Create Workbook instance holding reference to .xlsx file XSSFWorkbook workbook = null; try { workbook = new XSSFWorkbook(is); } catch (IOException e) { e.printStackTrace(); } if (Validator.isNull(workbook)) return; // Get first/desired sheet from the workbook XSSFSheet sheet = workbook.getSheetAt(0); // Iterate through each rows one by one Iterator<Row> rowIterator = sheet.iterator(); Map<String, Integer> columnNames = new HashMap<String, Integer>(); int columnCount = 0; long bbSecurityVocabularyId = AssetHelper.getVocabularyId(userId, "BB_Security", serviceContext); long bbIndustryVocabularyId = AssetHelper.getVocabularyId(userId, "BB_Industry", serviceContext); long bbAssetClassVocabularyId = AssetHelper.getVocabularyId(userId, "BB_Asset_Class", serviceContext); while (rowIterator.hasNext()) { Row row = rowIterator.next(); columnCount = row.getPhysicalNumberOfCells(); if (row.getRowNum() == 0) { for (int i = 0; i < columnCount; i++) { Cell cell = row.getCell(i); if (Validator.isNotNull(cell)) { columnNames.put(CellUtil.getStringCaps(cell), i); } } continue; } String id_isin = CellUtil.getString(row.getCell(columnNames.get("ID_ISIN"))); if (Validator.isNull(id_isin)) { System.out.println("id_isin is null or empty.. continuing...the name is..." + CellUtil.getString(row.getCell(columnNames.get("NAME")))); continue; } Asset asset = getAsset(userId, id_isin); asset.setSecurity_ticker(CellUtil.getString(row.getCell(columnNames.get("SECURITY_TICKER")))); asset.setId_cusip(CellUtil.getString(row.getCell(columnNames.get("ID_CUSIP")))); asset.setId_bb_global(CellUtil.getString(row.getCell(columnNames.get("ID_BB_GLOBAL")))); asset.setId_bb_sec_num_src(CellUtil.getLong(row.getCell(columnNames.get("ID_BB_SEC_NUM_SRC")))); asset.setName(CellUtil.getString(row.getCell(columnNames.get("NAME")))); asset.setChg_pct_mtd(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_MTD")))); asset.setChg_pct_5d(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_5D")))); asset.setChg_pct_1m(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_1M")))); asset.setChg_pct_3m(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_3M")))); asset.setChg_pct_6m(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_6M")))); asset.setChg_pct_ytd(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_YTD")))); asset.setBid_price(CellUtil.getDouble(row.getCell(columnNames.get("PX_BID")))); asset.setAsk_price(CellUtil.getDouble(row.getCell(columnNames.get("PX_ASK")))); asset.setLast_price(CellUtil.getDouble(row.getCell(columnNames.get("PX_LAST")))); asset.setChg_pct_high_52week(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_HIGH_52WEEK")))); asset.setChg_pct_low_52week(CellUtil.getDouble(row.getCell(columnNames.get("CHG_PCT_LOW_52WEEK")))); asset.setSecurity_des(CellUtil.getString(row.getCell(columnNames.get("SECURITY_DES")))); asset.setParent_comp_name(CellUtil.getString(row.getCell(columnNames.get("PARENT_COMP_NAME")))); String securityClass = CellUtil .getString(row.getCell(columnNames.get("BPIPE_REFERENCE_SECURITY_CLASS"))); if (securityClass.equalsIgnoreCase("FixedIncome")) { securityClass = "Fixed Income"; } asset.setVolatility_30d(CellUtil.getDouble(row.getCell(columnNames.get("VOLATILITY_30D")))); asset.setVolatility_90d(CellUtil.getDouble(row.getCell(columnNames.get("VOLATILITY_90D")))); asset.setVolatility_180d(CellUtil.getDouble(row.getCell(columnNames.get("VOLATILITY_180D")))); asset.setVolatility_360d(CellUtil.getDouble(row.getCell(columnNames.get("VOLATILITY_360D")))); asset.setCurrency(CellUtil.getString(row.getCell(columnNames.get("CRNCY"))).toUpperCase()); Country country = null; try { String countryCode = CellUtil.getString(row.getCell(columnNames.get("CNTRY_OF_DOMICILE"))); if (countryCode.equalsIgnoreCase("SP")) { countryCode = "ES"; } else if (countryCode.equalsIgnoreCase("EN")) { countryCode = "GB"; } country = CountryServiceUtil.fetchCountryByA2(countryCode); } catch (SystemException e) { e.printStackTrace(); } if (Validator.isNotNull(country)) { asset.setCountry(country.getCountryId()); } country = null; try { //CNTRY_OF_RISK String countryCode = CellUtil.getString(row.getCell(columnNames.get("CNTRY_OF_RISK"))); if (countryCode.equalsIgnoreCase("SP")) { countryCode = "ES"; } else if (countryCode.equalsIgnoreCase("EN")) { countryCode = "GB"; } country = CountryServiceUtil.fetchCountryByA2(countryCode); } catch (SystemException e) { e.printStackTrace(); } if (Validator.isNotNull(country)) { asset.setCountry_of_risk(country.getCountryId()); } else { asset.setCountry_of_risk(asset.getCountry()); } if (securityClass.equalsIgnoreCase("Fixed Income")) { asset.setSecurity_class(IConstants.SECURITY_CLASS_FIXED_INCOME); asset.setCurrent_price(asset.getBid_price() / 100); } else if (securityClass.equalsIgnoreCase("Fund")) { asset.setSecurity_class(IConstants.SECURITY_CLASS_FUND); asset.setCurrent_price(CellUtil.getDouble(row.getCell(columnNames.get("FUND_NET_ASSET_VAL")))); } else { asset.setSecurity_class(IConstants.SECURITY_CLASS_EQUITY); asset.setCurrent_price(asset.getLast_price()); } try { updateAsset(asset); } catch (SystemException e) { e.printStackTrace(); } long assetId = asset.getAssetId(); // Saving to AssetEntry table long entryId = AssetHelper.updateAssetEntry(assetId); AssetHelper.assignCategories(asset, entryId, userId, row, columnNames, serviceContext, bbSecurityVocabularyId, bbIndustryVocabularyId, bbAssetClassVocabularyId); if (securityClass.equalsIgnoreCase("Fixed Income")) { Bond bond = getBond(assetId); bond.setIssuer_bulk(CellUtil.getString(row.getCell(columnNames.get("ISSUER_BULK")))); bond.setCpn(CellUtil.getDouble(row.getCell(columnNames.get("CPN")))); bond.setCpn_typ(CellUtil.getString(row.getCell(columnNames.get("CPN_TYP")))); bond.setMty_typ(CellUtil.getString(row.getCell(columnNames.get("MTY_TYP")))); bond.setMty_years_tdy(CellUtil.getDouble(row.getCell(columnNames.get("MTY_YEARS_TDY")))); bond.setYld_ytm_ask(CellUtil.getDouble(row.getCell(columnNames.get("YLD_YTM_ASK")))); bond.setYld_ytm_bid(CellUtil.getDouble(row.getCell(columnNames.get("YLD_YTM_BID")))); bond.setYld_cur_mid(CellUtil.getDouble(row.getCell(columnNames.get("YLD_CUR_MID")))); bond.setBb_composite(CellUtil.getString(row.getCell(columnNames.get("BB_COMPOSITE")))); bond.setRtg_sp(CellUtil.getString(row.getCell(columnNames.get("RTG_SP")))); bond.setRtg_moody(CellUtil.getString(row.getCell(columnNames.get("RTG_MOODY")))); bond.setRtg_fitch(CellUtil.getString(row.getCell(columnNames.get("RTG_FITCH")))); bond.setCpn_freq(CellUtil.getDouble(row.getCell(columnNames.get("CPN_FREQ")))); bond.setFive_y_bid_cds_spread( CellUtil.getDouble(row.getCell(columnNames.get("5Y_BID_CDS_SPREAD")))); bond.setDur_mid(CellUtil.getDouble(row.getCell(columnNames.get("DUR_MID")))); bond.setPrice_to_cash_flow(CellUtil.getDouble(row.getCell(columnNames.get("PX_TO_CASH_FLOW")))); bond.setMaturity_dt(CellUtil.getDate(row.getCell(columnNames.get("MATURITY")))); bond.setCollat_typ(CellUtil.getString(row.getCell(columnNames.get("PAYMENT_RANK")))); bond.setCalc_typ(CellUtil.getDouble(row.getCell(columnNames.get("CALC_TYP")))); bond.setIs_bond_no_calctyp( Validator.isNull(CellUtil.getString(row.getCell(columnNames.get("IS_BOND_NO_CALCTYP"))))); bond.setIssue_dt(CellUtil.getDate(row.getCell(columnNames.get("ISSUE_DT")))); bond.setAmount_issued(CellUtil.getDouble(row.getCell(columnNames.get("AMT_ISSUED")))); bond.setAmount_outstanding(CellUtil.getDouble(row.getCell(columnNames.get("AMT_OUTSTANDING")))); try { bondLocalService.updateBond(bond); } catch (SystemException e) { e.printStackTrace(); } } else if (securityClass.equalsIgnoreCase("Fund")) { MutualFund mutualFund = getMutualFund(assetId); mutualFund.setFund_total_assets( CellUtil.getDouble(row.getCell(columnNames.get("FUND_TOTAL_ASSETS")))); mutualFund.setFund_asset_class_focus( CellUtil.getString(row.getCell(columnNames.get("FUND_ASSET_CLASS_FOCUS")))); mutualFund.setFund_geo_focus(CellUtil.getString(row.getCell(columnNames.get("FUND_GEO_FOCUS")))); try { mutualFundLocalService.updateMutualFund(mutualFund); } catch (SystemException e) { e.printStackTrace(); } } else if (securityClass.equalsIgnoreCase("Equity")) { Equity equity = getEquity(assetId); equity.setEqy_alpha(CellUtil.getDouble(row.getCell(columnNames.get("EQY_ALPHA")))); equity.setDividend_yield(CellUtil.getDouble(row.getCell(columnNames.get("DIVIDEND_YIELD")))); equity.setEqy_dvd_yld_12m(CellUtil.getDouble(row.getCell(columnNames.get("EQY_DVD_YLD_12M")))); equity.setEqy_dvd_yld_es(CellUtil.getDouble(row.getCell(columnNames.get("EQY_DVD_YLD_EST")))); equity.setDvd_payout_ratio(CellUtil.getDouble(row.getCell(columnNames.get("DVD_PAYOUT_RATIO")))); equity.setPe_ratio(CellUtil.getDouble(row.getCell(columnNames.get("PE_RATIO")))); equity.setTot_debt_to_com_eqy( CellUtil.getDouble(row.getCell(columnNames.get("TOT_DEBT_TO_COM_EQY")))); equity.setEbitda_to_revenue(CellUtil.getDouble(row.getCell(columnNames.get("EBITDA_TO_REVENUE")))); equity.setTrail_12m_prof_margin( CellUtil.getDouble(row.getCell(columnNames.get("TRAIL_12M_PROF_MARGIN")))); equity.setBest_current_ev_best_opp( CellUtil.getDouble(row.getCell(columnNames.get("BEST_CURRENT_EV_BEST_OPP")))); equity.setEqy_beta(CellUtil.getDouble(row.getCell(columnNames.get("EQY_ALPHA")))); equity.setReturn_sharpe_ratio( CellUtil.getDouble(row.getCell(columnNames.get("RETURN_SHARPE_RATIO")))); equity.setEqy_sharpe_ratio_1yr( CellUtil.getDouble(row.getCell(columnNames.get("EQY_SHARPE_RATIO_1YR")))); equity.setEqy_sharpe_ratio_3yr( CellUtil.getDouble(row.getCell(columnNames.get("EQY_SHARPE_RATIO_3YR")))); equity.setEqy_sharpe_ratio_5yr( CellUtil.getDouble(row.getCell(columnNames.get("EQY_SHARPE_RATIO_5YR")))); try { equityLocalService.updateEquity(equity); } catch (SystemException e) { e.printStackTrace(); } } } }
From source file:com.framework.common.ExcelSpreadsheet.java
public int getNumberOfExcelColumns() throws IOException, InvalidFormatException { int numberOfCells = 0; Iterator rowIterator = requiredWorksheet.rowIterator(); /**/*from ww w .ja v a2 s . c om*/ * Escape the header row * */ if (rowIterator.hasNext()) { Row headerRow = (Row) rowIterator.next(); //get the number of cells in the header row numberOfCells = headerRow.getPhysicalNumberOfCells(); } // System.out.println("number of cells " + numberOfCells); return numberOfCells; }
From source file:com.github.ukase.toolkit.xlsx.CellMerge.java
License:Open Source License
boolean isApplicable(Row row) { return isApplicableRow(row.getRowNum()) && isApplicableCell(row.getPhysicalNumberOfCells()); }
From source file:com.github.ukase.toolkit.xlsx.CellMerge.java
License:Open Source License
void fillRow(Row row) { int rowNumber; while ((rowNumber = row.getPhysicalNumberOfCells()) <= cellEnd) { row.createCell(rowNumber).setCellStyle(style); }/*from www . j a va2 s. c o m*/ }
From source file:com.github.ukase.toolkit.xlsx.RenderingTable.java
License:Open Source License
private void processCell(Row row, Element td) { TableCellBox cellBox = (TableCellBox) (box.getElementBoxes(td)).get(0); CellStyle style = prepareCellStyle(cellBox.getStyle()); mergedCells.stream().filter(merge -> merge.isApplicable(row)).forEach(merge -> merge.fillRow(row)); int cellNumber = row.getPhysicalNumberOfCells(); Cell cell = row.createCell(cellNumber); cell.setCellValue(td.getTextContent()); cell.setCellStyle(style);/*w w w . j av a 2 s . c om*/ mergeCells(row, td, cellNumber, style); calculateColumnWidth(cellNumber, cellBox.getStyle()); }
From source file:com.github.xiilei.ecdiff.Processor.java
License:Apache License
public void diff() { try {/*from w w w .j a v a2s . co m*/ logger.info("start ,src:" + job.getSrc() + ",dist:" + job.getDist()); Store store = this.getStoreFromSrc(); Workbook wb = readExcelFileByext(job.getDist()); this.font = wb.createFont(); this.font.setColor((short) 0xa); int rows_len = 0, i = 0, max_cells_len = 0; Sheet sheet = wb.getSheetAt(job.getDistSheet()); rows_len = sheet.getPhysicalNumberOfRows(); logger.info("Dist,open " + sheet.getSheetName() + " with " + rows_len + " rows"); for (i = 0; i < rows_len; i++) { Row row = sheet.getRow(i); max_cells_len = row.getPhysicalNumberOfCells(); if (!job.checkDistIndex(max_cells_len)) { logger.warn("Dist,The length of columns is too small at row " + i + ",length:" + max_cells_len); continue; } if (job.isByrow()) { cellComparer(store.get(i), row.getCell(job.getDistColumnIndex())); } else { cellComparer(store.get(getStringCellValue(row.getCell(job.getDistColumnIdIndex()))), row.getCell(job.getDistColumnIndex())); } } try (FileOutputStream out = new FileOutputStream(job.getOutFileName())) { wb.write(out); } logger.info("output file:" + job.getOutFileName()); } catch (Exception e) { logger.fatal(e.getMessage(), e); // e.printStackTrace(); } }
From source file:com.github.xiilei.ecdiff.Processor.java
License:Apache License
public Store getStoreFromSrc() throws IOException { Workbook wb = readExcelFileByext(job.getSrc()); Row row = null; int max_cells_len = 0; int rows_len = 0; Sheet sheet = wb.getSheetAt(job.getSrcSheet()); rows_len = sheet.getPhysicalNumberOfRows(); Store store = new Store(rows_len); logger.info("Src,open " + sheet.getSheetName() + " with " + rows_len + " rows"); for (int i = 0; i < rows_len; i++) { row = sheet.getRow(i);//from w ww.j a v a2 s . co m max_cells_len = row.getPhysicalNumberOfCells(); if (!job.checkSrcIndex(max_cells_len)) { logger.warn("Src,The length of columns is too small at row " + i + ",length:" + max_cells_len); continue; } if (job.isByrow()) { store.put(i, row.getCell(job.getSrcColumnIndex())); } else { store.put(getStringCellValue(row.getCell(job.getSrcColumnIdIndex())), row.getCell(job.getSrcColumnIndex())); } } return store; }
From source file:com.ncc.excel.ExcelUtil.java
License:Apache License
public List<Row> readExcel(Workbook wb) { Sheet sheet = null;// w w w. j av a 2 s .c om if (onlyReadOneSheet) {//??sheet System.out.println("selectedSheetName:" + selectedSheetName); // ??sheet(?????) sheet = selectedSheetName.equals("") ? wb.getSheetAt(selectedSheetIdx) : wb.getSheet(selectedSheetName); System.out.println(sheet.getSheetName()); } else { for (int i = 0; i < wb.getNumberOfSheets(); i++) {//??Sheet sheet = wb.getSheetAt(i); logger.info(sheet.getSheetName()); for (int j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {//?? Row row = sheet.getRow(j); for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) {//??? System.out.print(row.getCell(k) + "\t"); } System.out.println("---Sheet" + i + "?---"); } } } return null; }
From source file:com.opengamma.financial.security.equity.GICSCodeDescription.java
License:Open Source License
/** * Load S&P GICS code mappings from an Apace POI HSSFWorkbook * @param workbook HSSFWorkbook to parse S&P GCIS Excel * @param gicsMap Map to add mappings to *//*from www. j a va2 s. c o m*/ static void processGICSExcelWorkbook(Workbook workbook, Map<String, String> gicsMap) { //Assume 1 sheet Sheet sheet = workbook.getSheetAt(0); if (sheet == null) { return; } for (int rowNum = sheet.getFirstRowNum(); rowNum <= sheet.getLastRowNum(); rowNum++) { Row row = sheet.getRow(rowNum); if (row == null) { continue; } for (int cellNum = 0; cellNum < row.getPhysicalNumberOfCells(); cellNum++) { Cell cell = row.getCell(cellNum, Row.CREATE_NULL_AS_BLANK); if (isNumeric(cell)) { //worst case if the Excel file is in an incorrect (or updated) format // is that number -> random or empty string mappings will be created gicsMap.put(getGICSCellValue(cell), getGICSCellValue(row, cellNum + 1)); } } } }