Java Code Examples for org.apache.poi.hssf.usermodel.HSSFRow#getPhysicalNumberOfCells()

The following examples show how to use org.apache.poi.hssf.usermodel.HSSFRow#getPhysicalNumberOfCells() . 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: XLSFileNormalizer.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private int getColumnPosition(String columnName, HSSFRow row){
	int cells = row.getPhysicalNumberOfCells();
	logger.debug("\nROW " + row.getRowNum() + " has " + cells
			+ " cell(s).");
	for (int c = 0; c < cells; c++) {
		//get single cell
		HSSFCell cell = row.getCell(c);
		
		String valueField = null;
		try {
			valueField = parseCell(cell);
			
			if (valueField.equals(columnName)){
				return c;
			}
			
		} catch(Throwable t) {
			throw new RuntimeException("Impossible to parse cell [" + c + "]", t);
		}

	}
	return -1;
	
}
 
Example 2
Source File: XLSFileNormalizer.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addColumnHeader(HSSFRow row){
	//Get all existing columns names
	List<String> columnsNames = new ArrayList<String>();
	int cells = row.getPhysicalNumberOfCells();
	this.setNumberOfColumns(cells);

	for (int c = 0; c < cells; c++) {
		//get single cell
		HSSFCell cell = row.getCell(c);
		
		String valueField = null;
		try {
			valueField = parseCell(cell);
			columnsNames.add(valueField);
			
			
		} catch(Throwable t) {
			throw new RuntimeException("Impossible to parse cell [" + c + "]", t);
		}
	}
	
	String newColumnName = "ref_"+levelName;
	
	//Check if exist already a column with the same name, otherwise generate a new name
	while( columnsNames.contains(newColumnName)){
		int count = 2;
		newColumnName = newColumnName+"_"+count;
		count++;
	}
	
	//Add a new column header
	HSSFCell newCell = row.createCell(cells);
	newCell.setCellValue(newColumnName);
	
	this.setNewColumnName(newColumnName);
}
 
Example 3
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 4
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 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>> 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 6
Source File: InvestmentSummaryController.java    From primefaces-blueprints with The Unlicense 5 votes vote down vote up
public void postProcessXLS(Object document) {  
    HSSFWorkbook wb = (HSSFWorkbook) document;  
    HSSFSheet sheet = wb.getSheetAt(0);  
    HSSFRow header = sheet.getRow(0);  
      
    HSSFCellStyle cellStyle = wb.createCellStyle();    
    cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);  
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
      
    for(int i=0; i < header.getPhysicalNumberOfCells();i++) {  
        HSSFCell cell = header.getCell(i);  
          
        cell.setCellStyle(cellStyle);  
    }  
    
    Row row=sheet.createRow((short)sheet.getLastRowNum()+3);
    Cell cellDisclaimer = row.createCell(0);
    HSSFFont customFont= wb.createFont();
    customFont.setFontHeightInPoints((short)10);
    customFont.setFontName("Arial");
    customFont.setColor(IndexedColors.BLACK.getIndex());
    customFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    customFont.setItalic(true);
    
    cellDisclaimer.setCellValue("Disclaimer");
    HSSFCellStyle cellStyleDisclaimer = wb.createCellStyle();
    cellStyleDisclaimer.setFont(customFont);
    cellDisclaimer.setCellStyle(cellStyleDisclaimer);
    
    Row row1=sheet.createRow(sheet.getLastRowNum()+2);
    Cell cellDisclaimerContent1 = row1.createCell(0);
    cellDisclaimerContent1.setCellValue("The information contained in this website is for information purposes only, and does not constitute, nor is it intended to constitute, the provision of financial product advice.");
    
    Row row2=sheet.createRow(sheet.getLastRowNum()+1);
    Cell cellDisclaimerContent2 = row2.createCell(0);
    cellDisclaimerContent2.setCellValue("This website is intended to track the investor account summary information,investments and transaction in a partcular period of time. ");
    
}
 
Example 7
Source File: AccountSummaryController.java    From primefaces-blueprints with The Unlicense 5 votes vote down vote up
public void postProcessXLS(Object document) {  
    HSSFWorkbook wb = (HSSFWorkbook) document;  
    HSSFSheet sheet = wb.getSheetAt(0);  
    HSSFRow header = sheet.getRow(0);  
    HSSFCellStyle cellStyle = wb.createCellStyle();    
    cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);  
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
      
    for(int i=0; i < header.getPhysicalNumberOfCells();i++) {  
        HSSFCell cell = header.getCell(i);  
          
        cell.setCellStyle(cellStyle);  
    }  
    Row row=sheet.createRow((short)sheet.getLastRowNum()+3);
    Cell cellDisclaimer = row.createCell(0);
    HSSFFont customFont= wb.createFont();
    customFont.setFontHeightInPoints((short)10);
    customFont.setFontName("Arial");
    customFont.setColor(IndexedColors.BLACK.getIndex());
    customFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    customFont.setItalic(true);
    
    cellDisclaimer.setCellValue("Disclaimer");
    HSSFCellStyle cellStyleDisclaimer = wb.createCellStyle();
    cellStyleDisclaimer.setFont(customFont);
    cellDisclaimer.setCellStyle(cellStyleDisclaimer);
    
    Row row1=sheet.createRow(sheet.getLastRowNum()+2);
    Cell cellDisclaimerContent1 = row1.createCell(0);
    cellDisclaimerContent1.setCellValue("The information contained in this website is for information purposes only, and does not constitute, nor is it intended to constitute, the provision of financial product advice.");	    
    Row row2=sheet.createRow(sheet.getLastRowNum()+1);
    Cell cellDisclaimerContent2 = row2.createCell(0);
    cellDisclaimerContent2.setCellValue("This website is intended to track the investor account summary information,investments and transaction in a partcular period of time. ");
    
}
 
Example 8
Source File: TransactionSummaryController.java    From primefaces-blueprints with The Unlicense 5 votes vote down vote up
public void postProcessXLS(Object document) {
	HSSFWorkbook wb = (HSSFWorkbook) document;
	HSSFSheet sheet = wb.getSheetAt(0);
	HSSFRow header = sheet.getRow(0);

	HSSFCellStyle cellStyle = wb.createCellStyle();
	cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
	cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

	for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
		HSSFCell cell = header.getCell(i);

		cell.setCellStyle(cellStyle);
	}

	Row row = sheet.createRow((short) sheet.getLastRowNum() + 3);
	Cell cellDisclaimer = row.createCell(0);
	HSSFFont customFont = wb.createFont();
	customFont.setFontHeightInPoints((short) 10);
	customFont.setFontName("Arial");
	customFont.setColor(IndexedColors.BLACK.getIndex());
	customFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	customFont.setItalic(true);

	cellDisclaimer.setCellValue("Disclaimer");
	HSSFCellStyle cellStyleDisclaimer = wb.createCellStyle();
	cellStyleDisclaimer.setFont(customFont);
	cellDisclaimer.setCellStyle(cellStyleDisclaimer);

	Row row1 = sheet.createRow(sheet.getLastRowNum() + 2);
	Cell cellDisclaimerContent1 = row1.createCell(0);
	cellDisclaimerContent1
			.setCellValue("The information contained in this website is for information purposes only, and does not constitute, nor is it intended to constitute, the provision of financial product advice.");

	Row row2 = sheet.createRow(sheet.getLastRowNum() + 1);
	Cell cellDisclaimerContent2 = row2.createCell(0);
	cellDisclaimerContent2
			.setCellValue("This website is intended to track the investor account summary information,investments and transaction in a partcular period of time. ");

}
 
Example 9
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;
	}