如何通过 Sqlite 数据库对名为 Amount 的列求和?我想对我输入的金额求和,同时插入然后在创建的 Excel 工作表中显示总金额。请提供帮助。提前致谢。
public void saveExcelFile() {
try {
dbHelper = new DBHelper(MainActivity.this);
final Cursor cursor = dbHelper.getuser(selectedDate);
//create directory if not exist
if (!directory.isDirectory()) {
directory.mkdirs();
}
File file = new File(directory, edtDate.getText().toString() + ".xls");
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook;
workbook = Workbook.createWorkbook(file, wbSettings);
//Excel sheet name. 0 represents first sheet
WritableSheet sheet = workbook.createSheet("userList", 0);
// column and row
sheet.addCell(new Label(0, 0, "id"));
sheet.addCell(new Label(1, 0, "name"));
sheet.addCell(new Label(2, 0, "email"));
sheet.addCell(new Label(3, 0, "date"));
sheet.addCell(new Label(4, 0, "amount"));
if (cursor.moveToFirst()) {
do {
String id = cursor.getString(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String email = cursor.getString(cursor.getColumnIndex("email"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String amount = cursor.getString(cursor.getColumnIndex("amount"));
int i = cursor.getPosition() + 1;
sheet.addCell(new Label(0, i, id));
sheet.addCell(new Label(1, i, name));
sheet.addCell(new Label(2, i, email));
sheet.addCell(new Label(3, i, date));
sheet.addCell(new Label(4, i, amount));
} while (cursor.moveToNext());
}
//closing cursor
cursor.close();
workbook.write();
workbook.close();
Toast.makeText(getApplication(), "Data Exported in a Excel Sheet", Toast.LENGTH_SHORT).show();
} catch (WriteException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
最佳答案
做..
- 添加这个 jar 文件 opencsv-1.7.jar http://www.java2s.com/Code/Jar/o/Downloadopencsv17jar.htm
- 然后使用这段代码
公共(public)类 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);
}
}
<强>3。要获取列的总和,请执行以下操作。
Cursor cursor = db.rawQuery("SELECT SUM(" + DbHelper.CART_TOTAL + ") as Total FROM " + DbHelper.CART_TABLE, null);
if (cur.moveToFirst()) {
int total = cursor.getInt(cursor.getColumnIndex("Total"));// get final total
关于android - 如何求和Sqlite数据库的垂直列,然后将其导出到Excel Sheet Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47194707/