如何在Android中使用对象创建Excel并通过电子邮件发送

时间:2019-03-29 10:12:27

标签: java android excel kotlin

在我的应用程序上,我有一个记录对象,比方说我要与5个以上的人进行测验,这样我就可以获取所有记录的结果,并且我有一个包含所有这些信息的对象,是否是可能将此对象转换为Excel文件?例如:

|---------------------|------------------|
|        Quiz         |     Question 1   |...
|---------------------|------------------|
|      Quiz Name      |    What's 1+1    |...
|---------------------|------------------|

像这样的事情,要知道每个用户的回答及其得分,所有这些我都将其保存在对象上。

然后,如果我可以通过.xls或任何格式通过邮件发送它。

编辑

例如,我需要测验名称,如果我可以添加更多有关该测验的信息,可以,但是随后我需要添加所有已问过该测验(api的另一个对象)的用户,以便我可以获取名称和得分。

无论显示什么内容,我都想拥有.csvExcel

假设我有一个列表:

  1. createdAt
  2. updatedAt
  3. 用户(仅是user.email)
  4. 测验(只是测验名称)
  5. 总积分

所以我想将其填充到.csvExcel之内

4 个答案:

答案 0 :(得分:6)

CSV文件是一个简单的逗号分隔的文本文件。在您的情况下,格式为:

Quiz,Question 1
Quiz Name,What's 1+1

只要您能够将上述格式的记录写入扩展名为“ csv”的文件,就可以在excel中打开它并通过电子邮件发送。

请参阅以下stackoverflow帖子。

How to create a .csv on android

答案 1 :(得分:3)

这是您可以做什么的一个例子。

首先,我创建了一个Question类:

class Question {
    String question;
    String answer;

    Question(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }
}

和测验课程:

public class Quiz {

    String quizName;
    List<Question> questions;

    void addQuestion(Question question) {
        if (null == questions) {
            questions = new ArrayList<>();
        }
        questions.add(question);
    }
}

然后,这是实际的应用程序,我在其中使用了Apache POI:

public class MailExcel {

    public static void main(String[] args) {

        //Creating the quiz

        Quiz mQuiz = new Quiz();
        mQuiz.quizName = "Excel-quiz";
        Question question1 = new Question("Where do you find the best answers?", "Stack-Overflow");
        Question question2 = new Question("Who to ask?", "mwb");
        mQuiz.addQuestion(question1);
        mQuiz.addQuestion(question2);


        //Creating the workbook

        Workbook workbook = new XSSFWorkbook();
        CreationHelper creationHelper = workbook.getCreationHelper();
        Sheet sheet = workbook.createSheet("Quiz");
        Row row1 = sheet.createRow(0);
        Row row2 = sheet.createRow(1);
        row1.createCell(0).setCellValue("Quiz");
        row2.createCell(0).setCellValue(mQuiz.quizName);
        int col = 1;
        for (Question question: mQuiz.questions) {
            row1.createCell(col).setCellValue("Question " + col);
            row2.createCell(col).setCellValue(question.question);
            col++;
        }


        //Creating and saving the file

        FileOutputStream file = null;
        try {
            file = new FileOutputStream("quiz.xlsx");
            workbook.write(file);
            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

重要的是要包括org.apache.poi的jar文件。或者像我一样,将依赖项添加到您的Maven pom文件(或gradle文件,例如,如果您进行Android开发)。这是我的pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>mail-excel</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
    </dependencies>

</project>

希望这对您有用(对我有用)!

我将解决方案上传到GitHub:https://github.com/mwbouwkamp/create-excel

如果是Android开发,请添加以下依赖项:

implementation "org.apache.poi:poi:3.17"
implementation "org.apache.poi:poi-ooxml:3.17"

答案 2 :(得分:2)

您也可以使用Open CSV。

<dependency> 
    <groupId>com.opencsv</groupId> 
    <artifactId>opencsv</artifactId> 
    <version>4.1</version> 
</dependency> 

您可以参考这个。

Java Object to CSV file

答案 3 :(得分:2)

如果要脱机使用应用程序,则可以使用MWB已经回答过的Apache POI,但是由于您提到要通过电子邮件发送文件,因此我认为对互联网的访问没有限制,因此您可以考虑以下解决方案

Google App Scripts使从Google表格中获取数据的过程自动化变得非常简单,将模板电子邮件与Google表格中的指定数据组合在一起。 Google有SHEETS v4 API,您可以将其集成到Android应用中以处理工作表。您可以使用Google App脚本中的JavaScript来完成所有这些自动化过程,或者如果您希望集成到您的应用中,则可以使用API​​。