1.上传省略
2.得到文件后,采用POIFSFileSystem 进行解析
/*** @return 批量调整用户财富
*/
public String batchRegulateMoneybag(){
HttpServletRequest request=this.getRequest();
try {
if(file!=null){ //判断文件是否为空 不为空 就进行解析
User sessionuser = (User) request.getSession(true).getAttribute(Constants.SESSION_USER);
//Excel 2003
if(file.getContentType()!=null && "application/vnd.ms-excel".equals(file.getContentType())){
POIFSFileSystem poiFileSystem = new POIFSFileSystem(file.getInputStream());
//得到文档对象
HSSFWorkbook workbook = new HSSFWorkbook(poiFileSystem);
//得到第一个表单
HSSFSheet aSheet = workbook.getSheetAt(0);
String msg="";
if (aSheet == null) {
logger.error("workbook.getSheetAt(0) is null!");
msg="上传的文档中没有内容!";
return SUCCESS;
}
//得到最后一行的 行号
int lastRow = aSheet.getLastRowNum();
if(lastRow > 5000){
msg= "文档记录超过了5000条,请拆分文档,<B>拆分时请保证同一订单号的订单在一个文件里</B>";
request.setAttribute("msg", msg);
return SUCCESS;
}
//存放错误信息的集合
List<String> errorList=new ArrayList<String>();
//从0 开始 跳过第一列(不处理表头)
int amount=lastRow;
//调整记录成功的条数
int samount=0;
//调整失败的记录数
int famount=0;
//循环取出每行 然后 循环每一列
for(int i=1;i<=lastRow;i++){
HSSFRow row=aSheet.getRow(i); //得到 第 n 行
int lastCell=row.getLastCellNum(); //得到 n行的总列数
if(lastCell!=6){ //不是 6 的就有问题
errorList.add("第【"+(row.getRowNum()+1)+"】行,总列数不正确!");
famount++;
continue; //结束这次循环
}else{
boolean flag=true; //默认是执行 调整的 如果 用户名为空就不执行了
String [] params=new String[6]; //用户保存 一行记录中的 6个值
for(int j=0;j<lastCell;j++){
HSSFCell cell= row.getCell(j); //得到每行 第 n列
if(j==0 && cell==null){ //判断第一个用户名是否为空 如果为空 就跳出这次循环 不进行用户
errorList.add("第【"+(row.getRowNum()+1)+"】行【"+(j+1)+"】列,用户名为空!");
famount++;
flag=false;
break;
}else{
String param= getCellValue(cell); //解析当前列的值
params[j]=param;
}
}
if(flag){
//经过上面的处理后 获取到每一行的 6个值 然后进行 修改 用户的财富
int result=upUsermoneyBag(sessionuser,params);
if(result==1){
samount++;
}else if(result==0){
errorList.add("第【"+(row.getRowNum()+1)+"】行,调整财富失败!");
famount++;
}else if(result==-1){
errorList.add("第【"+(row.getRowNum()+1)+"】行,用户名不存在!");
famount++;
}
}
}
}
request.setAttribute("amount", amount);
request.setAttribute("famount", famount);
request.setAttribute("samount", samount);
request.setAttribute("errorList", errorList);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
解析每一列的数据
3.
/**
* @author LuoB.
* @return 得到当前列的值
*/
private String getCellValue(HSSFCell cell){
String param="";
if(cell==null){
param="0";
}else{
int type=cell.getCellType();
switch (type) {
case 0:
Double d=cell.getNumericCellValue();
param=d.toString();
break;
case 1:
param=cell.getStringCellValue();
break;
default:
param="0";
break;
}
}
return param;
}
4.使用上面的解析出来的数据
/**
* @param sessionuser 操作者
* @param params Excel表格中 一行的 6列值
* @return 1 成功 0 失败 -1 不存在这个用户
*/
private int upUsermoneyBag(User sessionuser,String [] params){
try{
if(params.length==6){
BiUser user=iBiUserService.findBiUserByName(params[0]);
Double cashback=Util.parseDouble(params[1]);
/*String cstr=params[2];
cstr=cstr.contains(".")?cstr.substring(0,cstr.indexOf(".")):cstr;*/
Long credits=Util.parseLong(params[2]);
Double nscashback=Util.parseDouble(params[3]);
/*String nscstr=params[4];
nscstr=nscstr.contains(".")?nscstr.substring(0,nscstr.indexOf(".")):nscstr;*/
Long nscredits=Util.parseLong(params[4]);
String note=params[5];
if(user != null){
BiMessage biMessage = new BiMessage();
biMessage.setContent(sessionuser.getUsername()+"调整用户“"+user.getLoginname()+"”的财富,现金"+(cashback+nscashback)+",贝壳"+(credits+nscredits)+" 。用户的IP是"+user.getYahooim()+",用户最后登录时间"+MyUtils.getTimeString(user.getLastlogin()));
biMessage.setTitle(user.getLoginname() + "调整财富提醒");
biMessage.setType("sys");
biMessage.setFromuser("系统");
biMessage.setTouser("购物狂");
biMessage.setTouserid(19717165l);
biMessage.setDownflag("0");
iMessageService.sendTextMsg(biMessage);
iBiUserService.regulateMoneybag(user, cashback, credits,nscashback,nscredits, sessionuser.getUsername(), note);
return 1;
}
else{
return -1;
}
}
return 0;
}catch(Exception e){
e.printStackTrace();
return 0;
}
}
poi-3.6 不支持 2007 的excel 3.7 就可以了