My aim is to create a .csv file from a table, to print a report. I can then store this .csv file into my sdCard. I have referred to some questions similar to this but they ask a jar file to be present. Is there any other way without a jar file to be integrated?
Have integrate a jar file in the component au.com.bytecode.opencsv.CSVWriter...What I dont find here is the logic to write into a new line...Help?
CSV is a known as a comma separated values file. You can use it to store data in a table structured format. For example, save following table to app/src/main/assets/movies. csv file of the project. It has two columns: title, and year as shown below.
After searching lots over internet i got solution of creating Csv file inside android App, internal storage (without SD Card), So i decided to share with others how to create csv file in android & How to attach it with mail.
First Download Opencsv.jar
library and add to your android project. https://sourceforge.net/projects/opencsv/files/opencsv/
Add the implementation directly (no download of .jar
)
implementation 'com.opencsv:opencsv:4.6' // Here is opencsv library
Or add jar file in android project :
opencsv.jar
file and paste in libs
folder.opencsv.jar
file and hit Add as library
.Add permission to Menifest.xml( If device API is 23 or greater Check RunTime Permission)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Open build.gradle (Module:app) and check it added :
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.android.support:recyclerview-v7:26.+'
testCompile 'junit:junit:4.12'
implementation files('libs/opencsv-4.1.jar') // Here is opencsv library added to project when using .jar
}
Now coding part : Creating csv file and inserting data into it.
String csv = (Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyCsvFile.csv"); // Here csv file name is MyCsvFile.csv
//by Hiting button csv will create inside phone storage.
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CSVWriter writer = null;
try {
writer = new CSVWriter(new FileWriter(csv));
List<String[]> data = new ArrayList<String[]>();
data.add(new String[]{"Country", "Capital"});
data.add(new String[]{"India", "New Delhi"});
data.add(new String[]{"United States", "Washington D.C"});
data.add(new String[]{"Germany", "Berlin"});
writer.writeAll(data); // data is adding to csv
writer.close();
callRead();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Now if you want to view your csv file inside the android follow below steps:
Now, If you want to attach this csv file to your app with attachment
Here is the code for attachment of csv file in mail :
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"email@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");
File file = new File(csv);
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));
}
});
check below code to generate CSV file. no need to use jar file.
you have to save one csv file in to SD-CARD.
Sample CSV FILE
public void exportEmailInCSV() throws IOException {
{
File folder = new File(Environment.getExternalStorageDirectory()
+ "/Folder");
boolean var = false;
if (!folder.exists())
var = folder.mkdir();
System.out.println("" + var);
final String filename = folder.toString() + "/" + "Test.csv";
// show waiting screen
CharSequence contentTitle = getString(R.string.app_name);
final ProgressDialog progDailog = ProgressDialog.show(
MailConfiguration.this, contentTitle, "even geduld aub...",
true);//please wait
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
}
};
new Thread() {
public void run() {
try {
FileWriter fw = new FileWriter(filename);
Cursor cursor = db.selectAll();
fw.append("No");
fw.append(',');
fw.append("code");
fw.append(',');
fw.append("nr");
fw.append(',');
fw.append("Orde");
fw.append(',');
fw.append("Da");
fw.append(',');
fw.append("Date");
fw.append(',');
fw.append("Leverancier");
fw.append(',');
fw.append("Baaln");
fw.append(',');
fw.append("asd");
fw.append(',');
fw.append("Kwaliteit");
fw.append(',');
fw.append("asd");
fw.append(',');
fw.append('\n');
if (cursor.moveToFirst()) {
do {
fw.append(cursor.getString(0));
fw.append(',');
fw.append(cursor.getString(1));
fw.append(',');
fw.append(cursor.getString(2));
fw.append(',');
fw.append(cursor.getString(3));
fw.append(',');
fw.append(cursor.getString(4));
fw.append(',');
fw.append(cursor.getString(5));
fw.append(',');
fw.append(cursor.getString(6));
fw.append(',');
fw.append(cursor.getString(7));
fw.append(',');
fw.append(cursor.getString(8));
fw.append(',');
fw.append(cursor.getString(9));
fw.append(',');
fw.append(cursor.getString(10));
fw.append(',');
fw.append('\n');
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
// fw.flush();
fw.close();
} catch (Exception e) {
}
handler.sendEmptyMessage(0);
progDailog.dismiss();
}
}.start();
}
}
add this permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With