Java Code Examples for org.apache.poi.hssf.usermodel.HSSFSheet#getPhysicalNumberOfRows()

The following examples show how to use org.apache.poi.hssf.usermodel.HSSFSheet#getPhysicalNumberOfRows() . 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: FileUtil.java    From JavaWeb with Apache License 2.0 6 votes vote down vote up
public static void readExcel(String filePth) throws Exception {
	InputStream is = new FileInputStream(filePth);
	//创建工作薄
	//XSSFWorkbook hwb = new XSSFWorkbook(is);
	HSSFWorkbook hwb = new HSSFWorkbook(new POIFSFileSystem(is));
	//得到sheet
	for (int i = 0; i < hwb.getNumberOfSheets(); i++) {
		HSSFSheet sheet = hwb.getSheetAt(i);
		int rows = sheet.getPhysicalNumberOfRows();
		//遍历每一行
		for (int j = 0; j < rows; j++) {
			HSSFRow hr = sheet.getRow(j);
			Iterator<?> it = hr.iterator();
			while(it.hasNext()){
				String context = it.next().toString();
				System.out.println(context);
			}
		}
	}
	hwb.close();
}
 
Example 2
Source File: FileUtil.java    From JavaWeb with Apache License 2.0 6 votes vote down vote up
public static void readExcel(String filePth) throws Exception {
	InputStream is = new FileInputStream(filePth);
	//创建工作薄
	//XSSFWorkbook hwb = new XSSFWorkbook(is);
	HSSFWorkbook hwb = new HSSFWorkbook(new POIFSFileSystem(is));
	//得到sheet
	for (int i = 0; i < hwb.getNumberOfSheets(); i++) {
		HSSFSheet sheet = hwb.getSheetAt(i);
		int rows = sheet.getPhysicalNumberOfRows();
		//遍历每一行
		for (int j = 0; j < rows; j++) {
			HSSFRow hr = sheet.getRow(j);
			Iterator<?> it = hr.iterator();
			while(it.hasNext()){
				String context = it.next().toString();
				System.out.println(context);
			}
		}
	}
	hwb.close();
}
 
Example 3
Source File: XlsSessionReader.java    From conference-app with MIT License 6 votes vote down vote up
private List<Session> readAllSessions(InputStream is) {
    final List<Session> result = new ArrayList<Session>();

    try {
        final POIFSFileSystem fileSystem = new POIFSFileSystem(is);
        final HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        final HSSFSheet sheet = workBook.getSheet("Alle Tage");

        int rows = sheet.getPhysicalNumberOfRows();
        // as the row is a header we start with the second one
        for (int r = 1; r < rows; r++) {
            final HSSFRow row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            final Session session = getSessionFromRow(row, r);
            if (session != null) {
                result.add(session);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error while reading sessions from file", e);
    }
    return result;
}
 
Example 4
Source File: HSSFExcelParser.java    From ureport with Apache License 2.0 5 votes vote down vote up
private int buildMaxColumn(HSSFSheet sheet){
	int rowCount=sheet.getPhysicalNumberOfRows();
	int maxColumnCount=0;
	for(int i=0;i<rowCount;i++){
		HSSFRow row=sheet.getRow(i);
		if(row==null){
			continue;
		}
		int columnCount=row.getPhysicalNumberOfCells();
		if(columnCount>maxColumnCount){
			maxColumnCount=columnCount;
		}
	}
	return maxColumnCount;
}
 
Example 5
Source File: ExcelUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 读取 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 6
Source File: ExcelUtil.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 读取 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 7
Source File: ExcelUtil.java    From jeewx with Apache License 2.0 4 votes vote down vote up
/**
	 * 读取 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;
	}