2 回答

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
對(duì)于其他正在尋找類似問題答案的人,根據(jù) Dheeraj 的代碼和另一項(xiàng)細(xì)微更改,最終的工作代碼將是 -
public void exportExcelSheet() throws IOException {
DatabaseHandler mDbHandler = new DatabaseHandler(mActivity);
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "Report.csv");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String[] ColumnNames = {"Roll No.", "Name", "LA", "LT", "% age"};
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
csvWrite.writeNext(ColumnNames);
String studentInfoQuery = "SELECT * FROM StudentList";
Cursor studentsListCursor = mDbHandler.execQuery(studentInfoQuery);
studentsListCursor.moveToFirst();
do {
int studentRoll = studentsListCursor.getPosition() + 1;
String AttendanceListQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentRoll + "';";
String AttendanceQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentRoll + "' AND isPresent = 1";
int attendancePercent = 0;
Cursor totalClasses = mDbHandler.execQuery(AttendanceListQuery);
Cursor attendedClasses = mDbHandler.execQuery(AttendanceQuery);
if (totalClasses == null) {
Log.d("profile", "totalClasses null");
}
if (attendedClasses == null) {
Log.d("profile", "attendedClasses null");
}
if (totalClasses != null && attendedClasses != null) {
totalClasses.moveToFirst();
attendedClasses.moveToFirst();
try {
attendancePercent = (int) (((float) attendedClasses.getCount() / totalClasses.getCount()) * 100);
} catch (Exception e) {
attendancePercent = -1;
}
}
assert attendedClasses != null;
assert totalClasses != null;
String showAttendedLectures = String.valueOf(attendedClasses.getCount());
String showTotalLectures = String.valueOf(totalClasses.getCount());
//String showMissedLectures = String.valueOf(totalClasses.getCount() - attendedClasses.getCount());
String AttendancePercentage = String.valueOf(attendancePercent);
try {
String[] arrStr = {studentsListCursor.getString(1), studentsListCursor.getString(0), showAttendedLectures, showTotalLectures, AttendancePercentage + " %"};
csvWrite.writeNext(arrStr);
} catch (Exception sqlException) {
Toast.makeText(mActivity, "FAILED", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", sqlException.getMessage(), sqlException);
}
Toast.makeText(mActivity, "Saved", Toast.LENGTH_SHORT).show();
}
while (studentsListCursor.moveToNext());
csvWrite.close();
}

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
這段代碼中有一個(gè)小錯(cuò)誤,即每次執(zhí)行 do-while 循環(huán)時(shí)都會(huì)創(chuàng)建 csvWriter 對(duì)象,因此最后一個(gè)輸出 CSV 文件僅從光標(biāo)中獲取了最后一行。這應(yīng)該可以解決問題:
public void exportExcelSheet() {
DatabaseHandler mDbHandler = new DatabaseHandler(mActivity);
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "Report.csv");
// ============== CHANGE ==============
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
String[] ColumnNames = {"Roll No.", "Name", "LA", "LT", "% age"};
// ============== CHANGE ==============
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
csvWrite.writeNext(ColumnNames);
String studentInfoQuery = "SELECT * FROM StudentList";
Cursor studentsListCursor = mDbHandler.execQuery(studentInfoQuery);
studentsListCursor.moveToFirst();
// ============== CHANGE ==============
do {
String AttendanceListQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentsListCursor.getPosition() + "';";
String AttendanceQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentsListCursor.getPosition() + "' AND isPresent = 1";
int attendancePercent = 0;
Cursor totalClasses = mDbHandler.execQuery(AttendanceListQuery);
Cursor attendedClasses = mDbHandler.execQuery(AttendanceQuery);
if (totalClasses == null) {
Log.d("profile", "totalClasses null");
}
if (attendedClasses == null) {
Log.d("profile", "attendedClasses null");
}
if (totalClasses != null && attendedClasses != null) {
totalClasses.moveToFirst();
attendedClasses.moveToFirst();
try {
attendancePercent = (int) (((float) attendedClasses.getCount() / totalClasses.getCount()) * 100);
} catch (Exception e) {
attendancePercent = -1;
}
}
assert attendedClasses != null;
assert totalClasses != null;
String showAttendedLectures = String.valueOf(attendedClasses.getCount());
String showTotalLectures = String.valueOf(totalClasses.getCount());
//String showMissedLectures = String.valueOf(totalClasses.getCount() - attendedClasses.getCount());
String AttendancePercentage = String.valueOf(attendancePercent);
try {
String[] arrStr = {studentsListCursor.getString(1), studentsListCursor.getString(0), showAttendedLectures, showTotalLectures, AttendancePercentage + " %"};
csvWrite.writeNext(arrStr);
// ============== CHANGE ==============
// studentsListCursor.moveToNext();
} catch (Exception sqlException) {
Toast.makeText(mActivity, "FAILED", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", sqlException.getMessage(), sqlException);
}
Toast.makeText(mActivity, "Saved", Toast.LENGTH_SHORT).show();
}
// ============== CHANGE ==============
while (studentsListCursor.moveToNext());
csvWrite.close();
}
添加回答
舉報(bào)