// excel 文件上传例子
public ActionForward productNeworderExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception{
HttpSession session=request.getSession();
if(session.getAttribute("managerid")==null){
return new ActionForward("/manager/sys_manager_login.jsp");
}
ProductNeworderForm proNewOrderForm = (ProductNeworderForm) form;
String strfilepathname = "";
FormFile upfile = proNewOrderForm.getUploadFileIn();//得到上传文件
if(upfile.getFileSize() == 0){
saveToken(request);
request.setAttribute("error", "请先选择要上传入库的本地Excel文件!");
return new ActionForward("/productNeworder.do?method=showProductPrograma") ;
}
else{
int len = upfile.getFileName().lastIndexOf(".");
String sfilename = upfile.getFileName().substring(len + 1); // 得到扩展名
sfilename = sfilename.toLowerCase(); // 将扩展名转换成小写
if(!"xls".equalsIgnoreCase(sfilename)&&!"xlsx".equalsIgnoreCase(sfilename)){
saveToken(request);
request.setAttribute("error", "上传文件格式不正确,必须为Execl文件!");
return new ActionForward("/productNeworder.do?method=showProductPrograma") ;
}
System.out.println(request.getRealPath("/"));
File file = new File(request.getRealPath("/") + "excel/");
if (!file.exists()) {
file.mkdir();
}
strfilepathname = "excel/productProgramaInUpload." + sfilename;// 上传路径+文件名称
// 上传文件
OutputStream bos = null;
InputStream stream = null;
stream = upfile.getInputStream();// 把文件读入
String filePath = request.getRealPath("/");// 取当前系统路径
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 建立一个上传文件的输出流
String str = filePath + strfilepathname;
bos = new FileOutputStream(str);
int bytesRead = 0;
byte[] buffer = new byte[4096];
while ((bytesRead = stream.read(buffer, 0, 4096)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件写入服务器
}
// 开始读文件
String real = request.getRealPath("/");
String sep = System.getProperties().getProperty("file.separator"); // 获取 \
real += "excel" + sep + "productProgramaInUpload.xls";
Workbook workbook = Workbook.getWorkbook(new File(real));
Sheet sheet = workbook.getSheet(0);
int rows = sheet.getRows();
String message="";
if(rows<2){
saveToken(request);
request.setAttribute("error", "上传的Execl文件必须是指定格式(行数过小),没有模板请先下载模板文件! ");
return new ActionForward("/productNeworder.do?method=showProductPrograma") ;
}else{
Cell[] ce = sheet.getRow(1);
if(ce.length<11||ce==null){
saveToken(request);
request.setAttribute("error", "上传的Execl文件必须是指定格式(列数过小),没有模板请先下载模板文件! ");
return new ActionForward("/productNeworder.do?method=showProductPrograma") ;
}
String productid=ce[0].getContents().trim(); //序号
String productcode=ce[1].getContents().trim();//产品类型名称
String parentid=ce[2].getContents().trim();//所属栏目
String neworder=ce[3].getContents().trim();//排序号
if(!"序号".equals(productid)||!"产品类型名称".equals(productcode)
||!"所属栏目".equals(parentid)||!"排序号".equals(neworder)){
saveToken(request);
request.setAttribute("error", "上传的Execl文件必须是指定格式,没有模板请先下载模板文件! ");
return new ActionForward("/productNeworder.do?method=showProductPrograma") ;
}
}
boolean productcodeBool = true;
boolean temp=true;
for (int i = 2; i < rows; i++) {
Cell[] rs = sheet.getRow(i);
// 获取顺序从左 - 到右
Integer productid= 0; //序号
String productcode=""; //产品类型名称
Integer parentid=0; //所属栏目
Integer neworder=0; //排序号
String productNo = "";
String parentNo = "";
String neworderNo = "";
//获取Excel表格中的值
try {
productNo = rs[0].getContents();
productcode = rs[1].getContents();
parentNo = rs[2].getContents();
neworderNo = rs[3].getContents();
} catch (Exception e) {
e.printStackTrace();
}
//判断序号是否为空,为空结束上传
if("".equals(productNo)||null == productNo){
break;
}
else{
//判断序号是否为整数
Pattern pattern = Pattern.compile("[0-9]*");
if(pattern.matcher(productNo).matches()==false){
message+=" <font style='color: red;'>序号:"+productid+" "+productNo+"必须为整数</font> ";
temp=false;
}else{
productid = Integer.parseInt(productNo);
}
if("".equals(productcode)||null == productcode){
message+=" <font style='color: red;'>序号:"+productid+" 产品类型名称格式不正确,请检查</font> ";
temp = false;
}
if(pattern.matcher(parentNo).matches()==false){
message+=" <font style='color: red;'>序号:"+productid+" "+parentNo+"必须为整数</font> ";
temp=false;
}else{
parentid = Integer.parseInt(parentNo);
}
if(pattern.matcher(neworderNo).matches()==false){
message+=" <font style='color: red;'>序号:"+productid+" "+neworderNo+"必须为整数</font> ";
temp=false;
}else{
neworder = Integer.parseInt(neworderNo);
}
// 循环之前判断产品类型名称是否存在,存在就提示出错 产品类型名称 - excel中与数据库对比
List<TProductDetail> listProDetail = productDetailManager.findByProductcode(productcode.trim());
if(listProDetail.isEmpty()||listProDetail.size()>1){
message+=" <font style='color: red;'>序号:"+productid+" "+productcode+" 不存在或不是唯一,请检查正确后重新操作</font> ";
productcodeBool = false;
}
// 中途不让出错,插入之前要去检查 excel是否正确格式
}
if(productcodeBool&&temp){
ProductNeworderForm pNeworder = new ProductNeworderForm();
// 获取信息 添加入库
ProductNeworderForm proNeworder = new ProductNeworderForm();
List<TProductDetail> proDetail = productDetailManager.findByProductcode(productcode);
proNeworder.setProductid(proDetail.get(0).getProductid());
String procodeString = proDetail.get(0).getBrandEn()+"["+proDetail.get(0).getBrandZh()+"]"+productcode;
proNeworder.setShowname(procodeString);
proNeworder.setNeworder(neworder);
proNeworder.setAddtime(new Date());
proNeworder.setTypeid(proDetail.get(0).getTypeid());
String parentidString = "栏目["+parentid+"]";
proNeworder.setParentid(parentidString);
List<ProductNeworderForm> listProNForm = productNeworderManager.findByProductid(proDetail.get(0).getProductid());
if(listProNForm.isEmpty()){
List<ProductNeworderForm> listPNF = productNeworderManager.findByProidAndParentidAndNeworder(proDetail.get(0).getProductid(), parentidString, neworder);
if(listPNF.isEmpty()){
productNeworderManager.save(proNeworder);
message += " 序号:" + productid +" " + productcode + " 成功入库! ";
}else{
message+=" <font style='color: red;'>序号:"+productid+" "+parentidString+"下的排序号 "+neworder+" 已经存在,请检查后重新操作</font> ";
}
}else{
message+=" <font style='color: red;'>序号:"+productid+" "+productcode+" 已经存在,请不要重复上传</font> ";
}
}
productcodeBool = true;
temp = true;
}
request.setAttribute("message", message);
return new ActionForward("/productNeworder.do?method=showProductPrograma");
}
}