here is my code for generating backup for my database i using apache poi for that purpose but it show some errors when i execute.
i start with HSSF worksheet and create a sheet and add details from my database and save that file in the specific loaction on sd card.
HSSFWorkbook backup=new HSSFWorkbook();
HSSFSheet sheet=backup.createSheet("Contact_Backup");
Map<String, Object[]> data = new HashMap<String, Object[]>();
sq = md.getReadableDatabase();
Cursor mCursor = sq.rawQuery("SELECT * FROM "
+ MyDatabase.TABLE_NAME, null);
if (mCursor.moveToFirst()) {
do {
idbackup.add(mCursor.getString(mCursor.getColumnIndex(MyDatabase.KEY_ID)));
namebackup.add(mCursor.getString(mCursor.getColumnIndex(MyDatabase.KEY_NAME)));
numberbackup.add(mCursor.getString(mCursor.getColumnIndex(MyDatabase.KEY_NUM)));
addressbackup.add(mCursor.getString(mCursor.getColumnIndex(MyDatabase.KEY_ADDR)));
mailbackup.add(mCursor.getString(mCursor.getColumnIndex(MyDatabase.KEY_MAIL)));
} while (mCursor.moveToNext());
}
if (idbackup.size() != 0) {
int j = 2;
for (int i = 0; i < idbackup.size(); i++) {
data.put(String.valueOf(j), new Object[]{idbackup.get(i), namebackup.get(i), numberbackup.get(i), addressbackup.get(i), mailbackup.get(i)});
j++;
}
}
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof Date)
cell.setCellValue((Date) obj);
else if (obj instanceof Boolean)
cell.setCellValue((Boolean) obj);
else if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Double)
cell.setCellValue((Double) obj);
}
}
try {
File directory = new File("/sdcard/BulkMessenger/backups"); /*creating the file directory
directory.mkdir();
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM MM dd, yyyy h:mm a");
String dateString = sdf.format(date);
File file = new File(directory, dateString+"backupcontact.xls");
FileOutputStream out = new FileOutputStream(file);
backup.write(out);
out.close();
Toast.makeText(getApplicationContext(),"Successfully backup generated in backup",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
my error log is given below in it filenotfound exception that what i got.
07-08 09:52:38.250 15615-15615/com.example.web03.bulkmessenger E/dalvikvm: Could not find class 'java.awt.font.FontRenderContext', referenced from method org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn
thanks for help in advance!
its related to my file directory thats exception is raised!! thanks for advice guys
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With