Java获取Excel表格数据

本文介绍了一种从Excel文件中读取并解析数据的方法,包括数值、文本和时间格式的处理,同时提供了将时间字符串转换为Timestamp格式的实用代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传入excel文件对象,数据格式可以包括数值,文本,时间 

返回list集合(一行数据是一个list)

public static List<List<String[]>> getExcelData(File file) {
        HSSFWorkbook workbook;
        List<List<String[]>> dataList = new ArrayList<List<String[]>>();
        try {
            workbook = new HSSFWorkbook(new FileInputStream(file));
            HSSFSheet sheet;
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                sheet = workbook.getSheetAt(i);
                List<String[]> rows = new ArrayList<String[]>();
                int colsnum = 0;
                for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                    HSSFRow row = sheet.getRow(j);
                    if (row != null) {
                        colsnum = sheet.getRow(1).getPhysicalNumberOfCells();
                        String[] cols = new String[colsnum];
                        for (int k = 0; k < colsnum; k++) {
                            Cell cell = row.getCell(k);
                            if (cell != null) {
                                switch (cell.getCellTypeEnum()) {
                                    case NUMERIC:
                                        if (DateUtil.isCellDateFormatted(cell)) {
                                            Date date = cell.getDateCellValue();
                                            DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
                                            cols[k] = formater.format(date);
                                        } else if (String.valueOf(cell.getNumericCellValue()).contains(".")) {
                                            DecimalFormat df = new DecimalFormat("#");
                                            cols[k] = df.format(cell.getNumericCellValue());
                                        } else {
                                            cols[k] = (cell + "").trim();
                                        }
                                        continue;
                                        default:
                                            cols[k] = (cell + "").trim();
                                            continue;
                                }
                            } else {
                                cols[k] = "";
                            }
                        }
                        rows.add(cols);
                    }
                }
                dataList.add(rows);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }

 传入时间字符串,返回Timetamp格式时间

 
public static Timestamp toDate(String basicDate){
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Timestamp ts = null;
    try {
        ts = new Timestamp(dateFormat.parse(basicDate).getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return ts;
}

参考:https://blog.youkuaiyun.com/qx30339/article/details/54020308

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值