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) throwsIOException {
LinkmanService linkmanService=newLinkmanService();
XlsMain xlsMain = newXlsMain();
Linkman linkman =null;
List list =xlsMain.readXls("G:\\studyUtile\\Tomcat6.0.18\\webapps\\CMCC1.2\\zmtupian\\2013-7\\朋友.xls");
for (Linkman man : list){
System.out.println(man);
linkmanService.addMan(man);
}
}
//测试类 G:\studyUtile\Tomcat6.0.18\webapps\CMCC1.2\zmtupian\2013-7\朋友.xls传过来的路径
//非测试类 G:\studyUtile\Tomcat6.0.18\webapps\CMCC1.2\zmtupian\2013-7\朋友.xls传过来的路径
public List readXls(Stringpath) throws IOException {
System.out.println(path+"传过来的路径");
InputStream is = newFileInputStream(path);
HSSFWorkbook hssfWorkbook = newHSSFWorkbook(is);
Linkman linkman=null;
List list = newArrayList();
// 循环工作表Sheet
for (int numSheet = 0; numSheet< hssfWorkbook.getNumberOfSheets();numSheet++){
HSSFSheethssfSheet =hssfWorkbook.getSheetAt(numSheet);
if(hssfSheet == null) {
continue;
}
//循环行Row
System.out.println("总共的记录行数为:"+hssfSheet.getLastRowNum());
for (introwNum = 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;