基于struts2及poi实现的导入功能

1、使用到的jar包:poi-3.7-20101029.jar、poi-ooxml-3.7-20101029.jar、poi-ooxml-schemas-3.7-20101029.jar、poi-scratchpad-3.7-20101029.jar;

2、前端发送请求的action:

   <body>
   <s:form id="form1" action="/system/black!impBlackLists.action" namespace="/system" method="post" theme="simple"  enctype="multipart/form-data">
    <div class="rightt">
  <div class="righttcn">系统设置<span> > </span> 黑名单管理<span> > </span>信息导入</div>
 </div>
 <div class="right">
  <div class="tbsp">
   <div class="ckxxcn">
    <div class="list">
     <table width="100%" border="0"  cellpadding="0" cellspacing="0" align="center" >
         <tr height="30">
          <td class="cn01">
           选择导入的文件:
          </td>
          <td class="cn02">
           <s:file name="importFile"></s:file>
          </td>
         </tr>
     </table>
    </div>
   </div>
  </div>
  <table width="100%" border="0" align="center" cellpadding="0" style="margin-top: 10px;margin-bottom: 5px;"
   cellspacing="0">
   <tr height="30">
    <td class="cn02" align="left">
     <s:submit cssClass="blue_btn_s" value="导入"></s:submit>
    </td>
   </tr>
  </table>
 </div>
   </s:form>
   <s:if test="prompt!=null">
<s:property value="prompt" escape="false"/>
<s:if test="prompt.contains('成功')">
<script type="text/javascript">
parent.document.getElementById('bntSearch').click();
</script>
</s:if>
</s:if>
  </body>

struts2xml配置:

<action name="black" class="com.yicomm.system.web.action.blackListAction">
      <result name="resultList">/system/blacklist/blacklist_index.jsp</result>
      <result name ="editSuc">/system/blacklist/blacklist_edit.jsp</result>
      <result name ="blackInfo">/system/blacklist/blacklist_edit.jsp</result>
      <result name="delSuc" type="json"></result>
      <result name="impSuc">/system/blacklist/blacklist_import.jsp</result>
     </action>

后台实现的action方法:

 private File importFile; // 上传的文件
 private String importFileFileName; // 保存原始文件名

 

// 信息导入
 public String impBlackLists() throws IOException, Exception{
  String returnMsg = this.valBlackLists( this.getImportFile(), this.getImportFileFileName());
  if(returnMsg == ""){
   returnMsg = this.importBlackLists(this.getImportFile(), this.getImportFileFileName());
   setPrompt("<script>alert('"+returnMsg+"')</script>");
  }
  else{
   setPrompt("<script>alert('"+returnMsg+"')</script>");
  }
  return "impSuc";
 }
 // 判断文件类型
 private Workbook createWorkBook(InputStream is,String importFileFleName) throws IOException{
  if(importFileFleName.toLowerCase().endsWith("xls")){
  return new HSSFWorkbook(is);
  }
  if(importFileFleName.toLowerCase().endsWith("xlsx")){
  return new XSSFWorkbook(is);
  }
  return null;
 }
 // 导入信息验证
 private String valBlackLists(File filename,String importFileFleName) throws Exception, IOException{
  String returnMsg = "";
  Workbook book = this.createWorkBook(new FileInputStream(filename),importFileFleName);
  Sheet sheet = book.getSheetAt(0); // 获得工作簿对象
  Row row = sheet.getRow(0);
  // 判断是否为标准模板
  if(!row.getCell(0).getStringCellValue().equals("类型")
     || !row.getCell(1).getStringCellValue().equals("昵称")
     || !row.getCell(2).getStringCellValue().equals("备注")){
   returnMsg = "请使用标准模板";
  }
  else if(sheet.getLastRowNum() <= 0){
   returnMsg = "这个模板内容为空";
  }
  else{
   for( int i =1; i<=sheet.getLastRowNum();i++){
    row = sheet.getRow(i);
    if(null == row.getCell(0)){
     returnMsg +="第"+(i+1)+"行第1列类型不能为空";
    }
    if(null == row.getCell(1)){
     returnMsg +="第"+(i+1)+"行第2列昵称不能为空";
    }
   }
  }
  return returnMsg;
 }
 // 导入信息
 private String importBlackLists(File filename,String importFileFleName) throws Exception, IOException{
  String returnMsg = "";
  int ii = 0; // 成功条数
  int jj = 0; // 失败条数
  Workbook book = this.createWorkBook(new FileInputStream(filename),importFileFleName);
  Sheet sheet = book.getSheetAt(0); // 获得工作簿对象
  Row row = sheet.getRow(0);
  for( int i =1; i<=sheet.getLastRowNum();i++){
   row = sheet.getRow(i);
   String nick = row.getCell(1).getStringCellValue();
   List<Blacklist> list = EJBFactory4Sys.getBlacklistFacadeLocal().findByNick(nick);
   if(list !=null && list.size() >0){
    ii++;
    continue;
   }
   Blacklist entity = new Blacklist();
   entity.setNick(row.getCell(1).getStringCellValue());
   entity.setType(row.getCell(0).getStringCellValue());
   entity.setRemark(row.getCell(2).getStringCellValue());
   entity.setOpUser(getCurrMember().getUserName());
   
   entity.setOpDate(new Date());
   entity.setSource("0"); // 手动输入
   entity.setTimes(1); // 输入次数
   entity.setUpUser(getCurrMember().getUserName());
   entity.setUpDate(new Date());
   
   EJBFactory4Sys.getBlacklistFacadeLocal().save(entity);
   jj++;
   
  }
  returnMsg +="总共导入【"+(ii+jj)+"】条记录,成功【"+jj+"】条,失败【"+ii+"】条";
  return returnMsg;
 }

 

 

你可能感兴趣的:(poi,struts2)