Android开发: 将Sqlite数据库导入Excel表格当中

来源:互联网 发布:华星全站仪传输软件 编辑:程序博客网 时间:2024/06/11 04:54

如题,
如果需要用excel来操作sql中的数据,
那么久必要先将sql中的数据导出
网上大多都是excel数据导入sql来使用和一些基础的操作,
今天就来实现,将sql中的数据导入
先需要导入第三方工具,jxl.jar;Androidstudio可以自己去找,下载,eclipse需要下载手动导入到lib下面(本人还用的es,抱歉了)
jxl.jar资源
http://download.csdn.net/detail/fengdeweilai/9890053
搞清楚基本事项后,接下来上码

创建一个工具类ExcelUtils

private Context context;//上下文对象    private SQLiteDatabase sdb = SQLiteDatabase.openOrCreateDatabase(            "/sdcard/xxx.db", null);//打开sdb管理独享    private WritableWorkbook book;//写入excel管理对象public SQliteDataUtils(Context context) {//构造方法    this.context = context;    }

context用于去显示完成的toast提示

在一个Sql中有多张表格的时候,每一个type对应一张表,也对应这一个excel文件

public void ExportExcel(int type) {        try {            String filePath = "/sdcard/";            String s = "";            switch (type) {//用来区分多张表,每张表都来对应一个excel            case 1:                s = filePath + "1.xls";                break;            case 2:                s = filePath + "2.xls";                break;            case 3:                s = filePath + "3.xls";                break;            }            File file = new File(s);//获取或者创建excel表                String name = tableName(type);//用变量名代替sql表名(用type来对应),这个就不拿出来了                Cursor c = sdb.query(name, null, null, null, null, null, null);//获取真个表数据的cursor对象                SqliteExportExcel(type, name, file, c);//导出excel表        } catch (Exception e) {            e.printStackTrace();        }    }
/**     * @param type-对应的表     * @param typeName-excel中工作表名称     * @param file-对应的excel文件     * @param c-数据库装载调用对象     * @throws IOException     * @throws JXLException     * @throws WriteException     */    public void SqliteExportExcel(int type, String Name, File file, Cursor c)            throws IOException, JXLException, WriteException {        this.landType = type;        // 打开文件        book = Workbook.createWorkbook(file);        WritableSheet sheet = book.createSheet(Name, 0);//在excel文件中创建表单,那么为表单名称        String[] ziDuan = c.getColumnNames();//获取所有字段的数据集合        int row = 0;//excel的行数从0开始,为字段头        for (int i = 0; i < ziDuan.length; i++) {// 写入字段头            Label label = new Label(i, row, ziDuan[i]);            sheet.addCell(label);        }        // 便利写入数据        while (c.moveToNext()) {//遍历Cursor            row++;//对应excel中的行数,没写一次递增            for (int i = 0; i < ziDuan.length; i++) {//i对应到excel中的每一列                //String s =convertZiDuan(c, type, ziDuan[i], i,  c.getString(i));//用于去查找连锁相关的数据                Label label = new Label(i, row, s);//创建数据对象                sheet.addCell(label);//添加写入事件            }        }        book.write();//开始执行写入        book.close();//关闭资源    }

索引链表字段,这里仅供参考

private String[] zds = "xiangzhen,dcdy,jzlx,jzsj,nhlx".split(",");    /**     *      * @param c-Cursor对象     * @param type-表类型     * @param zd-字段名称,这里一开始对应附表的表名     * @param i-字段在Cursor的当前栏下标     * @param s-需要过滤或者说转换的字符     * @return 过滤处理后的字段值,准备放入excel中的值     */     //字符以自己测试,开发的sql表对应就好,这里只提供逻辑,以免误区    private String convertZiDuan(Cursor c, int type, String zd, int i, String s) {        for (int j = 0; j < zds.length; j++) {            if (zds[j].equals(zd)) {                String zdType = c.getString(c.getColumnIndex(zd));                Cursor cc = sdb.query(zd, null, "type=?",                        new String[] { zdType }, null, null, null);                cc.moveToFirst();            int cur=cc.getColumnIndex("name");                s = cc.getString(cur);                cc.close();            }        }        return s;    }

主要由于SqliteExportExcel写入数据,到此,我已经实现了数据的转化!你呢?

原创粉丝点击