美文网首页
Android 使用 Apache POI 读取 Excel.m

Android 使用 Apache POI 读取 Excel.m

作者: Obadiah | 来源:发表于2019-06-05 14:32 被阅读0次

    依赖这个库

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

    excel 的模板


    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<User>()
                box.put(user)
            }
        }
        toast("读取完成")
    
    }
    

    相关文章

      网友评论

          本文标题:Android 使用 Apache POI 读取 Excel.m

          本文链接:https://www.haomeiwen.com/subject/eizjxctx.html