Android sql 数据库的导出到excel中

从数据库中导出数据

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * Created by Administrator on 2016/8/1.
 */
public class DatabaseDump {

    private SQLiteDatabase mDb;


    public DatabaseDump(SQLiteDatabase db) {
        mDb = db;

    }

    public void exportData() {

        try {

            //  Log.i("mdb", mDb.getPath());
            // get the tables out of the given sqlite database
            String sql = "SELECT * FROM sqlite_master";

            Cursor cur = mDb.rawQuery(sql, new String[0]);
            cur.moveToFirst();

            String tableName;
            while (cur.getPosition() < cur.getCount()) {
                tableName = cur.getString(cur.getColumnIndex("name"));

                // don't process these two tables since they are used
                // for metadata
                if (!tableName.equals("android_metadata")
                        && !tableName.equals("sqlite_sequence")) {
                    writeExcel(tableName);
                }
                cur.moveToNext();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

            /*    要生成的Excel文件名*/

    public void writeExcel(String tableName) {
        WritableWorkbook wwb = null;
        String fileName;
        fileName = Environment.getExternalStorageDirectory().getPath().toString()+File.separator +
                tableName + ".xls";

        File isexist = new File(fileName);
        if (!isexist.exists())
        {
            try {
                isexist.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        int r = 0;
        Log.e("datadump","writeexcel");
        String sql = "select * from " + tableName;
        Cursor cur = mDb.rawQuery(sql, new String[0]);
        int numcols = cur.getColumnCount();
        int numrows = cur.getCount();
        // Log.i("row", numrows + "");
        // Log.i("col", numcols + "");

        String records[][] = new String[numrows + 1][numcols];// 存放答案,多一行标题行
        if (cur.moveToFirst()) {
            while (cur.getPosition() < cur.getCount()) {
                for (int c = 0; c < numcols; c++) {
                    if (r == 0) {
                        records[r][c] = cur.getColumnName(c);
                        records[r + 1][c] = cur.getString(c);
                    } else {
                        records[r + 1][c] = cur.getString(c);
                    }
                    //  Log.i("value" + r + " " + c, records[r][c]);
                }
                cur.moveToNext();
                r++;
            }

            cur.close();
        }
        try {
            // 首先要使用Workbook类的工厂方法创建一个可写入的工作薄(Workbook)对象
            wwb = Workbook.createWorkbook(new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (wwb != null) {
            // 创建一个可写入的工作表
            // WorkbookcreateSheet方法有两个参数,第一个是工作表的名称,第二个是工作表在工作薄中的位置
            WritableSheet ws = wwb.createSheet("Result", 0);    // 下面开始添加单元格
            for (int i = 0; i < numrows + 1; i++) {
                for (int j = 0; j < numcols; j++) {
                    // 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
                    Label labelC = new Label(j, i, records[i][j]);
            //        Log.i("Newvalue" + i + " " + j, records[i][j]);
                    try {
                        // 将生成的单元格添加到工作表中
                        ws.addCell(labelC);
                    } catch (RowsExceededException e) {
                        e.printStackTrace();
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }

                }
            }
            try {
                // 从内存中写入文件中
                wwb.write();
                // 关闭资源,释放内存
                wwb.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值