-1

如何在 android 模拟器中使用 CSV 将 sqlite 数据库数据导出到 excel 表中?

这可能与否?

请给出一些想法,我是android平台的新手

4

2 回答 2

1

来自Sqlite 文档

将结果写入文件 默认情况下,sqlite3 将查询结果发送到标准输出。您可以使用“.output”命令更改此设置。只需将输出文件的名称作为 .output 命令的参数,所有后续查询结果都将写入该文件。使用“.output stdout”再次开始写入标准输出。例如:

sqlite> .mode list
sqlite> .separator |
sqlite> .output test_file_1.txt
sqlite> select * from tbl1;
sqlite> .exit
$ cat test_file_1.txt
hello|10
goodbye|20
$

如果您要替换 '|' 中的分隔符 到“,”它可能只是工作。当然,您应该学习如何sqlite3从 shell 中使用来查看表格内容。这要容易得多。

于 2013-01-22T17:05:33.027 回答
0

我最近在我的应用程序中实现了 excel 导出功能。我还包含了有关如何将过滤后的数据导出到 excel 而不是整个表的完整代码。

您需要为此创建第二个表。第二个将保存此操作所需的数据(在我的第二个表中,我删除了我的自动增量 ID 列,因为我不想在我的 excel 文件中使用它)。

您需要先清除第二个表,然后添加条目。

然后使用 SqLiteToExcel 对象将 db 导出到 excel 并将文件保存在某处。

然后我有一个电子邮件意图,其中附有用于共享的 excel 文件(允许与电子邮件以外的其他应用程序共享)。这是我的方法:

    private void ExportData() {

     //CHECK IF YOU HAVE WRITE PERMISSIONS OR RETURN
    int permission = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(getContext(), "Storage permissions not granted", Toast.LENGTH_SHORT).show();
        return;
    }

    //get database object
    myDbhelper = new MyDbHelper(getContext());
    SQLiteDatabase database = myDbhelper.getWritableDatabase();

    //delete all entries in the second table
    database.delete("Table2",null,null);

    //Create a cursor of the main database with your filters and sort order applied
    Cursor cursor = getActivity().getContentResolver().query(
            uri,
            projections,
            selection,
            args,
            sortOrder);

    //loop through cursor and add entries from first table to second table
    try {
        while (cursor.moveToNext()) {
            final String ColumnOneIndex = cursor.getString(cursor.getColumnIndexOrThrow("COLUMN_ONE"));
            final String ColumnTwoIndex = cursor.getString(cursor.getColumnIndexOrThrow("COLUMN_TWO"));
            final String ColumnThreeIndex = cursor.getString(cursor.getColumnIndexOrThrow("COLUMN_THREE"));

      //add entries from table one into the table two
            ContentValues values = new ContentValues();
            values.put("TABLE2_COLUMN_1", ColumnOneIndex);
            values.put("TABLE2_COLUMN_2", ColumnTwoIndex );
            values.put("TABLE2_COLUMN_3", ColumnThreeIndex);

            database.insert("table2", null, values);
        }
    } finally {
        //close cursor after looping is complete
        cursor.close();
    }

     //create a string for where you want to save the excel file
    final String savePath = Environment.getExternalStorageDirectory() + "/excelfileTemp";
    File file = new File(savePath);
    if (!file.exists()) {
        file.mkdirs();
    }
     //create the sqLiteToExcel object
    SQLiteToExcel sqLiteToExcel = new SQLiteToExcel(getContext(), "databasefile.db",savePath);

    //use sqLiteToExcel object to create the excel file 
    sqLiteToExcel.exportSingleTable("table2","excelfilename.xls", new SQLiteToExcel.ExportListener() {
        @Override
        public void onStart() {

        }
        @Override
        public void onCompleted(String filePath) {
            //now attach the excel file created and be directed to email activity
            Uri newPath = Uri.parse("file://" + savePath + "/" +"excelfilename.xls");

            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());

            Intent emailintent = new Intent(Intent.ACTION_SEND);

            emailintent.setType("application/vnd.ms-excel");
            emailintent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            emailintent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");
            emailintent.putExtra(Intent.EXTRA_STREAM,newPath);

            startActivity(Intent.createChooser(emailintent, "Send Email"));
        }
        @Override
        public void onError(Exception e) {
            System.out.println("Error msg: " + e);
            Toast.makeText(getContext(), "Failed to Export data", Toast.LENGTH_SHORT).show();
        }
    });
}

我在我的应用程序中实现了这个方法并且它有效

于 2018-07-08T04:38:37.203 回答