安卓实现导入Excel文件

使用简化版的jar包

复制代码
api files('libs/poi-3.12-android-a.jar')
api files('libs/poi-ooxml-schemas-3.12-a.jar')

导入遇到了两个兼容问题

1.build.gradle文件里面

复制代码
android {

要添加

复制代码
packagingOptions {
    exclude 'META-INF/INDEX.LIST'
}

2.加载大文件要在清单文件里面加android:largeHeap="true"和配置文件的解除大小限制

导入成功后,后面实现就简单了

第一步选择设备内的excel格式文件

可以指定文件类型跳转也可以用户选择后判断文件格式

第二步根据选择返回的Uri获取文件真实位置路径

第三步

解析文件

复制代码
/**
 * 读取excel   (xls和xlsx)
 * 从Excel报表中读出数据
 */
public static void readExcel(File file) {
    String filePath = file.getAbsolutePath();
    Sheet sheet = null;
    List<Map<String, String>> list = null;
    Workbook wb = null;
    if (filePath == null) {
        return;
    }
    String extString = filePath.substring(filePath.lastIndexOf("."));
    InputStream is = null;
    try {
        is = new FileInputStream(filePath);
        if (".xls".equals(extString)) {
            wb = new HSSFWorkbook(is);
        } else if (".xlsx".equals(extString)) {
            wb = new XSSFWorkbook(is);
        } else {
            wb = null;
        }
        if (wb != null) {
            // 用来存放表中数据
            // 获取第一个sheet
            sheet = wb.getSheetAt(0);
            if (sheet == null) {
                return;
            }
            //获得当前sheet的开始行
            int firstRowNum = sheet.getFirstRowNum();
            //获得当前sheet的结束行
            int lastRowNum = sheet.getLastRowNum();
            //循环除了第一行的所有行
            for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
                //获得当前行
                Row row = sheet.getRow(rowNum);
                if (row == null) {
                    continue;
                }
                //获得当前行的开始列
                int lastCellNum = row.getLastCellNum();
                //获得当前行的列数
                String[] cells = new String[lastCellNum];

                Log.d(TAG, "readDataFromExcel: " + row.getCell(0).toString());
                Log.d(TAG, "readDataFromExcel: " + (String) getCellFormatValue(row.getCell(0)));
            }
            BaseDialog.hiddenWaitingDialog();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * 获取单个单元格数据
 *
 * @param cell
 * @return
 */
private static Object getCellFormatValue(Cell cell) {
    Object cellValue;
    if (cell != null) {
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                cellValue = cell.getBooleanCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellValue = cell.getDateCellValue();
                } else {
                    cellValue = cell.getNumericCellValue();
                }
                break;
            case Cell.CELL_TYPE_STRING:
                cellValue = cell.getStringCellValue();
                break;
            default:
                cellValue = "";
        }
    } else {
        cellValue = "";
    }
    return cellValue;
}

后续操作看需求了

jar包下载地址:https://github.com/binZai-ComeOn/ReadXlsx?tab=readme-ov-file

相关推荐
流星白龙2 小时前
【MySQL】7.MySQL基本查询(2)
android·mysql·adb
mldlds2 小时前
MySQL加减间隔时间函数DATE_ADD和DATE_SUB的详解
android·数据库·mysql
智算菩萨4 小时前
MP3音频编码原理深度解析与Python全参数调优实战:从心理声学模型到LAME编码器精细控制
android·python·音视频
studyForMokey6 小时前
【Android面试】Activity生命周期专题
android·面试·职场和发展
chehaoman6 小时前
MySQL的索引
android·数据库·mysql
开开心心就好8 小时前
免费自媒体多功能工具箱,图片音视频处理
人工智能·pdf·ocr·excel·音视频·语音识别·媒体
恋猫de小郭9 小时前
React Native 鸿蒙 2026 路线发布,为什么它的适配成本那么高?
android·前端·react native
studyForMokey10 小时前
【Android面试】窗口机制专题
android·面试·职场和发展
用户0132014360311 小时前
Android 资源管理与常用布局详解|基础入门
android
陆业聪11 小时前
从 OpenClaw 到 Android:Harness Engineering 是怎么让 Agent 变得可用的
android·人工智能·ai编程