How to create an excel with an object in Android and send it via Email

A CSV file is a simple comma separated text file. In your case, the format will be:

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

As long as you are able to write records in above format to a file with extension "csv", you will be able to open it in excel and email it too.

Please refer to following stackoverflow post.

How to create a .csv on android


You can use Open CSV also.

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

you can refer this.

Java Object to CSV file


Here is an example of what you could do.

First I created a Question class:

class Question {
    String question;
    String answer;

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

And a Quiz class:

public class Quiz {

    String quizName;
    List<Question> questions;

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

Then, here is the actual application, where I make use of 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();
        }

    }

}

What is important, is that you include the jar-files for org.apache.poi. Or, as I did, add the dependencies to your Maven pom-file (or gradle file e.g., if you do Android development). Here is my pom-file:

<?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>

Hope this works for you (does for me)!

I uploaded my solution to GitHub: https://github.com/mwbouwkamp/create-excel

In case of Android development, add the following dependency:

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