我已经研究了如何触发电子邮件并用数据库查询中的信息填充它,但我想将其写入CSV文件,以便我可以使用excel进行格式化(可能附加到电子邮件中)邮件,如果这是可能的话)。
下面是我的代码,它创建要发送到电子邮件的文本:
private void exportRedRiskItemsToEmail(){
recipient = email;
subject.append(inspectionRef).append(" - Locators Rack Inspection Report (Red Risk Items)");
message.append("Dear ").append(contactFirstName).append(",\r\n");
message.append("\r\n");
message.append("Following todays inspection, below is a list of locations which are noted as Red Risk:\r\n");
final Cursor areasCursor = (Cursor) rmDbHelper.fetchAllAreasForInspection(inspectionId);
if(areasCursor.moveToFirst()){
do{
areaId = areasCursor.getLong(areasCursor.getColumnIndex(RMDbAdapter.AREA_ID));
areaNumber = RMUtilities.notEmpty(areasCursor.getString(areasCursor.getColumnIndex(RMDbAdapter.AREA_NUMBER)), "number unknown");
areaRef = RMUtilities.notEmpty(areasCursor.getString(areasCursor.getColumnIndex(RMDbAdapter.AREA_REF)), "");
if (areaRef != ""){
areaRef = " (" + areaRef + ")";
}
message.append("______________________________________________________").append("\r\n");
message.append("\r\n");
message.append("Area ").append(areaNumber).append(areaRef).append("\r\n");
message.append("\r\n");
}
while (areasCursor.moveToNext());
}
areasCursor.close();
message.append("______________________________________________________").append("\r\n");
message.append("\r\n");
message.append("Please fully off-load above locations (for uprights, please off-load bays either side). \r\n");
message.append("\r\n");
message.append("We will follow up with our full report and quotation for repairs shortly. \r\n");
message.append("\r\n");
message.append("In the meantime, if you have any queries, please give me a call on 07970 088845. \r\n");
message.append("\r\n");
message.append("Kind regards, \r\n");
message.append("\r\n");
message.append(inspector).append("\r\n");
sendEmail(recipient, subject.toString(), message.toString());
}
以下是触发电子邮件的代码:
private void sendEmail(String recipient, String subject, String message) {
try {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.setType("message/rfc822");
// emailIntent.setType("high/priority"); //This didn't work - any way to do this though!?
if (recipient != null) emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{recipient});
if (subject != null) emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
if (message != null) emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
} catch (ActivityNotFoundException e) {
// cannot send email for some reason
}
}
任何指针都非常赞赏。
答案 0 :(得分:0)
您可能需要查看OpenCSV库。它是免费的,开源的,易于使用。它不支持直接从SQLite DB转储,但是编写实现它的必要代码应该非常简单。 OpenCSV确实支持将对象列表写入CSV,因此如果您可以将数据从sqlite数据库中取出到List中,那么您将很高兴。
此主题也是here is a post。
OpenCSV也会写入文件。然后可以将此文件附加到电子邮件中,或者按照您的意愿使用(上传到谷歌驱动器,通过USB等转移到计算机)。
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"));
// feed in your array (or convert your data to an array)
writer.write(entries);
writer.close();
这只是一个片段,我现在从内存写入工作,所以它可能有点偏。您也可以在android中查找编写文件。您可以将其存储在外部存储器或应用程序存储器中。如果要从应用程序外部访问该文件,则需要将其放在外部存储器中。
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,"subject line");
sendIntent.putExtra(Intent.EXTRA_TEXT,"Body of email");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("yourfile.csv")));
sendIntent.setType("vnd.android.cursor.dir/email");
startActivity(Intent.createChooser(sendIntent,"Email:"));