0

I wrote a simple test to verify the possibility of writing in one .csv file with AndroidStudio.

The file gets generated correctly but when I try to open it with Excel its blank.

What am I doing wrong?

I used what I saw in this thread but when I do it it doesn't work - How to Create a csv file in android

Thanks for the answers!

Here is the code:

import java.io.IOException;
import java.io.PrintWriter;

public class MainActivity extends AppCompatActivity {
    public File folder;
    private PrintWriter pw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            folder = new File(Environment.getExternalStorageDirectory() + "/Folder");
            boolean var = false;
            if(!folder.exists()){
                var = folder.mkdir();
            }
            System.out.println("" + var);
            final String filename = folder.toString() + "/" + "TestExcel.csv";
            pw = new PrintWriter(new File(filename));
            Toast.makeText(this,"File Created",Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(this,"IOException",Toast.LENGTH_SHORT).show();

            e.printStackTrace();
        }

        for(int i = 0; i<10; i++){
            writeDown(i);
        }
    }

    private void writeDown(int level)
    {
        StringBuilder sb = new StringBuilder();
        sb.append(level);
        sb.append(',');
        pw.write(sb.toString());
        Toast.makeText(this,"Writing...",Toast.LENGTH_SHORT).show();
    }
}

0 Answers0