Comparator排序例子

Comparator排序例子,根据list 中对象的某一个属性大小排序

import java.util.Comparator;
List<PoDetail> returnList = new ArrayList();

Collections.sort(returnList,new Comparator<PoDetail>(){  
				public int compare(PoDetail arg0, PoDetail arg1) {  
				  return Integer.valueOf(arg0.getLineNo()).compareTo( Integer.valueOf(arg1.getLineNo()));
				}  
			});


File upload 支持2种版本的方法:



import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/** analyze excel and convert to ExcelUploadViewModel
	 * @param event
	 * @return
	 */
	public ExcelUploadViewModel getDataFromExcel(FileUploadEvent event){
		
		Workbook workbook = null;
		Sheet sheet = null;
		Row row = null;
	    Cell cell = null;
	    StringBuffer message_buffer = new StringBuffer();
		boolean success = true;
		
		ExcelUploadViewModel excelViewModel = new ExcelUploadViewModel();
		
		
		int i_row = 0;
		String str_val=null;
		
		String[] row_arr = new String[MAX_COL];
        List<String[]> excel_arr = new ArrayList<String[]>();
		try {
			
	        if (event.getFile().getFileName().endsWith(".xls")) {
	            workbook = new HSSFWorkbook(event.getFile().getInputstream());
	        } else if (event.getFile().getFileName().endsWith(".xlsx")) {
	            workbook = new XSSFWorkbook(event.getFile().getInputstream());
	        }
			
			sheet = workbook.getSheetAt(0);
			int rowCount = sheet.getPhysicalNumberOfRows();
			

			if (rowCount > MAX_ROW) {
				message_buffer.append("The upload file has more than "
						+ MAX_ROW + " rows!\n");
				success = false;
			}else if(rowCount==0){
				message_buffer.append("The upload file has no data .");
				success = false;
			}
			/**
			 * int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();
			 * if(columnCount!=MAX_COL){
				message_buffer.append("The upload file doesn’t match with template. ");
				success = false;
			}*/
			 if(sheet!=null && success ){
				 i_row = sheet.getLastRowNum();
				 //set excel value to arraylist
                 for (int i = 0;i <= i_row;i++) {
                	 PoUploadModel model = new PoUploadModel();
                     row_arr = new String[MAX_COL];
                     row = sheet.getRow(i);
                     for (int j = 0;j < MAX_COL;j++) {
                         cell = row.getCell(j);
                         if (cell != null) {
                             cell.setCellType(Cell.CELL_TYPE_STRING);
                             str_val=cell.getStringCellValue();
                             
                             if(str_val==null){
                                 str_val="";
                             }else{
                                 str_val=str_val.trim();
                             }
                             row_arr[j] = str_val;
                         }
                     }
                    excel_arr.add(row_arr);
                 }
				
			}

		} catch (Exception ee) {
			ee.printStackTrace();	
			excelViewModel.setErrCode("false");
			excelViewModel.setErrCode("upload excel failed:"+ee.getMessage());
		}finally{
			if(!success){
				excelViewModel.setErrCode("false");
				excelViewModel.setMessage(message_buffer.toString());
			}
			excelViewModel.setDataList(excel_arr);
			
		}
		return excelViewModel;
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值