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