Android program to convert the SQLite database to excel

前端 未结 4 1647
逝去的感伤
逝去的感伤 2020-12-03 02:07

I want to change the sqlite database .db file to excel.

But I am not able to find what exactly I have to do. Can anybody please elabor

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 02:46

    Export Android SqliteDb to CSV format

    You need to do these step...

    1. add this jar file opencsv-1.7.jar http://www.java2s.com/Code/Jar/o/Downloadopencsv17jar.htm

    2. And then use this code

           
    public class ExportDatabaseToCSV{
        
        Context context;
        public ExportDatabaseToCSV(Context context) {
            this.context=context;
        }
        
        
        public void exportDataBaseIntoCSV(){
            
          
           CredentialDb db = new CredentialDb(context);//here CredentialDb is my database. you can create your db object.
           File exportDir = new File(Environment.getExternalStorageDirectory(), "");        
              
           if (!exportDir.exists()) 
              {
                  exportDir.mkdirs();
              }
    
          File file = new File(exportDir, "csvfilename.csv");
          
          try 
          {
              file.createNewFile();                
              CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
              SQLiteDatabase sql_db = db.getReadableDatabase();//here create a method ,and return SQLiteDatabaseObject.getReadableDatabase();
              Cursor curCSV = sql_db.rawQuery("SELECT * FROM "+CredentialDb.TABLE_NAME,null);
              csvWrite.writeNext(curCSV.getColumnNames());
             
              while(curCSV.moveToNext())
                  {
                     //Which column you want to export you can add over here...
                      String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)};
                      csvWrite.writeNext(arrStr);
                  }
    
              csvWrite.close();
              curCSV.close();
          }
          catch(Exception sqlEx)
          {
              Log.e("Error:", sqlEx.getMessage(), sqlEx);
          }
        }   
    }
    

提交回复
热议问题