Java Code Examples for org.apache.poi.hssf.usermodel.HSSFCell#getCellType()
The following examples show how to use
org.apache.poi.hssf.usermodel.HSSFCell#getCellType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 6 votes |
public static Integer findMatchColumn(HSSFRow row, String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() != HSSFCell.CELL_TYPE_STRING) { continue; } HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (cellValue.getString().matches(str)) { return Integer.valueOf(colNum); } } return null; }
Example 2
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 6 votes |
public static Integer findColumn(HSSFRow row, String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (str.equals(cellValue.getString())) { return Integer.valueOf(colNum); } } } return null; }
Example 3
Source File: XlsSessionReader.java From conference-app with MIT License | 6 votes |
private String getCellValue(HSSFCell cell) { String result = null; if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: result = cell.getCellFormula().toString(); break; case HSSFCell.CELL_TYPE_NUMERIC: result = "" + cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; default: } } return result; }
Example 4
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 6 votes |
public static Integer findColumn(final HSSFRow row, final String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() == Cell.CELL_TYPE_STRING) { final HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (str.equals(cellValue.getString())) { return Integer.valueOf(colNum); } } } return null; }
Example 5
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 6 votes |
public static Integer findMatchColumn(final HSSFRow row, final String str) { for (int colNum = row.getFirstCellNum(); colNum <= row.getLastCellNum(); colNum++) { final HSSFCell cell = row.getCell(colNum); if (cell == null) { continue; } if (cell.getCellType() != Cell.CELL_TYPE_STRING) { continue; } final HSSFRichTextString cellValue = cell.getRichStringCellValue(); if (cellValue.getString().matches(str)) { return Integer.valueOf(colNum); } } return null; }
Example 6
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 6 votes |
public static int getIntCellValue(final HSSFSheet sheet, final int r, final int c) { final HSSFRow row = sheet.getRow(r); if (row == null) { return 0; } final HSSFCell cell = row.getCell(c); try { if (cell.getCellType() != Cell.CELL_TYPE_NUMERIC) { return 0; } } catch (final RuntimeException e) { System.err.println("Exception at sheet name:" + sheet.getSheetName() + ", row:" + (r + 1) + ", col:" + (c + 1)); throw e; } return (int) cell.getNumericCellValue(); }
Example 7
Source File: WordEmbedsTest.java From sun-wordtable-read with Apache License 2.0 | 5 votes |
private static void listEmbeds2(XWPFDocument doc) throws Exception { for (final PackagePart pPart : doc.getAllEmbedds()) { final String contentType = pPart.getContentType(); System.out.println(contentType + "\n"); if (contentType.equals("application/vnd.ms-excel")) { final HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream()); for (int sheet = 0; sheet < embeddedWorkbook.getNumberOfSheets(); sheet++) { final HSSFSheet activeSheet = embeddedWorkbook.getSheetAt(sheet); if (activeSheet.getSheetName().equalsIgnoreCase("Sheet1")) { for (int rowIndex = activeSheet.getFirstRowNum(); rowIndex <= activeSheet .getLastRowNum(); rowIndex++) { final HSSFRow row = activeSheet.getRow(rowIndex); for (int cellIndex = row.getFirstCellNum(); cellIndex <= row .getLastCellNum(); cellIndex++) { final HSSFCell cell = row.getCell(cellIndex); if (cell != null) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) System.out.println("Row:" + rowIndex + " Cell:" + cellIndex + " = " + cell.getStringCellValue()); if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { System.out.println("Row:" + rowIndex + " Cell:" + cellIndex + " = " + cell.getNumericCellValue()); cell.setCellValue(cell.getNumericCellValue() * 2); // update // the // value } } } } } } } } }
Example 8
Source File: readExcel.java From Selenium with The Unlicense | 5 votes |
private static String cellToString(HSSFCell cell) { Object result; switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: result = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_FORMULA: result = cell.getCellFormula(); break; default: throw new RuntimeException("Unknown Cell Type"); } return result.toString(); }
Example 9
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 5 votes |
public static int getIntCellValue(HSSFSheet sheet, int r, int c) { HSSFRow row = sheet.getRow(r); if (row == null) { return 0; } HSSFCell cell = row.getCell(c); if (cell.getCellType() != HSSFCell.CELL_TYPE_NUMERIC) { return 0; } return (int) cell.getNumericCellValue(); }
Example 10
Source File: ExcelUtil.java From jeewx with Apache License 2.0 | 5 votes |
/** * 读取 Excel文件内容 * * @param excel_name * @return * @throws Exception */ public static List<List<Object>> readExcelByInputStream( InputStream inputstream) throws Exception { // 结果集 List<List<Object>> list = new ArrayList<List<Object>>(); HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream); // 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0); // 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 // //org.jeecgframework.core.util.LogUtil.info("excel行数: "+hssfsheet.getPhysicalNumberOfRows()); for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { HSSFRow hssfrow = hssfsheet.getRow(j); if (hssfrow != null) { int col = hssfrow.getPhysicalNumberOfCells(); // 单行数据 List<Object> arrayString = new ArrayList<Object>(); for (int i = 0; i < col; i++) { HSSFCell cell = hssfrow.getCell(i); if (cell == null) { arrayString.add(""); } else if (cell.getCellType() == 0) { arrayString.add(new Double(cell.getNumericCellValue()) .toString()); } else {// 如果EXCEL表格中的数据类型为字符串型 arrayString.add(cell.getStringCellValue().trim()); } } list.add(arrayString); } } return list; }
Example 11
Source File: ExcelUtil.java From jeewx with Apache License 2.0 | 5 votes |
/** * 读取 Excel文件内容 * * @param excel_name * @return * @throws Exception */ public static List<List<Object>> readExcelByList(String excel_name) throws Exception { // 结果集 List<List<Object>> list = new ArrayList<List<Object>>(); HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream( excel_name)); // 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0); // 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { HSSFRow hssfrow = hssfsheet.getRow(j); if (hssfrow != null) { int col = hssfrow.getPhysicalNumberOfCells(); // 单行数据 List<Object> arrayString = new ArrayList<Object>(); for (int i = 0; i < col; i++) { HSSFCell cell = hssfrow.getCell(i); if (cell == null) { arrayString.add(""); } else if (cell.getCellType() == 0) { arrayString.add(new Double(cell.getNumericCellValue()) .toString()); } else {// 如果EXCEL表格中的数据类型为字符串型 arrayString.add(cell.getStringCellValue().trim()); } } list.add(arrayString); } } return list; }
Example 12
Source File: ReadExcelUtil.java From DWSurvey with GNU Affero General Public License v3.0 | 5 votes |
public static String getCellStringValue(HSSFCell cell) { String cellValue = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); if (cellValue.trim().equals("") || cellValue.trim().length() <= 0) { cellValue = " "; } break; case HSSFCell.CELL_TYPE_NUMERIC: // cellValue = String.valueOf(cell.getNumericCellValue()); DecimalFormat formatter = new DecimalFormat("######"); cellValue = formatter.format(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cellValue = String.valueOf(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: cellValue = " "; break; case HSSFCell.CELL_TYPE_BOOLEAN: break; case HSSFCell.CELL_TYPE_ERROR: break; default: break; } return cellValue; }
Example 13
Source File: ReadExcelUtil.java From DWSurvey with GNU Affero General Public License v3.0 | 5 votes |
public static void reader(String filePath) { try { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); HSSFRow row = sheet.getRow(3); HSSFCell cell = row.getCell((short) 0); int type = cell.getCellType(); String msg = getCellStringValue(cell); System.out.println(type + ":" + msg); } catch (IOException e) { e.printStackTrace(); } }
Example 14
Source File: XLSFileNormalizer.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
private String parseCell(HSSFCell cell) { String valueField = null; if(cell == null) return ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: valueField = cell.getCellFormula().toString(); break; case HSSFCell.CELL_TYPE_NUMERIC: Double numericValue = cell.getNumericCellValue(); //testing if the double is an integer value if ((numericValue == Math.floor(numericValue)) && !Double.isInfinite(numericValue)) { //the number is an integer, this will remove the .0 trailing zeros int numericInt = numericValue.intValue(); valueField = String.valueOf(numericInt); } else { valueField = String.valueOf(cell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_STRING: valueField = cell.getStringCellValue(); break; default: } return valueField; }
Example 15
Source File: PoiUtil.java From dk-fitting with Apache License 2.0 | 5 votes |
/** * 获取单元格中的内容 ,该犯法用于解析各种形式的数据 */ private Object getCellString(HSSFCell cell) { Object result = null; if (cell != null) { int cellType = cell.getCellType(); switch (cellType) { case HSSFCell.CELL_TYPE_STRING: result = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_NUMERIC: result = cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_FORMULA: result = cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_ERROR: result = null; break; case HSSFCell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue(); break; case HSSFCell.CELL_TYPE_BLANK: result = null; break; default: break; } } return result; }
Example 16
Source File: ExcelUtil.java From jeewx with Apache License 2.0 | 4 votes |
/** * 读取 Excel文件内容 * * @param excel_name * @return * @throws Exception */ public static List<String[]> readExcel(String excel_name) throws Exception { // 结果集 List<String[]> list = new ArrayList<String[]>(); HSSFWorkbook hssfworkbook = new HSSFWorkbook(new FileInputStream( excel_name)); // 遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数 HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0); // 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数 for (int j = 0; j < hssfsheet.getPhysicalNumberOfRows(); j++) { HSSFRow hssfrow = hssfsheet.getRow(j); if(hssfrow!=null){ int col = hssfrow.getPhysicalNumberOfCells(); // 单行数据 String[] arrayString = new String[col]; for (int i = 0; i < col; i++) { HSSFCell cell = hssfrow.getCell(i); if (cell == null) { arrayString[i] = ""; } else if (cell.getCellType() == 0) { // arrayString[i] = new Double(cell.getNumericCellValue()).toString(); if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) { if (HSSFDateUtil.isCellDateFormatted(cell)) { Date d = cell.getDateCellValue(); // DateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); arrayString[i] = formater.format(d); } else { arrayString[i] = new BigDecimal(cell.getNumericCellValue()).longValue()+""; } } } else {// 如果EXCEL表格中的数据类型为字符串型 arrayString[i] = cell.getStringCellValue().trim(); } } list.add(arrayString); } } return list; }
Example 17
Source File: POIUtils.java From ermasterr with Apache License 2.0 | 4 votes |
public static void copyRow(final HSSFSheet oldSheet, final HSSFSheet newSheet, final int oldRowNum, final int newRowNum) { final HSSFRow oldRow = oldSheet.getRow(oldRowNum); final HSSFRow newRow = newSheet.createRow(newRowNum); if (oldRow == null) { return; } newRow.setHeight(oldRow.getHeight()); if (oldRow.getFirstCellNum() == -1) { return; } for (int colNum = oldRow.getFirstCellNum(); colNum <= oldRow.getLastCellNum(); colNum++) { final HSSFCell oldCell = oldRow.getCell(colNum); final HSSFCell newCell = newRow.createCell(colNum); if (oldCell != null) { final HSSFCellStyle style = oldCell.getCellStyle(); newCell.setCellStyle(style); final int cellType = oldCell.getCellType(); newCell.setCellType(cellType); if (cellType == Cell.CELL_TYPE_BOOLEAN) { newCell.setCellValue(oldCell.getBooleanCellValue()); } else if (cellType == Cell.CELL_TYPE_FORMULA) { newCell.setCellFormula(oldCell.getCellFormula()); } else if (cellType == Cell.CELL_TYPE_NUMERIC) { newCell.setCellValue(oldCell.getNumericCellValue()); } else if (cellType == Cell.CELL_TYPE_STRING) { newCell.setCellValue(oldCell.getRichStringCellValue()); } } } POIUtils.copyMergedRegion(newSheet, getMergedRegionList(oldSheet, oldRowNum), newRowNum); }
Example 18
Source File: XlsTable.java From Leo with Apache License 2.0 | 4 votes |
public Object getValue(int row, String column) throws DataSetException { if (logger.isDebugEnabled()) logger.debug("getValue(row={}, columnName={}) - start", Integer.toString(row), column); assertValidRowIndex(row); int columnIndex = getColumnIndex(column); HSSFCell cell = _sheet.getRow(row + 1).getCell(columnIndex); if (cell == null) { return null; } int type = cell.getCellType(); switch (type) { case HSSFCell.CELL_TYPE_NUMERIC: HSSFCellStyle style = cell.getCellStyle(); if (HSSFDateUtil.isCellDateFormatted(cell)) { return getDateValue(cell); } else if (XlsDataSetWriter.DATE_FORMAT_AS_NUMBER_DBUNIT.equals(style.getDataFormatString())) { // The special dbunit date format return getDateValueFromJavaNumber(cell); } else { return getNumericValue(cell); } case HSSFCell.CELL_TYPE_STRING: return cell.getRichStringCellValue().getString(); case HSSFCell.CELL_TYPE_FORMULA: throw new DataTypeException("Formula not supported at row=" + row + ", column=" + column); case HSSFCell.CELL_TYPE_BLANK: return null; case HSSFCell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue() ? Boolean.TRUE : Boolean.FALSE; case HSSFCell.CELL_TYPE_ERROR: throw new DataTypeException("Error at row=" + row + ", column=" + column); default: throw new DataTypeException("Unsupported type at row=" + row + ", column=" + column); } }
Example 19
Source File: OfficeConverter.java From BBSSDK-for-Android with Apache License 2.0 | 4 votes |
private void xlsToHtml() throws Throwable { FileOutputStream output = new FileOutputStream(new File(htmlPath)); StringBuffer htmlHeaderSB = new StringBuffer(); htmlHeaderSB.append("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' " + "xmlns='http://www.w3.org/TR/REC-html40'>"); htmlHeaderSB.append("<head><meta http-equiv=Content-Type content='text/html; charset=utf-8'><meta name=ProgId content=Excel.Sheet>" + "</head><body>"); output.write(htmlHeaderSB.toString().getBytes()); HSSFSheet sheet; HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath)); // 获整个Excel for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) { if (workbook.getSheetAt(sheetIndex) != null) { sheet = workbook.getSheetAt(sheetIndex);// 获得不为空的这个sheet if (sheet != null) { int firstRowNum = sheet.getFirstRowNum(); // 第一行 int lastRowNum = sheet.getLastRowNum(); // 最后一行 // 构造Table output.write(("<table width=\"100%\" style=\"border:1px solid #000;border-width:1px 0 0 1px;margin:2px 0 2px 0;" + "border-collapse:collapse;\">").getBytes()); for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) { if (sheet.getRow(rowNum) != null) {// 如果行不为空, HSSFRow row = sheet.getRow(rowNum); short firstCellNum = row.getFirstCellNum(); // 该行的第一个单元格 short lastCellNum = row.getLastCellNum(); // 该行的最后一个单元格 int height = (int) (row.getHeight() / 15.625); // 行的高度 output.write(("<tr height=\"" + height + "\" style=\"border:1px solid #000;border-width:0 1px 1px 0;" + "margin:2px 0 2px 0;\">").getBytes()); for (short cellNum = firstCellNum; cellNum <= lastCellNum; cellNum++) { // 循环该行的每一个单元格 HSSFCell cell = row.getCell(cellNum); if (cell != null) { if (cell.getCellType() != HSSFCell.CELL_TYPE_BLANK) { StringBuffer tdStyle = new StringBuffer("<td style=\"border:1px solid #000; border-width:0 1px 1px 0;" + "margin:2px 0 2px 0; "); HSSFCellStyle cellStyle = cell.getCellStyle(); HSSFPalette palette = workbook.getCustomPalette(); // 类HSSFPalette用于求颜色的国际标准形式 HSSFColor hColor = palette.getColor(cellStyle.getFillForegroundColor()); HSSFColor hColor2 = palette.getColor(cellStyle.getFont(workbook).getColor()); String bgColor = convertToStardColor(hColor);// 背景颜色 short boldWeight = cellStyle.getFont(workbook).getBoldweight(); // 字体粗细 short fontHeight = (short) (cellStyle.getFont(workbook).getFontHeight() / 2); // 字体大小 String fontColor = convertToStardColor(hColor2); // 字体颜色 if (bgColor != null && !"".equals(bgColor.trim())) { tdStyle.append(" background-color:"); tdStyle.append(bgColor); tdStyle.append("; "); } if (fontColor != null && !"".equals(fontColor.trim())) { tdStyle.append(" color:"); tdStyle.append(fontColor); tdStyle.append("; "); } tdStyle.append(" font-weight:"); tdStyle.append(boldWeight); tdStyle.append("; "); tdStyle.append(" font-size: "); tdStyle.append(fontHeight); tdStyle.append("%;"); output.write((tdStyle + "\"").getBytes()); int width = (int) (sheet.getColumnWidth(cellNum) / 35.7); // int cellRegionCol = getMergerCellRegionCol(sheet, rowNum, cellNum); // 合并的列(solspan) int cellRegionRow = getMergerCellRegionRow(sheet, rowNum, cellNum);// 合并的行(rowspan) String align = convertAlignToHtml(cellStyle.getAlignment()); // String vAlign = convertVerticalAlignToHtml(cellStyle.getVerticalAlignment()); output.write((" align=\"" + align + "\" valign=\"" + vAlign + "\" width=\"" + width + "\" ").getBytes()); output.write((" colspan=\"" + cellRegionCol + "\" rowspan=\"" + cellRegionRow + "\"").getBytes()); output.write((">" + getCellValue(cell) + "</td>").getBytes()); } } } output.write("</tr>".getBytes()); } } output.write(("</table>").getBytes()); } } } output.write(("</body></html>").getBytes()); output.close(); }
Example 20
Source File: POIUtils.java From ermaster-b with Apache License 2.0 | 4 votes |
public static void copyRow(HSSFSheet oldSheet, HSSFSheet newSheet, int oldRowNum, int newRowNum) { HSSFRow oldRow = oldSheet.getRow(oldRowNum); HSSFRow newRow = newSheet.createRow(newRowNum); if (oldRow == null) { return; } newRow.setHeight(oldRow.getHeight()); if (oldRow.getFirstCellNum() == -1) { return; } for (int colNum = oldRow.getFirstCellNum(); colNum <= oldRow .getLastCellNum(); colNum++) { HSSFCell oldCell = oldRow.getCell(colNum); HSSFCell newCell = newRow.createCell(colNum); if (oldCell != null) { HSSFCellStyle style = oldCell.getCellStyle(); newCell.setCellStyle(style); int cellType = oldCell.getCellType(); newCell.setCellType(cellType); if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) { newCell.setCellValue(oldCell.getBooleanCellValue()); } else if (cellType == HSSFCell.CELL_TYPE_FORMULA) { newCell.setCellFormula(oldCell.getCellFormula()); } else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) { newCell.setCellValue(oldCell.getNumericCellValue()); } else if (cellType == HSSFCell.CELL_TYPE_STRING) { newCell.setCellValue(oldCell.getRichStringCellValue()); } } } POIUtils.copyMergedRegion(newSheet, getMergedRegionList(oldSheet, oldRowNum), newRowNum); }