linux083 2014-01-14 01:09 采纳率: 0%
浏览 1260

sqliteDB里面的数据如何用Excel表格的形式显示在手机上?

你好!我想问问怎样才可以把sqliteDB里面的内容显示在手机 上?我用的ListView 显示的!为什么达不到像Excel表格一样的形式了??代码如下:
package com.example.com.ht.sqlitelistview;

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

import android.os.Bundle;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {
SQLiteDatabase ht_read_db;
public void initialize () {
sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "ht_write.db", null, 1);
// sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "F:\users.db", null, 1);

        final SQLiteDatabase ht_write_db = ht_dbhelper.getWritableDatabase();
    Button btn_read,btn_write,btn_create_table;
    Button btn_insert,btn_update,btn_query,btn_del;

    btn_read = (Button) findViewById(R.id.button_read);
    btn_write = (Button) findViewById(R.id.button_write);
    btn_create_table = (Button) findViewById(R.id.create_table);

    btn_insert = (Button) findViewById(R.id.btn_insert);
    btn_update = (Button) findViewById(R.id.btn_updata);
    btn_query = (Button) findViewById(R.id.btn_querey);
    btn_del = (Button) findViewById(R.id.btn_del);
    btn_read.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println("创建一个只读华泰—数据库");
            sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "ht_read.db", null, 1);
            ht_read_db = ht_dbhelper.getReadableDatabase();



        }
    });
    btn_write.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println("创建一个可读可写的的华泰数据库");
            sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "ht_write2.db", null, 1);
        //  sqliteopenhelper ht_dbhelper = new sqliteopenhelper(MainActivity.this, "F:\\users.db", null, 1);

            SQLiteDatabase ht_write_db2 = ht_dbhelper.getWritableDatabase();


        }
    });
    btn_create_table.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            System.out.println("create table user_info");
            String createstr = "create table user_info("+"_id int,"+"name char(20),"+"age int)";
            ht_write_db.execSQL(createstr);
            System.out.println("create table ht_user_information");
            String createstr2 = "create table ht_user_information("+"user_id int,"+"user_name char(20),"+"user_phone int,"+"user_address char,"+"user_age int,"+"user_password int)";
            ht_write_db.execSQL(createstr2);
        }
    });
    btn_insert.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            System.out.println("insert table str1");
            String insertstr = "insert into user_info(_id,name,age)"+"values(?,?,?)";
            Object[] valuesObjestc = {1,"seven",22};
            ht_write_db.execSQL(insertstr, valuesObjestc);

            System.out.println("insert table str2");
            String inserstr2 = "insert into user_info(_id,name)"+"values(2,'Jim')";
            ht_write_db.execSQL(inserstr2);
            System.out.println("insert table str3");
            String inserstr3 = "insert into user_info(_id,name,age)"+"values(3,'Jdf',24)";
            ht_write_db.execSQL(inserstr3);

            System.out.println("insert table str2");
            String inserstr4 = "insert into user_info(_id,name,age)"+"values(4,'Jff',10)";
            ht_write_db.execSQL(inserstr4);
            System.out.println("insert table str5");
            String inserstr5 = "insert into user_info(_id,name,age)"+"values(5,'Jgg',563)";
            ht_write_db.execSQL(inserstr5);

            ContentValues valzw = new ContentValues();
            valzw.put("_id", "45");
            valzw.put("name", "zzzzz");
            valzw.put("age", 100);
            //ht_write_db.insert("user_info", "_id", valzw);//android insert
            ht_write_db.insert("user_info", "name", valzw);


        }
    });
    btn_update.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            System.out.println("更新table");
            ContentValues values = new ContentValues();
            values.put("name", "zw");
            ht_write_db.update("user_info", values, "_id=?", new String[]{"1"});
            ContentValues value_age = new ContentValues();
            value_age.put("age",100);
            value_age.put("name", "two");
            value_age.put("age", 555);

            ht_write_db.update("user_info", value_age, "_id=?", new String[]{"2"});
            ContentValues value_id = new ContentValues();
            value_id.put("name", "周希");
            ht_write_db.update("user_info", value_id, "_id=?", new String[]{"5"});

        }
    });
    btn_query.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View arg0) {
            System.out.println("query table!");

            Cursor cursor = ht_write_db.query("user_info", new String[]{"_id", "name","age" },null, null, null,null,"name");





            //cursor.move(1);//向下

            cursor.moveToFirst();
            //cursor.moveToLast();
            //cursor.move(-1);//向上
            //cursor.moveToNext();
            //cursor.moveToPosition(1);
        //  cursor.moveToPrevious();//上一行!


            String str_cursor = cursor.getString(cursor.getColumnIndexOrThrow("age"));
            System.out.println("cursor.getColumIndex is:" +cursor.getColumnIndex("age"));
            System.out.println("str_cursor is:" + str_cursor);
            //cursor.close();//此处不能关,否则listview显示不出内容来.

            SimpleCursorAdapter sCursorAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.user, cursor, new String[]{"_id","name","age"}, new int[]{R.id.editText1,R.id.editText2,R.id.editText3});
            ListView listview = (ListView)findViewById(R.id.listview);//取listView 对像!

            listview.setAdapter(sCursorAdapter);

        }
    });
    btn_del.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            System.out.println("del table !");
            ht_write_db.delete("user_info", "age=?", new String[] {"100"});
            String del_db = "drop database ht_write";
            //ht_write_db.execSQL(del_db);
            String del_table = "drop table user_info";
            ht_write_db.execSQL(del_table);
            System.out.println("已经删除table info");
            String del_table2 = "drop table ht_user_information";
            ht_write_db.execSQL(del_table2);
            System.out.println("已经删除table ht_user_information");

        }
    });


}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
    this.initialize();
    }finally{


    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
class sqliteopenhelper extends SQLiteOpenHelper{

    public sqliteopenhelper(Context sqliteopenhelper, String name,
            CursorFactory factory, int version) {
        super(sqliteopenhelper, name, factory, version);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("华泰—数据库创建");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("华泰-数据库更新");

    }

}

}

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-01 17:16
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    要将SQLite数据库中的数据以Excel表格的形式展示到手机上,您可以使用Java库,例如Apache POI(Apache POI是开源的、免费的第三方组件),它提供了处理Excel文件所需的API。以下是一个基本示例,展示了如何从SQLite数据库中获取数据并将其导出为Excel。

    首先,确保您的项目中有以下依赖项: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.0.0</version> </dependency> 然后,您可以在MainActivity中添加以下代码:

    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends Activity {
    
        private static final String DATABASE_NAME = "example.db";
        private static final String TABLE_NAME = "myTable";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Initialize SQLite DB and get writable database
            SQLiteDatabase ht_read_db = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
            SQLiteDatabase ht_write_db = openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
    
            // Create a simple cursor adapter for ListView
            ListView listView = findViewById(R.id.listView);
            SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                    this,
                    R.layout.row_layout,
                    ht_read_db.query(TABLE_NAME, new String[]{"_id", "column1", "column2"}, null, null, null),
                    new String[]{"_id", "column1", "column2"},
                    0);
    
            // Set up ListView with your custom cursor adapter
            listView.setAdapter(simpleCursorAdapter);
    
            // Save the list view as an Excel file
            saveAsExcel(listView);
        }
    
        /**
         * Saves the list view as an Excel file.
         *
         * @param listView The list view that contains the data to be saved.
         */
        private void saveAsExcel(ListView listView) {
            Workbook workbook = new XSSFWorkbook();
    
            Sheet sheet = workbook.createSheet("Example Sheet");
            Row row = sheet.createRow(0); // Header row
    
            // Write column headers
            CellStyle style = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setBold(true);
            style.setFont(font);
            row.createCell(0).setCellValue("ID");
            row.createCell(1).setCellValue("Column1");
            row.createCell(2).setCellValue("Column2");
    
            // Write data from the list view to each cell in the worksheet
            int i = 1;
            for (int j = 0; j < listView.getCount(); j++) {
                Cell cell = row.createCell(i++);
                cell.setCellValue((String) listView.getItemAt(j));
            }
    
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream("example.xlsx");
                workbook.write(outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    此代码将在Android设备上创建一个名为“Example Sheet”的Excel文件,并在其中包含列表视图的列标题和所有行数据。根据您的需求,您可以自定义这些列标题和数据格式。

    评论

报告相同问题?