poi 读取Excel

package cn.poi.dao.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingThreshold;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.omg.CORBA.portable.OutputStream;

import cn.poi.bean.User;
import cn.poi.dao.ApiDao;

public class ApiDaoImpl implements ApiDao {
 POIFSFileSystem fs = null;
 HSSFWorkbook wb = null;
 User user = null;

 @Override
 public List<User> readExcel() {
  List<User> list = new ArrayList<User>();
  try {
   fs = new POIFSFileSystem(new FileInputStream("D:\\excel\\coach.xls"));// 导入文件
   wb = new HSSFWorkbook(fs);// 创建excel工作簿
   // 循环输出工作表
   for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
    HSSFSheet hs = wb.getSheetAt(numSheet);
    if (hs == null) {
     continue;
    }
    // 循环行
    for (int row = 1; row <= hs.getLastRowNum(); row++) {
     HSSFRow hr = hs.getRow(row);
     if (hr == null) {
      continue;
     }
     user = new User();
     // 循环列Cell
     // 0姓名 1身份证号 2性别 3所在单位 4培训地点省
     //5培训地点市6主办单位7承办单位8协办单位9教练员级别10培训时间
     //11发证时间12证书编号13 教龄 14段级位 15学历
     HSSFCell cell = hr.getCell(0);
     if (cell == null) {
      continue;
     }
     user.setName(getValue(cell));;
   /*  HSSFCell cell1 = hr.getCell(1);
     if (cell1 == null) {
      continue;
     }
     System.out.println(getValue(cell1));
     user.setId(Integer.valueOf(getValue(cell1)));*/
     HSSFCell cell2 = hr.getCell(2);
     if (cell2 == null) {
      continue;
     }
     user.setSex(getValue(cell2));
     HSSFCell cell3 = hr.getCell(3);
     if (cell3 == null) {
      continue;
     }
     user.setUnit(getValue(cell3));
     HSSFCell cell4 = hr.getCell(4);
     if (cell4 == null) {
      continue;
     }
     user.setPxProvince(getValue(cell4));
     HSSFCell cell5 = hr.getCell(5);
     if (cell5 == null) {
      continue;
     }
     user.setPxCity(getValue(cell5));
     HSSFCell cell6 = hr.getCell(6);
     if (cell6 == null) {
      continue;
     }
     user.setHostCompany(getValue(cell6));
     HSSFCell cell7 = hr.getCell(7);
     if (cell7 == null) {
      continue;
     }
     user.setHoldCompany(getValue(cell7));
     HSSFCell cell8 = hr.getCell(8);
     if (cell8 == null) {
      continue;
     }
     user.setAssistantCompany(getValue(cell8));
     HSSFCell cell9 = hr.getCell(9);
     if (cell9 == null) {
      continue;
     }
     user.setCoachLevel(getValue(cell9));
     HSSFCell cell10 = hr.getCell(10);
     if (cell10 == null) {
      continue;
     }
/*     user.setPxTime((Date)(new SimpleDateFormat(getValue(cell10))));*/
     list.add(user);
     HSSFCell cell11 = hr.getCell(11);
     if (cell11 == null) {
      continue;
     }
//     user.setPxCity(getValue(cell11));
     HSSFCell cell12 = hr.getCell(12);
     if (cell12 == null) {
      continue;
     }
     System.out.println(getValue(cell12));
/*     user.setZsNo(Integer.valueOf(getValue(cell12)));*/
     HSSFCell cell13 = hr.getCell(13);
     if (cell13 == null) {
      continue;
     }
//     user.setAge(Integer.valueOf(getValue(cell13)));
     HSSFCell cell14 = hr.getCell(14);
     if (cell14 == null) {
      continue;
     }
     user.setSkillLevel(getValue(cell14));
     HSSFCell cell15 = hr.getCell(15);
     if (cell15 == null) {
      continue;
     }
     user.setXl(getValue(cell15));
 
    }
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return list;
 }

 // 获取单元格的值
 private 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((int) hssfCell.getNumericCellValue());
  } else {
   // 返回字符串
   return String.valueOf(hssfCell.getStringCellValue());
  }
 }

 @Override
 public void writeExcel(List<User> list) {
  // 获得总列数
  int size = list.size();
  wb = new HSSFWorkbook();
  // sheet对应一个列
  HSSFSheet sheet = wb.createSheet("id");
  HSSFRow firstRow = sheet.createRow(0);
  HSSFCell[] fistcell = new HSSFCell[size];
  String[] names = new String[size];
  names[0] = "编号";
  names[1] = "姓名";
  names[2] = "性别";
  names[3] = "年龄";
  for (int j = 0; j < size; j++) {
   fistcell[j] = firstRow.createCell(j);
   fistcell[j].setCellValue(new HSSFRichTextString(names[j]));
  }
  for (int i = 0; i < list.size(); i++) {
   // 创建一行
   HSSFRow row = sheet.createRow(i + 1);
   list.get(i);// 得到要插入的数据
   for (int z = 0; z <= 4; z++) {
    // 添加元素
    HSSFCell cell = row.createCell(0);
    cell.setCellValue(user.getId());
    HSSFCell cell1 = row.createCell(1);
    cell1.setCellValue(user.getName());
    HSSFCell cell2 = row.createCell(2);
    cell2.setCellValue(user.getSex());
    HSSFCell cell3 = row.createCell(3);
    cell3.setCellValue(user.getAge());
   }
  }
  try {
   FileOutputStream out=new FileOutputStream("D:\\excel\\id.xls");
   wb.write(out);
   out.close();
   System.out.println("数据导出成功");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

posted @ 2016-10-18 11:06  你说我是你的超级英雄  阅读(243)  评论(0编辑  收藏  举报