POI实现Excl文件的导入

来源:互联网 发布:不干胶排版打印软件 编辑:程序博客网 时间:2024/04/28 21:32
package com.onepiece.utils;import java.io.FileInputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import com.onepiece.domain.manager.Informations;public class Inputone {    public static void main(String[] args) {        import_excel("d:/信息.xls");    }    //针对2007版本的xls    public static List<Informations> import_excel(String exlname){        List<Informations> infos=new ArrayList<Informations>();        String path=exlname;//获取文件的路径        try {            InputStream is = new FileInputStream(path);            XSSFWorkbook  hssfWorkbook = new XSSFWorkbook (is);            // 循环工作表Sheet            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {                XSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);                if (hssfSheet == null) {                    continue;                }                // 循环行Row                for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {                    XSSFRow hssfRow = hssfSheet.getRow(rowNum);                    Map<Integer,String> map=new HashMap<Integer,String>();                    if (hssfRow == null) {                        continue;                    }                    for (int cellNum = 0; cellNum <=hssfRow.getPhysicalNumberOfCells(); cellNum++) {                        XSSFCell xh = hssfRow.getCell(cellNum);                        if (xh == null) {                             map.put(cellNum,"");                            continue;                        }                        map.put(cellNum, getValue(xh));//将值存入map中                    }                    //将map数据注入到对象中                    Informations info=maptoInfo(map);                    infos.add(info);                }                System.out.println(infos.toString());            }        }catch (Exception e) {            e.printStackTrace();        }        return  infos;    }    public static Informations maptoInfo(Map<Integer,String> map){        Informations info=new Informations();        info.setAgnlicense(map.get(0));        info.setAgnaddr(map.get(1));        info.setAgnname(map.get(2));        info.setLeaglname(map.get(3));        info.setLegaltdentity(map.get(4));        info.setLegtel(map.get(5));        info.setLegphone(map.get(6));        info.setTrainscope(map.get(7));        //分教点        info.setBranchid(map.get(8));        info.setBranchname(map.get(9));        info.setPricinpal(map.get(10));        info.setTel(map.get(11));        info.setPhone(map.get(12));        info.setBtrainscope(map.get(13));        //教师        info.setTeachername(map.get(14));        info.setTeacherid(map.get(15));        info.setGraddshchool(map.get(16));        info.setMajor(map.get(17));        info.setHightstacad(map.get(18));        info.setTeachmajor(map.get(19));        info.setCertification(map.get(20));        info.setProfesstitle(map.get(21));        info.setDuty(map.get(22));        info.setTeachersex(map.get(23));        info.setResume(map.get(24));        return info;    }    /**     * 得到Excel表中的值     *      * @param xh     *            Excel中的每一个格子     * @return Excel中每一个格子中的值     */    @SuppressWarnings("static-access")    private static String getValue(XSSFCell xh) {        if (xh.getCellType() == xh.CELL_TYPE_BOOLEAN) {            // 返回布尔类型的值            return String.valueOf(xh.getBooleanCellValue());        } else if (xh.getCellType() == xh.CELL_TYPE_NUMERIC) {            // 返回数值类型的值            return String.valueOf(xh.getNumericCellValue());        } else {            // 返回字符串类型的值            return String.valueOf(xh.getStringCellValue());        }    }}
0 0
原创粉丝点击