Java Export CVS file

Source: Internet
Author: User

Package Testcvs;

Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;
Import java.util.ArrayList;
Import java.util.List;

/**
*
* CSV file Export Tool class
*/
public class Csvutils {

public static void Main (string[] args) {

list<object> list = new ArrayList ();
List.add ("Title 1 name");
List.add ("Title 2 Age");

List<object> list3=new arraylist<object> ();
List3.add ("Data 11");
List3.add ("Data 22");
List<list<object>> datalist=new arraylist<list<object>> ();
Datalist.add (LIST3);

Createcsvfile (list, dataList);
System.out.println ("OK");
}

/**
* CSV file Generation method
*
* @param head
* @param dataList
* @param OutPutPath
* @param filename
* @return
*/
public static File Createcsvfile (list<object> head,
List<list<object>> dataList) {

File csvfile = null;
BufferedWriter csvwtriter = null;
try {
CSVFile = new File ("D:\\exportcvsfile.csv");//d:\\exportcvsfile.csv represents the physical location of the exported file
File parent = Csvfile.getparentfile ();//Get Parent Directory
if (parent! = NULL &&!parent.exists ()) {
Parent.mkdirs ();//Create Folder
}
Csvfile.createnewfile ();//create a specific file

GB2312 make the delimiter read correctly ","
Csvwtriter = new BufferedWriter (New OutputStreamWriter (
New FileOutputStream (CSVFile), "GB2312"), 1024);
Write to file header
Writerow (head, csvwtriter);

Write file contents
for (list<object> row:datalist) {
Writerow (row, csvwtriter);
}
Csvwtriter.flush ();

} catch (Exception e) {

E.printstacktrace ();
} finally {
try {
Csvwtriter.close ();
} catch (IOException e) {
E.printstacktrace ();
}

}
return csvfile;
}

/**
* Write a row of data methods
*
* @param row
* @param csvwriter
* @throws IOException
*/
private static void Writerow (list<object> row, BufferedWriter csvwriter)
Throws IOException {
Write to file header
for (Object Data:row) {
StringBuffer sb = new StringBuffer ();

Rowstr "Title 1 name",
Rowstr "Title 2 Age",
Rowstr "Data 11",
Rowstr "Data 22",

String rowstr = sb.append ("\" "). Append (data). Append (" \ ","). toString ();/"+ Data +"
Csvwriter.write (ROWSTR);
}
Csvwriter.newline ();//Each call is replaced by a line
}
}

Eventually:

Java Export CVS file

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.