Android excel表操作

今天学习下Android中将数据保存到 excel 表中,本文是在下面文献基础上进行的复现:
https://blog.csdn.net/linzhenxiang123/article/details/53730439

一、MainActivity 代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText name = null;
    private EditText sex = null;
    private EditText phone = null;
    private EditText address = null;
    private Button button = null;
    private ExcelUtil excelUtil = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = (EditText)findViewById(R.id.name);
        sex = (EditText)findViewById(R.id.sex);
        phone = (EditText)findViewById(R.id.phone);
        address = (EditText)findViewById(R.id.address);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);

        excelUtil = new ExcelUtil(MainActivity.this,"/sdcard/test.xls");
    }

    @Override
    public void onClick(View v) {
        String name1 = name.getText().toString();
        String sex1 = sex.getText().toString();
        String phone1 = phone.getText().toString();
        String address1 = address.getText().toString();
        excelUtil.writeToExcel(name1,sex1,phone1,address1);
    }
}

二、 Excel 操作
下面是 excel 操作的主要代码

import android.app.Activity;
import android.widget.Toast;

import java.io.File;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelUtil {

        private WritableWorkbook wwb;
        private File excelFile;
        private Activity activity;

        public ExcelUtil(Activity activity, String excelPath) {
            this.activity = activity;
            excelFile = new File(excelPath);
            createExcel(excelFile);
        }

        public void createExcel(File file) {
            WritableSheet ws = null;
            try {
                if (!file.exists()) {
                    wwb = Workbook.createWorkbook(file);
                    ws = wwb.createSheet("sheet1", 0);

                    Label lbl1 = new Label(0, 0, "姓名");
                    Label lbl2 = new Label(1, 0, "性别");
                    Label lbl3 = new Label(2, 0, "电话");
                    Label lbl4 = new Label(3, 0, "地址");

                    ws.addCell(lbl1);
                    ws.addCell(lbl2);
                    ws.addCell(lbl3);
                    ws.addCell(lbl4);

                    wwb.write();
                    wwb.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void writeToExcel(Object... args) {

            try {
                Workbook oldWwb = Workbook.getWorkbook(excelFile);
                wwb = Workbook.createWorkbook(excelFile, oldWwb);
                WritableSheet ws = wwb.getSheet(0);

                int row = ws.getRows();
                Label lab1 = new Label(0, row, args[0] + "");
                Label lab2 = new Label(1, row, args[1] + "");
                Label lab3 = new Label(2, row, args[2] + "");
                Label lab4 = new Label(3, row, args[3] + "");
                ws.addCell(lab1);
                ws.addCell(lab2);
                ws.addCell(lab3);
                ws.addCell(lab4);

                wwb.write();
                wwb.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

三、 jar包
CSDN 上有免积分的 jxl jar 可下载

四、问题
1.容易出现问题 bug :the input file was not found ;可以尝试 直接在桌面上创建 xls文件,并导入到手机准备放置的路径下(即不通过代码创建xls文件);

2.发现将 txt 文件按照一定的个数保存,然后txt 可以直接导入到 excel,感觉效果是一样的;如果没有非常必须的需求(感觉这个直接保存excel问题还是太多),可以保存先为txt (桌面上用excel 重新加载下就可以了,可以查看 txt导入excel)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值