Android Excel(xls,xlsx)表格数据简单生成和读取尝试

1.我们数据一般常用的有SharedPreferences以键值对数据形式保存到本地xml里面,对于一些大量字段的数据,就不能直观看到数据了。

                -- 我们也可以根据自己的需求,使用适合自己的版本的表格

2.添加jxl.jar包,导入到自己项目

链接:https://pan.baidu.com/s/1QSIwXDHVrfasW9pWbHzAaQ 
提取码:051l 
复制这段内容后打开百度网盘手机App,操作更方便哦

3.自己定义数据类:

package com.example.test_01.excel;

public class Userinfo {
    private int id;
    private String name;
    private String sex;
    private String phone;
    private String email;

    public Userinfo() {
    }

    public Userinfo(int id, String name, String sex, String phone, String email) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.phone = phone;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

4.把数据导进去excel表格:

 private static final String TAG = "ExcelActivity";
 private String excel_save;
 private List<Userinfo> list;
       excel_save = getApplication().getExternalCacheDir() + File.separator + "TestExcel";
       //新增三个用户信息
       list=new ArrayList<>();
       list.add(new Userinfo(1,"小明","男","18123456789","jk3232@qq.com"));
       list.add(new Userinfo(2,"小花","女","18123456789","jk5656@qq.com"));
       list.add(new Userinfo(3,"小南","男","18123456789","jk9898@qq.com"));
       Log.i(TAG, "onCreate: list="+list.toString());

调用init_SaveExcel(),把数据保存 

/**
     * 增加Excel保存数据
     */
    private void init_SaveExcel() {

        // 准备设置excel工作表的标题
        String[] title = {"id", "name", "sex", "phone", "email"};

        File BuildDir = new File(getApplication().getExternalCacheDir(), "TestExcel");   //打开UHFData目录,如不存在则生成
        if (BuildDir.exists() == false) BuildDir.mkdirs();

        // 创建Excel工作薄
        WritableWorkbook wwb;
        // 在SD卡中,新建立一个jxl文件,并写入数据
        try {
            wwb = Workbook.createWorkbook(new File(excel_save + File.separator + "userinfo.xls"));
            Log.i(TAG, "init_veinSaveExcel: 创建文件成功!");
            // 添加第一个工作表并设置第一个Sheet的名字
            WritableSheet sheet = wwb.createSheet("UserData", 0);
            Label label;

            for (int i = 0; i < title.length; i++) {
                label = new Label(i, 0, title[i]);
                // 将定义好的单元格添加到工作表中
                try {
                    sheet.addCell(label);
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
            //查找测试的数据
            if (list.isEmpty()) {
                Log.i(TAG, "init_veinExcel: ls_user 数据为null");
            } else {
                Log.i(TAG, "init_veinExcel: ls_user 数据不为null" + list.toString());

                for (int i = 0; i < list.size(); i++) {
                    /*
                     * 保存数字到单元格,需要使用jxl.write.Number 必须使用其完整路径,否则会出现错误
                     */
                    //添加用户id
                    label = new Label(0, i + 1, String.valueOf(list.get(i).getId()));
                    sheet.addCell(label);
                    //添加用户名
                    label = new Label(1, i + 1, list.get(i).getName());
                    sheet.addCell(label);
                    //添加性别
                    label = new Label(2, i + 1, list.get(i).getSex());
                    sheet.addCell(label);
                    //添加电话
                    label = new Label(3, i + 1, list.get(i).getPhone());
                    sheet.addCell(label);
                    //添加邮箱
                    label = new Label(4, i + 1, list.get(i).getEmail());
                    sheet.addCell(label);
                }
            }
            wwb.write(); //写入数据
            wwb.close(); //关闭文件


        } catch (IOException e) {
            e.printStackTrace();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    }

 最终生成表格保存

我这边没有软件打不开,直接把文件复制到电脑打开

打开excel表

5.读取excel数据:

    private void init_ReadExcel() {
        String id = null, name = null, sex = null, phone = null, email = null;
        Workbook book = null;
        try {
            book = Workbook.getWorkbook(new File(excel_save + File.separator + "userinfo.xls"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }

        //获得第一个工作表对象
        Sheet sheet = book.getSheet("UserData");

        int rows = sheet.getRows();
        int cols = sheet.getColumns();

        System.out.println("总列数:" + cols);
        System.out.println("总行数:" + rows);
        System.out.println("----------------------------");

        //循环读取数据
        for (int j = 1; j < rows; j++) {

            for (int i = 0; i < cols; i++) {
                System.out.println("第" + j + "行,第" + i + "列为:" + sheet.getCell(i, j).getContents());
                //如果用户名和用户组好相同,则不插入,否则就插入
                switch (i) {
                    case 0:
                        id = sheet.getCell(i, j).getContents();
                        break;
                    case 1:
                        name = sheet.getCell(i, j).getContents();
                        break;
                    case 2:
                        sex = sheet.getCell(i, j).getContents();
                        break;
                    case 3:
                        phone = sheet.getCell(i, j).getContents();
                        break;
                    case 4:
                        email = sheet.getCell(i, j).getContents();
                        break;
                    default:
                        Log.i(TAG, "init_veinReadExcel: 未知参数!");
                        break;
                }
            }

            Log.i(TAG, "Excel:第" + j + "条信息={" + "id=" + id + ",name=" + name + ",sex=" + sex + ",phone=" + phone + ",email=" + email + "}");

        }
    }

看输出结果

 到这里基本是创建数据表格和读取指定表格的数据,就实现了,就可以把这些数据单独分离出来,结合自己的需求来实现自己的功能啦!如果文章有问题,可以评论留言,修改不足和交流学习!

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值