android excel 筛选功能,Android实现Excel表格展示数据
前言
在Android開發過程中,我們偶爾會需要將一些數據以Excel表格展示。那么今天就讓我們來學習下Excel表格展示相關內容吧
今天涉及的內容:
準備數據model
1.1 ColumnTitle
1.2 RowTitle
1.3 Cell
準備表格布局
2.1 頂部標題欄布局
2.2 左側豎行標題欄布局
2.3 表頭布局
2.4 表格內容布局
其他屬性準備
3.1 布局寬高屬性
3.2 布局背景
效果圖及項目結構圖
庫依賴
在Activity中使用示例
適配器ExcelAdapter代碼
先來波效果圖
1.gif
一. 準備數據model
Excel表格展示涉及到三組數據:
ColumnTitle : 列數據集合(頂部標題欄)
RowTitle: 行數據集合(左側豎列標題欄)
Cell:表內容數據集合
下面以頂部標題欄顯示課程,左側標題豎列顯示第幾節課,然后表內容顯示每節課的時間為例進行講解。
1.1 ColumnTitle
課程數據實體類ColumnTitle代碼如下:
/**
* Title: Excel 列bean
*
* description:
* autor:pei
* created on 2020/10/29
*/
data class ColumnTitle(
var course:String? //課程
)
1.2 RowTitle
第幾節課數據實體類RowTitle代碼如下:
/**
* Title: Excel 行 bean
* description:
* autor:pei
* created on 2020/10/29
*/
data class RowTitle(
var lesson: String? //課節,如:第一節課,第二節課...
)
1.3 Cell
顯示時間的數據實體類Cell代碼如下:
/**
* Title: Excel 表中數據 bean
* description:
* autor:pei
* created on 2020/10/29
*/
data class Cell(
var time:String? //時間
){}
二.準備表格布局
表格的布局主要涉及到四個layout: 頂部標題欄布局, 左側豎行標題欄布局,左上角表頭布局 和 表格中內容布局
下面依次給出各布局代碼:
2.1 頂部標題欄布局
頂部標題欄布局adapter_excel_item_top_cell.xml代碼如下:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/excel_width"
android:layout_height="@dimen/excel_top_cell_height"
android:background="@drawable/excel_title_bg">
android:id="@+id/mTvTopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/black"/>
2.2 左側豎行標題欄布局
左側豎行標題欄布局adapter_excel_item_left_cell.xml代碼如下:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/excel_left_cell_width"
android:layout_height="@dimen/excel_height"
android:background="@drawable/excel_title_bg">
android:id="@+id/mTvLeftName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@color/black"/>
2.3 表頭布局
左上角表頭布局adapter_excel_item_top_left_cell.xml代碼如下:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/excel_left_cell_width"
android:layout_height="@dimen/excel_top_cell_height"
android:background="@drawable/excel_title_bg">
android:id="@+id/mTvLeftTopName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"
android:text="課節\\課程"/>
2.4 表格內容布局
最后是表格內容布局adapter_excel_item_cell.xml代碼:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="@dimen/excel_width"
android:layout_height="@dimen/excel_height"
android:background="@drawable/excel_cell_bg">
android:id="@+id/mTvCellName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
android:textSize="16sp"
android:textColor="@color/black"/>
三.其他屬性準備
3.1 布局寬高屬性
在以上四個布局中會涉及到幾個寬高屬性值。我們需要在res\values\dimen.xml中添加以下屬性值:
100dp
80dp
40dp
40dp
具體數值大小的話,大家可以根據實際情況設置,此處只做示例展示。
3.2 布局背景
上面幾個布局中會涉及到背景的問題。我寫的示例中主要涉及兩個背景xml文件:excel_cell_bg.xml和excel_title_bg.xml。
下面貼出excel_cell_bg.xml代碼:
android:width="1dp"/>
excel_title_bg.xml代碼如下:
android:width="1dp"/>
四. 效果圖及項目結構圖
效果圖.gif
項目結構圖.png
五. 庫依賴
Excel表格展示的實現我們使用的是ExcelPanel庫,ExcelPanel庫官網地址為:ExcelPanel。那么我們需要在app_model的buid.gradle中添加相關依賴:
總結
以上是生活随笔為你收集整理的android excel 筛选功能,Android实现Excel表格展示数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android怎么用别人的工程,Andr
- 下一篇: android动态监听事件,Androi