Excel 导入

package util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import service.LinkmanService;
import dao.Linkman;
public class XlsMain {
 
 public static void main(String[] args) throws IOException  {
   LinkmanService  linkmanService=new LinkmanService();
   XlsMain xlsMain = new XlsMain();
   Linkman linkman = null; 
   List list = xlsMain.readXls("G:\\studyUtile\\Tomcat 6.0.18\\webapps\\CMCC1.2\\zmtupian\\2013-7\\朋友.xls");
   for (Linkman man : list) {
   
    System.out.println(man);
    linkmanService.addMan(man);
  }

 }
 //测试类 G:\studyUtile\Tomcat 6.0.18\webapps\CMCC1.2\zmtupian\2013-7\朋友.xls传过来的路径
 //非测试类 G:\studyUtile\Tomcat 6.0.18\webapps\CMCC1.2\zmtupian\2013-7\朋友.xls传过来的路径
 public  List readXls(String path) throws IOException {
  
  System.out.println(path+"传过来的路径");
  InputStream is = new FileInputStream(path); 
  HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
  Linkman linkman=null;
  List list = new ArrayList();
  // 循环工作表Sheet
  for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
   HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); 
    if (hssfSheet == null) {
     continue;
    
   // 循环行Row 
    System.out.println("总共的记录行数为:"+hssfSheet.getLastRowNum());
    for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++){
     HSSFRow hssfRow = hssfSheet.getRow(rowNum);
     if (hssfRow == null) {
      continue;  
     
     linkman=new Linkman();
     HSSFCell username = hssfRow.getCell(0); //得到一行第一列的值
     if (username == null) {   
      continue; 
     
     linkman.setUsername(getValue(username));
    
     HSSFCell email = hssfRow.getCell(2);
     if (email == null) {   
      continue; 
     
     linkman.setEmail(getValue(email));
    
     HSSFCell modelphone = hssfRow.getCell(3);
     if (modelphone == null) {   
      continue; 
     
     linkman.setModelphone(getValue(modelphone));
    
     HSSFCell adress = hssfRow.getCell(4);
     if (adress == null) {   
      continue; 
     
     linkman.setAdress(getValue(adress));
    
     HSSFCell workunit = hssfRow.getCell(5);
     if (workunit == null) {   
      continue; 
     
     linkman.setWorkunit(getValue(workunit));
    
     HSSFCell telephone = hssfRow.getCell(6);
     if (telephone == null) {   
      continue; 
     
     linkman.setTelephone(getValue(telephone));
    
     HSSFCell homephone = hssfRow.getCell(7);
     if (homephone == null) {   
      continue; 
     
     linkman.setHomephone(getValue(homephone));
    
     HSSFCell department = hssfRow.getCell(8);
     if (department == null) {   
      continue; 
     
     linkman.setDepartment(getValue(department));
    
     HSSFCell post = hssfRow.getCell(9);
     if (post == null) {   
      continue; 
     
     linkman.setPost(getValue(post));
    
     HSSFCell faxnumber = hssfRow.getCell(10);
     if (faxnumber == null) {   
      continue; 
     
     linkman.setFaxnumber(getValue(faxnumber));
    
     HSSFCell reemail = hssfRow.getCell(11);
     if (reemail == null) {   
      continue; 
     
     linkman.setReemail(getValue(reemail));
    
     HSSFCell postcode = hssfRow.getCell(12);
     if (postcode == null) {   
      continue; 
     
     String postd=getValue(postcode);
     int n=postd.indexOf(".");
     String sp= postd.substring(0, n);
     linkman.setPostcode(Integer.parseInt(sp));
    
     HSSFCell linkdesc = hssfRow.getCell(13);
     if (linkdesc == null) {   
      continue; 
     
     linkman.setLinkdesc(getValue(linkdesc));
     list.add(linkman);
    }
   
  }
  
  return list;
  
 }
    public String getValue(HSSFCell hssfCell) {
     if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
      // 返回布尔类型的值     
      return String.valueOf(hssfCell.getBooleanCellValue());
      } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
       // 返回数值类型的值    
       return String.valueOf(hssfCell.getNumericCellValue());
       } else { 
        // 返回字符串类型的值      
        return String.valueOf(hssfCell.getStringCellValue()); 
         
     
     
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值