• Java读取excel文件


    1.poi方式读取解析

    excel文件:

    package com.mi.util;
    
    import java.io.FileInputStream;
    import java.io.InputStream;
    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.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    
    import com.mi.entity.Student;
    
    public class POIReadExcelTool {
    
        public static void main(String[] args) throws Exception {
            List<Student> list = readXls();
            for(Student stu : list){
                System.out.println(stu.getId());
                System.out.println(stu.getName());
                System.out.println(stu.getAge());
                System.out.println(stu.getBirth());
            }
        }
    
        public static List<Student> readXls() throws Exception {
            InputStream is = new FileInputStream("D:/student.xls");
    
            HSSFWorkbook excel = new HSSFWorkbook(is);
            Student stu = null;
            List<Student> list = new ArrayList<Student>();
            
            // 循环工作表Sheet
            for (int numSheet = 0; numSheet < excel.getNumberOfSheets(); numSheet++) {
                HSSFSheet sheet = excel.getSheetAt(numSheet);
                if (sheet == null)
                    continue;
                // 循环行Row
                for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
                    HSSFRow row = sheet.getRow(rowNum);
                    if (row == null)
                        continue;
                    stu = new Student();
                    // 循环列Cell
                    // 0学号 1姓名 2年龄 3生日
                    /*System.out.println((int)cell0.getNumericCellValue());
                    System.out.println(cell1.getStringCellValue());
                    System.out.println((int)cell2.getNumericCellValue());
                    System.out.println(cell3.getStringCellValue());*/
                    /**console:
                     *         1
                            张三
                            16
                            1997-03-12
                            2
                            李四
                            17
                            1996-08-12
                     */
                    HSSFCell cell0 = row.getCell(0);
                    if (cell0 == null)
                        continue;
                    stu.setId((int)cell0.getNumericCellValue());
                    HSSFCell cell1 = row.getCell(1);
                    if (cell1 == null)
                        continue;
                    stu.setName(cell1.getStringCellValue());
                    HSSFCell cell2 = row.getCell(2);
                    if (cell2 == null)
                        continue;
                    stu.setAge((int)cell2.getNumericCellValue());
                    HSSFCell cell3 = row.getCell(3);
                    if (cell3 == null)
                        continue;
                    stu.setBirth(new SimpleDateFormat("yyyy-mm-dd").parse(cell3.getStringCellValue()));
                    list.add(stu);
                }
            }
    
            return list;
        }
    
        @SuppressWarnings("unused")
        private static String getValue(HSSFCell cell) {
            if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                // 返回布尔类型 值
                return String.valueOf(cell.getBooleanCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                //返回数值类型的值
                return String.valueOf(cell.getNumericCellValue());
            } else {
                //返回字符串类型的值
                return cell.getStringCellValue();
            }
        }
    }

    结果:

    1
    张三
    16
    Sun Jan 12 00:03:00 CST 1997
    2
    李四
    17
    Fri Jan 12 00:08:00 CST 1996

  • 相关阅读:
    ConnectionUtils
    设置组件内容模板和头部模板
    设置idea 代码模板
    Win10 安装流程 Maven
    IDEA: Error:java: 无效的源发行版: 9
    eclipse js的自动提示
    SQLserver SQL语句自动格式化工具的调出
    java计算两个n阶矩阵相乘
    JSP页面输出数据库表格
    threadpool 的配置实用
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/5913798.html
Copyright © 2020-2023  润新知