android - how to store data from sqlite to google excel sheet -
i new in android. made 3 columns in sqlite , storing user input in sqlite
i want when device wifi(internet) upload data google excel sheet accordingly column on specific user account.
my solution convert sqlite database csv in first step in second step convert csv file xls , works fine me, need 2 libraries (opencsv-1.7.jar; poi-3.8-20120326.jar)
public class exportdatabasecsvtask extends asynctask<string, void, boolean> { private final progressdialog dialog = new progressdialog(databaseexampleactivity.this); @override protected void onpreexecute() { this.dialog.setmessage("exporting database..."); this.dialog.show(); } protected boolean doinbackground(final string... args) { file dbfile=getdatabasepath("database_name"); //aabdatabasemanager dbhelper = new aabdatabasemanager(getapplicationcontext()); aabdatabasemanager dbhelper = new aabdatabasemanager(databaseexampleactivity.this) ; system.out.println(dbfile); // displays data base path in logcat file exportdir = new file(environment.getexternalstoragedirectory(), ""); if (!exportdir.exists()) { exportdir.mkdirs(); } file file = new file(exportdir, "excerdb.csv"); try { if (file.createnewfile()){ system.out.println("file created!"); system.out.println("myfile.csv "+file.getabsolutepath()); }else{ system.out.println("file exists."); } csvwriter csvwrite = new csvwriter(new filewriter(file)); //sqlitedatabase db = dbhelper.getwritabledatabase(); cursor curcsv=db.getdb().rawquery("select * " + db.table_name,null); csvwrite.writenext(curcsv.getcolumnnames()); while(curcsv.movetonext()) { string arrstr[] ={curcsv.getstring(0),curcsv.getstring(1),curcsv.getstring(2)}; /*curcsv.getstring(3),curcsv.getstring(4)};*/ csvwrite.writenext(arrstr); } csvwrite.close(); curcsv.close(); /*string data=""; data=readsaveddata(); data= data.replace(",", ";"); writedata(data);*/ return true; } catch(sqlexception sqlex) { log.e("mainactivity", sqlex.getmessage(), sqlex); return false; } catch (ioexception e) { log.e("mainactivity", e.getmessage(), e); return false; } } protected void onpostexecute(final boolean success) { if (this.dialog.isshowing()) { this.dialog.dismiss(); } if (success) { toast.maketext(databaseexampleactivity.this, "export succeed", toast.length_short).show(); } else { toast.maketext(databaseexampleactivity.this, "export failed", toast.length_short).show(); } }}
export csv xls part
public class csvtoexcelconverter extends asynctask<string, void, boolean> { private final progressdialog dialog = new progressdialog(databaseexampleactivity.this); @override protected void onpreexecute() {this.dialog.setmessage("exporting excel..."); this.dialog.show();} @override protected boolean doinbackground(string... params) { arraylist arlist=null; arraylist al=null; //file dbfile= new file(getdatabasepath("database_name").tostring()); file dbfile=getdatabasepath("database_name"); string yes= dbfile.getabsolutepath(); string infilepath = environment.getexternalstoragedirectory().tostring()+"/excerdb.csv"; outfilepath = environment.getexternalstoragedirectory().tostring()+"/test.xls"; string thisline; int count=0; try { fileinputstream fis = new fileinputstream(infilepath); datainputstream myinput = new datainputstream(fis); int i=0; arlist = new arraylist(); while ((thisline = myinput.readline()) != null) { al = new arraylist(); string strar[] = thisline.split(","); for(int j=0;j<strar.length;j++) { al.add(strar[j]); } arlist.add(al); system.out.println(); i++; }} catch (exception e) { system.out.println("shit"); } try { hssfworkbook hwb = new hssfworkbook(); hssfsheet sheet = hwb.createsheet("new sheet"); for(int k=0;k<arlist.size();k++) { arraylist ardata = (arraylist)arlist.get(k); hssfrow row = sheet.createrow((short) 0+k); for(int p=0;p<ardata.size();p++) { hssfcell cell = row.createcell((short) p); string data = ardata.get(p).tostring(); if(data.startswith("=")){ cell.setcelltype(cell.cell_type_string); data=data.replaceall("\"", ""); data=data.replaceall("=", ""); cell.setcellvalue(data); }else if(data.startswith("\"")){ data=data.replaceall("\"", ""); cell.setcelltype(cell.cell_type_string); cell.setcellvalue(data); }else{ data=data.replaceall("\"", ""); cell.setcelltype(cell.cell_type_numeric); cell.setcellvalue(data); } //*/ // cell.setcellvalue(ardata.get(p).tostring()); } system.out.println(); } fileoutputstream fileout = new fileoutputstream(outfilepath); hwb.write(fileout); fileout.close(); system.out.println("your excel file has been generated"); } catch ( exception ex ) { ex.printstacktrace(); } //main method ends return true; } protected void onpostexecute(final boolean success) { if (this.dialog.isshowing()) { this.dialog.dismiss(); } if (success) { toast.maketext(databaseexampleactivity.this, "file built!", toast.length_long).show(); } else { toast.maketext(databaseexampleactivity.this, "file fail build", toast.length_short).show(); } } }
Comments
Post a Comment