0

我已经研究出如何触发电子邮件并用数据库查询中的信息填充它,但我想将其写入 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 
    } 
}

任何指针都非常感谢。

4

1 回答 1

0

您可能想查看OpenCSV库。它是免费、开源且易于使用的。它不支持直接从 SQLite DB 转储,但编写必要的代码来实现这一点应该非常简单。OpenCSV 确实支持将对象列表写入 CSV,因此,如果您可以将数据从 sqlite DB 中取出到列表中,那么您会很高兴。

这里还有一篇关于这个主题的帖子。

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:")); 
于 2012-12-20T21:13:03.683 回答