Android 使用 Apache POI 读取 Excel.md

依赖这个库

implementation "com.github.SUPERCILEX.poi-android:poi:3.17"

excel 的模板


Android 使用 Apache POI 读取 Excel.md_第1张图片
Image 21.png

读取数据后用 ObjectBox 储存到数据库

private fun readUserInfoExcel(path: String) {
    var sheet1 = getSheet1(path)
    sheet1.forEachIndexed { index, row ->
        if (index == 0) {
            return@forEachIndexed
        }
        var name = ""
        var roomNumber = ""
        var cardNumber = ""
        row.forEachIndexed { index, cell ->
            cell.setCellType(CellType.STRING)
            var value = cell.stringCellValue
            when (index) {
                0 -> {
                    name = value
                }
                1 -> {
                    roomNumber = value
                }
                2 -> {
                    cardNumber = value
                }
            }
        }
        var user = User(name = name, roomNumber = roomNumber, cardNumber = cardNumber)
        ObjectBox.get()?.let { store ->
            var box = store.boxFor()
            box.put(user)
        }
    }
    toast("读取完成")

}

你可能感兴趣的:(Android 使用 Apache POI 读取 Excel.md)