List of usage examples for org.apache.poi.xssf.usermodel XSSFRow getCell
@Override public XSSFCell getCell(int cellnum)
From source file:com.accounting.reportMBean.ReportCommonBean.java
public void simplePostProcessXLSXOfStock(Object document) { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate4"); String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate1"); XSSFWorkbook wb = (XSSFWorkbook) document; XSSFCellStyle headerCellStyle = wb.createCellStyle(); XSSFCellStyle headerCellStyle1 = wb.createCellStyle(); XSSFCellStyle headerCellStyle2 = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle.setFont(headerFont); headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER); Font headerFont1 = wb.createFont(); headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING); headerFont1.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle1.setFont(headerFont); headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT); Font headerFont3 = wb.createFont(); headerFont3.setBoldweight(Font.U_SINGLE); headerFont3.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle2.setFont(headerFont1); headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT); XSSFSheet sheet = wb.getSheetAt(0);/*from w w w . j a v a2 s .c om*/ int noOfColumns = sheet.getRow(2).getLastCellNum(); for (int i = 0; i < noOfColumns; i++) { sheet.autoSizeColumn(i); } sheet.shiftRows(0, sheet.getLastRowNum(), 4); XSSFRow firstRow = sheet.createRow(1); firstRow.createCell(0).setCellValue("STOCK VALUE WITH VALUES"); firstRow.getCell(0).setCellStyle(headerCellStyle); XSSFRow secondRow = sheet.createRow(0); secondRow.createCell(0).setCellValue(getLoggedInOffice().getName()); secondRow.getCell(0).setCellStyle(headerCellStyle); // XSSFRow thirdRow = sheet.createRow(3); String date = ndc.convertToNepaliDate(new Date()); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); String time = sdf.format(new Date()); thirdRow.createCell(0) .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName()); thirdRow.getCell(0).setCellStyle(headerCellStyle2); XSSFRow fourthRow = sheet.createRow(2); fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString); fourthRow.getCell(0).setCellStyle(headerCellStyle); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7)); }
From source file:com.accounting.reportMBean.ReportCommonBean.java
public void simplePostProcessXLSX(Object document) { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate4"); String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate1"); XSSFWorkbook wb = (XSSFWorkbook) document; XSSFCellStyle headerCellStyle = wb.createCellStyle(); XSSFCellStyle headerCellStyle1 = wb.createCellStyle(); XSSFCellStyle headerCellStyle2 = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle.setFont(headerFont); headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER); Font headerFont1 = wb.createFont(); headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING); headerFont1.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle1.setFont(headerFont); headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT); Font headerFont3 = wb.createFont(); headerFont3.setBoldweight(Font.U_SINGLE); headerFont3.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle2.setFont(headerFont1); headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT); XSSFSheet sheet = wb.getSheetAt(0);/*from ww w.j ava 2 s. co m*/ int noOfColumns = sheet.getRow(2).getLastCellNum(); for (int i = 0; i < noOfColumns; i++) { sheet.autoSizeColumn(i); } sheet.shiftRows(0, sheet.getLastRowNum(), 4); XSSFRow firstRow = sheet.createRow(1); firstRow.createCell(0).setCellValue(pageName); firstRow.getCell(0).setCellStyle(headerCellStyle); XSSFRow secondRow = sheet.createRow(0); secondRow.createCell(0).setCellValue(getLoggedInOffice().getName()); secondRow.getCell(0).setCellStyle(headerCellStyle); // XSSFRow thirdRow = sheet.createRow(3); String date = ndc.convertToNepaliDate(new Date()); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); String time = sdf.format(new Date()); thirdRow.createCell(0) .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName()); thirdRow.getCell(0).setCellStyle(headerCellStyle2); XSSFRow fourthRow = sheet.createRow(2); fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString); fourthRow.getCell(0).setCellStyle(headerCellStyle); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7)); }
From source file:com.appdynamics.jrbronet.projectplan.ExcelManager.java
private static void copyRow(XSSFWorkbook workbook, XSSFSheet sourceWorksheet, int sourceRowNum, XSSFSheet destinationWorksheet, int destinationRowNum) { // Get the source / new row XSSFRow newRow = destinationWorksheet.getRow(destinationRowNum); XSSFRow sourceRow = sourceWorksheet.getRow(sourceRowNum); // If the row exist in destination, push down all rows by 1 else create a new row if (newRow != null) { destinationWorksheet.shiftRows(destinationRowNum, destinationWorksheet.getLastRowNum(), 1); } else {// w ww. j a va2 s .co m newRow = destinationWorksheet.createRow(destinationRowNum); } // Loop through source columns to add to new row for (int i = 0; i < sourceRow.getLastCellNum(); i++) { // Grab a copy of the old/new cell XSSFCell oldCell = sourceRow.getCell(i); XSSFCell newCell = newRow.createCell(i); // If the old cell is null jump to next cell if (oldCell == null) { newCell = null; continue; } // Copy style from old cell and apply to new cell XSSFCellStyle newCellStyle = workbook.createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); ; newCell.setCellStyle(newCellStyle); // If there is a cell comment, copy if (oldCell.getCellComment() != null) { newCell.setCellComment(oldCell.getCellComment()); } // If there is a cell hyperlink, copy if (oldCell.getHyperlink() != null) { newCell.setHyperlink(oldCell.getHyperlink()); } // Set the cell data type newCell.setCellType(oldCell.getCellType()); //newCell.setCellValue(oldCell.getRawValue()); //newCell.setCellType(oldCell.getCellType()); // Set the cell data value switch (oldCell.getCellType()) { case Cell.CELL_TYPE_BLANK: newCell.setCellValue(oldCell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getRichStringCellValue()); break; } } // If there are are any merged regions in the source row, copy to new row /* for (int i = 0; i < worksheet.getNumMergedRegions(); i++) { CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) { CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(), (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow() )), cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn()); worksheet.addMergedRegion(newCellRangeAddress); } } */ }
From source file:com.atanas.kanchev.testframework.dataservices.dataprovider.excel.ExcelParser.java
License:Apache License
/** * Gets table data./*from w ww.j a v a 2 s . c om*/ * * @param sheetName the sheet name * @return the table data */ public synchronized List<Map<String, Object>> getTableData(String sheetName) { XSSFSheet sheet = workbook.getSheet(sheetName); int totalColumns = sheet.getRow(0).getLastCellNum(); logger.debug("Number of columns: " + totalColumns); logger.debug( "Number of data rows: ".concat(String.valueOf(sheet.getLastRowNum() - sheet.getFirstRowNum()))); List<String> header = getHeaders(sheetName); List<Map<String, Object>> data = new LinkedList<>(); LinkedHashMap<String, Object> xlData; for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getLastRowNum(); i++) { xlData = new LinkedHashMap<>(); XSSFRow dRow = sheet.getRow(i); if (null == dRow) { logger.warn("Empty row, exiting excel reader"); break; } for (int j = dRow.getFirstCellNum(); j < totalColumns; j++) { switch (dRow.getCell(j).getCellType()) { case XSSFCell.CELL_TYPE_STRING: xlData.put(header.get(j), dRow.getCell(j).getStringCellValue()); logger.debug("Data in cell " + dRow.getCell(j).getAddress() + " : " + dRow.getCell(j).getStringCellValue()); break; case XSSFCell.CELL_TYPE_NUMERIC: xlData.put(header.get(j), dRow.getCell(j).getNumericCellValue()); logger.debug("Data in cell " + dRow.getCell(j).getAddress() + " : " + dRow.getCell(j).getNumericCellValue()); break; } } data.add(xlData); } return data; }
From source file:com.axelor.apps.admin.service.ViewDocExportService.java
License:Open Source License
private void writeCell(XSSFRow row, Integer oldRowIndex, int count, XSSFCellStyle cellStyle) { XSSFRow oldRow = oldSheet.getRow(oldRowIndex); while (count < oldRow.getLastCellNum()) { XSSFCell oldCell = oldRow.getCell(count); Cell cell = row.createCell(count); cell.setCellStyle(cellStyle);/*from w w w . java2s . c o m*/ cell.setCellValue(oldCell.getStringCellValue()); count++; } }
From source file:com.centurylink.mdw.drools.Excel2007Parser.java
License:Apache License
@SuppressWarnings("deprecation") private void processSheet(XSSFSheet sheet, List<? extends DataListener> listeners) { int mergedRegionCount = sheet.getNumMergedRegions(); CellRangeAddress[] mergedRanges = new CellRangeAddress[mergedRegionCount]; for (int i = 0; i < mergedRegionCount; i++) { mergedRanges[i] = sheet.getMergedRegion(i); }//w w w . ja v a 2s. co m for (int i = 0; i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i); if (row != null) { newRow(listeners, i, row.getLastCellNum()); for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) { XSSFCell cell = row.getCell(cellNum); if (cell != null) { CellRangeAddress merged = getRangeIfMerged(cell, mergedRanges); if (merged != null) { XSSFRow topRow = sheet.getRow(merged.getFirstRow()); XSSFCell topLeft = topRow.getCell(merged.getFirstColumn()); String cellValue = topLeft.getCellType() == CellType.NUMERIC.getCode() ? String.valueOf(topLeft.getNumericCellValue()) : topLeft.getStringCellValue(); newCell(listeners, i, cellNum, cellValue, topLeft.getColumnIndex()); } else { String cellValue = null; if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cellValue = String.valueOf(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = String.valueOf(cell.getNumericCellValue()); else cellValue = cell.getStringCellValue(); newCell(listeners, i, cellNum, cellValue, DataListener.NON_MERGED); } } } } } finishSheet(listeners); }
From source file:com.centurylink.mdw.workflow.drools.Excel2007Parser.java
License:Apache License
private void processSheet(XSSFSheet sheet, List<? extends DataListener> listeners) { int mergedRegionCount = sheet.getNumMergedRegions(); CellRangeAddress[] mergedRanges = new CellRangeAddress[mergedRegionCount]; for (int i = 0; i < mergedRegionCount; i++) { mergedRanges[i] = sheet.getMergedRegion(i); }//from w w w . j a v a 2 s .c o m for (int i = 0; i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i); if (row != null) { newRow(listeners, i, row.getLastCellNum()); for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) { XSSFCell cell = row.getCell(cellNum); if (cell != null) { CellRangeAddress merged = getRangeIfMerged(cell, mergedRanges); if (merged != null) { XSSFRow topRow = sheet.getRow(merged.getFirstRow()); XSSFCell topLeft = topRow.getCell(merged.getFirstColumn()); newCell(listeners, i, cellNum, topLeft.getStringCellValue(), topLeft.getColumnIndex()); } else { String cellValue = null; if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cellValue = String.valueOf(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = String.valueOf(cell.getNumericCellValue()); else cellValue = cell.getStringCellValue(); newCell(listeners, i, cellNum, cellValue, DataListener.NON_MERGED); } } } } } finishSheet(listeners); }
From source file:com.clri.servlet.ExcelUpload.java
public MajorProductions getProductions(XSSFRow row, MajorProductions majorProductions) { String articleCode = row.getCell(0).getRawValue(); String items = row.getCell(1).getStringCellValue(); double quantity = row.getCell(2).getNumericCellValue(); double value = row.getCell(3).getNumericCellValue(); String year = row.getCell(4).getStringCellValue(); majorProductions.setItems(items);//ww w .j av a 2s . c om majorProductions.setQuantity(quantity); majorProductions.setValue(value); majorProductions.setYear(year); majorProductions.setArticleCode(articleCode); return majorProductions; }
From source file:com.clri.servlet.ExcelUpload.java
public MajorCustomers getCustomers(XSSFRow row, MajorCustomers majorCustomers) { String items = row.getCell(0).getStringCellValue(); String articleCode = row.getCell(1).getRawValue(); String country = row.getCell(2).getStringCellValue(); double quantity = row.getCell(3).getNumericCellValue(); double value = row.getCell(4).getNumericCellValue(); String year = row.getCell(5).getStringCellValue(); majorCustomers.setItems(items);//from w w w . j av a 2 s . c om majorCustomers.setQuantity(quantity); majorCustomers.setValue(value); majorCustomers.setYear(year); majorCustomers.setCountry(country); majorCustomers.setArticleCode(articleCode); return majorCustomers; }
From source file:com.cn.util.ExcelImport.java
/** * ?2007excel//from w w w. j ava2 s . c om * * @param file * @return */ private static List<List<Object>> read2007Excel(InputStream inputStream) throws IOException { List<List<Object>> dataList = new ArrayList<>(); XSSFWorkbook xwb = new XSSFWorkbook(inputStream); XSSFSheet sheet = xwb.getSheetAt(0); XSSFRow row = null; XSSFCell cell = null; Object val = null; DecimalFormat df = new DecimalFormat("0");// ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ? for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) { row = sheet.getRow(i); if (row == null) { continue; } List<Object> objList = new ArrayList<>(); for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) { cell = row.getCell(j); if (cell == null) { val = null; objList.add(val); continue; } switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: val = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { val = df.format(cell.getNumericCellValue()); } else if ("General".equals(cell.getCellStyle().getDataFormatString())) { val = df.format(cell.getNumericCellValue()); } else { val = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); } break; case XSSFCell.CELL_TYPE_BOOLEAN: val = cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_BLANK: val = ""; break; default: val = cell.toString(); break; } objList.add(val); } dataList.add(objList); } return dataList; }