Android如何将数据存储/保存到Excel文件或CVS文件

时间:2018-07-14 23:11:29

标签: java android

我正在开发新的应用程序,以从用户那里获取一些信息,例如他的姓名,jop,他的年龄等。

谢谢..

1 个答案:

答案 0 :(得分:0)

CSV文件只是一个文本文件,因此比Excel文件要容易得多。例如:

// get a Uri to write to first

// Some made up data to write
StringBuilder d = new StringBuilder();
d.append("col1,col2,col3\n");
d.append("1,2,3\n");
d.append("2,3,4\n");
String data = d.toString();

// Get a file output stream however is compatible with your app. Showing one of
// many ways here.
// once you have it, call outputStream.write(data.getBytes());
try {
    ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "w");
    if (pfd != null) {
        FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
        fileOutputStream.write(data.getBytes());
        fileOutputStream.close();
        pfd.close();
    }
} catch (Exception e) {
    e.printStackTrace();
}