excel 、 csv 文件上传

1. excel上传

import jar: 


org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(inp);
         for(int i=0, sheetLength=workbook.getNumberOfSheets(); i<sheetLength; i++)
         {
            Sheet sheet = workbook.getSheetAt(i);
            if(sheet == null)
            {
               continue;
            }
            int userCount = 0;
            for(int j=1, rowLength=sheet.getLastRowNum(); j<=rowLength; j++)
            {
               Row row = sheet.getRow(j);
               if(row != null && row.getRowNum() > 0)
               {
                  user = new User();
                  try
                  {
                     user.setUserName(getValue(row.getCell(0)));
                     user.setEmail(getValue(row.getCell(1)));
                     user.setCellphone(getValue(row.getCell(2)));
                     user.setTelephone(getValue(row.getCell(3)));
                  }
                  catch(Exception e)
                  {
                     appendImportExcelLog(String.format("Exception:import user.userName=[%s];"+e, user.getUserName());
                     continue;
                  }
                     
          }
      }
      catch(Exception e)
      {
         log.error("Exception: import user's Exception", e);
      }


2. csv文件上传

跳过第一条的数据


public SimpleReader(Reader reader, char separator, char quotechar, char escape, int line,
         boolean strictQuotes, boolean ignoreLeadingWhiteSpace)
   {
      this.br = new BufferedReader(reader);
      this.parser = new CSVParser(separator, quotechar, escape, strictQuotes, ignoreLeadingWhiteSpace);
      this.skipLines = line;
   }


 try
      {
         SimpleReader reader = new SimpleReader(new InputStreamReader(inp, Charset.forName("gbk")), ',', '\'', 1);
         int userCount = 0; 
         for(String[] line : reader.readAll())
         {
            user = new UserDAO();
            try
            {
               user.setUserName(line[0].trim());
               user.setEmail(line[1].trim());
               user.setCellphone(line[2].trim());
               user.setTelephone(line[3].trim());
             }
             catch(Exception e)
             {
                appendImportExcelLog(String.format("Exception:import user.userName=[%s];"+e, user.getUserName()));
                continue;
             }

      }
      catch(Exception e)
      {
         log.error("Exception: import user's Exception", e);
      }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值