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());
}
}
}