File download public method and call

Source: Internet
Author: User

/*
* List<string[]> Incoming
* Public method
*/
public static Boolean exportcsv (file file, List dataList) {
Boolean issuccess = false;

if (file = = NULL | |!file.getname (). toUpperCase (). EndsWith (format)) {
Issuccess = false;
} else {
FileOutputStream out = null;
OutputStreamWriter OSW = null;
BufferedWriter bw = NULL;

int i = 1;
try {
BW = new BufferedWriter (new FileWriter (file));
for (int j = 0; J < Datalist.size (); j + +) {
The first line is the title
string[] data = (string[]) Datalist.get (j);
String datastr = "";
for (int k = 0; k < data.length; k++) {
if (k = = data.length-1) {
Datastr = Datastr + data[k];
} else {
Datastr = Datastr + data[k] + ",";
}
}
Bw.write (DATASTR);
Bw.newline ();
}
Bw.flush ();
Issuccess = true;
} catch (IOException e) {
E.printstacktrace ();
Issuccess = false;
} finally {
if (out! = null) {
try {
Out.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

}

return issuccess;
}

Call

File csvfile = null;
try {
Generate Temporary files
CSVFile = File.createtempfile ("Template name", "template format");
} catch (IOException e) {
E.printstacktrace ();
}
list<string[]> dataList = new arraylist<string[]> ();
string[] data = new string[]{"name", "Age", "number"}; File Column Name
Datalist.add (data);

Csvutil.exportcsv (CSVFile, dataList);

File download public method and call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.