java poi读取excel文件

来源:互联网 发布:全职太太 知乎 编辑:程序博客网 时间:2024/04/30 11:52
需要导入poi-3.15.jarimport 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 java.io.File;import java.io.FileInputStream;import java.util.ArrayList;import java.util.List;/**批量读取文件夹的excel文件 * Created by Administrator on 2017/6/12. * */public class LoadExcelUtil {    /**     * @param strPath     * @return List<dataAll>     * @throws Exception     */    public static List<dataAll> testReadExcel(String strPath) throws Exception{        List<dataAll> list = new ArrayList<dataAll>();        dataAll data = null;        File file = new File(strPath);//读取文件夹        if (file.exists()) {            File[] files = file.listFiles();            if (files.length == 0) {                System.out.println("文件夹是空的!");            }else {                for (File file2 : files) {                    System.out.println("读取文件 :" + file2.getAbsolutePath());                //创建输入流                FileInputStream fis = new FileInputStream(new File(file2.getAbsolutePath()));                //通过构造函数传参                HSSFWorkbook workbook = new HSSFWorkbook(fis);                //                HSSFSheet sheet = workbook.getSheetAt(0);                //第一行0开始计算//        HSSFRow row = sheet.getRow(0);                for (int k = 1; k < sheet.getPhysicalNumberOfRows(); k++) {                    data = new dataAll();                    HSSFRow row = sheet.getRow(k);                    //读取数据                    HSSFCell cell0 = row.getCell(0);                    data.setName(cell0.getStringCellValue());                    HSSFCell cell1 = row.getCell(1);                    data.setStatus(cell1.getStringCellValue());                    HSSFCell cell2 = row.getCell(2);                    data.setPerson(cell2.getStringCellValue());                    HSSFCell cell3 = row.getCell(3);                    data.setAddress(cell3.getStringCellValue());                    HSSFCell cell4 = row.getCell(4);                    data.setRegister_type(cell4.getStringCellValue());                    HSSFCell cell5 = row.getCell(5);                    data.setDate(cell5.getStringCellValue());                    HSSFCell cell6 = row.getCell(6);                    data.setDepart(cell6.getStringCellValue());                    HSSFCell cell7 = row.getCell(7);                    data.setDepart_code(cell7.getStringCellValue());                    HSSFCell cell8 = row.getCell(8);                    data.setCode(cell8.getStringCellValue());                    HSSFCell cell9 = row.getCell(9);                    data.setCreate_time(cell9.getStringCellValue());                    HSSFCell cell10 = row.getCell(10);                    data.setUpdate_time(cell10.getStringCellValue());                    HSSFCell cell11 = row.getCell(11);                    data.setRN(cell11.getStringCellValue());                    list.add(data);                }                workbook.close();                fis.close();                }            }        }        return list;    }}比较详细的blog http://blog.csdn.net/huazhangena/article/details/7587731