Web利用POI导入XML

1.准备好excel如下:
2.编写jsp页面,用于上传文件
< s:form action ="importStudent" method= "post"
                   enctype ="multipart/form-data" >
                   导入Excel文件: < s:file name ="importFile" ></ s:file>
                   < br />
                   < s:submit value ="导入" ></ s:submit>
 </ s:form>

<  s:form   action  = "importStudent"   method =  "post"
                     enctype  = "multipart/form-data"   >
                   导入Excel文件:   <  s:file   name  = "importFile"   ></  s:file >
                     <  br   />
                     <  s:submit   value  = "导入"   ></  s:submit >
  </  s:form >
2.编写Action,用于解析XML

public class ImportExcelAction extends ActionSupport {
       private File importFile ;
       private String importFileFileName ;
       private List<Student> students ;
       public String importStudent() {
             try {
                   if (importFile != null) {
                        Workbook workBook = this .createWorkbook( new FileInputStream(
                                     importFile ));
                         if (workBook != null) {
                               students = new ArrayList<Student>();
                               for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
                                    Sheet sheet = workBook.getSheetAt(i);
                                     if (sheet != null) {
                                           for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                                                Row row = sheet.getRow(j);
                                                 if (row != null) {
                                                      Student stu = new Student();
                                                      stu.setId(( int ) row.getCell(0)
                                                                  .getNumericCellValue());
                                                      stu.setName(row.getCell(1)
                                                                  .getStringCellValue());
                                                      stu.setAge(( int ) row.getCell(2)
                                                                  .getNumericCellValue());
                                                      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                                                                   "yyyy-mm-dd" );
                                                      stu.setBirth(simpleDateFormat.parse(row
                                                                  .getCell(3).getStringCellValue()));
                                                       students .add(stu);
                                                }
                                          }
                                    }
                              }
                               return SUCCESS ;
                        }
                  }
            } catch (Exception e) {
                   // TODO: handle exception
                  e.printStackTrace();
            }
             return ERROR ;
      }
       private Workbook createWorkbook(InputStream is) throws IOException {
             if (importFileFileName == null || "" .equals(importFileFileName ))
                   return null ;
             if (importFileFileName .endsWith( "xls")) // HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是. xls
                   return new HSSFWorkbook(is);
             if (importFileFileName .endsWith( "")) // XSSFWorkbook:是操作Excel2007的版本,扩展名是. xlsx
                   return new XSSFWorkbook(is);
             return null ;
      }
       public void setImportFile(File importFile) {
             this .importFile = importFile;
      }
       public File getImportFile() {
             return importFile ;
      }
       public void setImportFileFileName(String importFileFileName) {
             this .importFileFileName = importFileFileName;
      }
       public String getImportFileFileName() {
             return importFileFileName ;
      }
       public void setStudents(List<Student> students) {
             this .students = students;
      }
       public List<Student> getStudents() {
             return students ;
      }
}


3.配置struts的配置文件
< action name ="importStudent" class= "ImportExcelAction" method ="importStudent" >
                   < result name ="success" > /result.jsp</ result >
                   < result name ="error" > /error.jsp</ result >
</ action>


4.返回结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值