Java Operations Create new Excel and insert values

Source: Internet
Author: User

public class Test {
/**
* Exported CVS file
* @param exportdata the data collection to import into Excel
* @param rowmapper Excel header section
* @param where to save OutputPath Excel files
* @return Excel file
*/
@SuppressWarnings ("Rawtypes")
public static File Createcsvfile (List exportdata,linkedhashmap rowmapper,string outputPath) {
File csvfile = null;
BufferedWriter csvfileoutputstream = null;
try {
String temp= "Data Dynamic update situation comparative analysis of statistics _";
CSVFile = File.createtempfile (temp, ". csv", New File (OutputPath));
GB2312 make the delimiter read correctly ","
Csvfileoutputstream = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (CSVFile), "GB2312"), 1024);
Write to file header
For (Iterator propertyiterator =rowmapper.entryset (). Iterator ();p ropertyiterator.hasnext ();) {
Java.util.Map.Entry propertyentery = (java.util.Map.Entry) propertyiterator.next ();
Csvfileoutputstream.write (Propertyentery.getvalue (). toString ());
if (Propertyiterator.hasnext ()) {
Csvfileoutputstream.write (",");
}
}
Line break
Csvfileoutputstream.newline ();
Write file contents
for (Iterator Iterator = Exportdata.iterator (); Iterator.hasnext ();) {
Object row = Iterator.next ();
for (Iterator propertyiterator = Rowmapper.entryset (). Iterator ();p ropertyiterator.hasnext ();) {
Java.util.Map.Entry propertyentry= (Java.util.Map.Entry) propertyiterator.next ();
try {
Csvfileoutputstream.write (Beanutils.getproperty (Row, string.valueof (Propertyentry.getkey ())));
if (Propertyiterator.hasnext ()) {
Csvfileoutputstream.write (",");
}
} catch (Illegalaccessexception e) {
E.printstacktrace ();
} catch (InvocationTargetException e) {
E.printstacktrace ();
} catch (Nosuchmethodexception e) {
E.printstacktrace ();
}
}
if (Iterator.hasnext ()) {
Csvfileoutputstream.newline ();
}
}
Csvfileoutputstream.flush ();

} catch (IOException e) {
E.printstacktrace ();
}
finally{
try {
Csvfileoutputstream.close ();
} catch (Exception E2) {
E2.printstacktrace ();
}
}
return csvfile;

}



/**
* Test class
* @param args
*/
@SuppressWarnings ({"Rawtypes", "Unchecked"})
public static void Main (string[] args) {
Excel header map is the table header section of Excel
Linkedhashmap map = new Linkedhashmap ();
Map.put ("1", "serial number");
Map.put ("2", "court name");
Map.put ("3", "number of competent courts");
Map.put ("4", "number of courts with data");
Map.put ("5", "Automatic aggregation of Case volume");
Map.put ("6", "Number of successful cases");
Map.put ("7", "number of failed cases in storage");
Map.put ("8", "New Day Collection");
Map.put ("9", "closed on the same day");
Map.put ("10", "court number");
Map.put ("11", "New Day Collection");
Map.put ("12", "closed on the same day");
Map.put ("13", "number of courts with data");

/**
* ExportData Excel data collection
* Note that the number of cell cells in row is consistent with the length of the header (but not mandatory)
*/
list<map> ExportData = new arraylist<map> (); Excel data collection

Map row1 = new linkedhashmap<string,string> (); For each piece of data, keep it consistent with the header (loop out, add to expordata in turn)
Row1.put ("1", "10001"); Row ("Cell's index, starting from 0", "Value stored in cell");
Row1.put ("2", "Beijing High Court");
Row1.put ("3", "2000");
Row1.put ("4", "200");
Row1.put ("5", "451");
Row1.put ("6", "45612");
Row1.put ("7", "59525");
Row1.put ("8", "8518");
Row1.put ("9", "1569");
Row1.put ("10", "235");
Row1.put ("11", "125");
Row1.put ("12", "125");
Row1.put ("13", "125");
Exportdata.add (ROW1);
Test.createcsvfile (ExportData, map, "f:/");

}
}


Java Operations Create new Excel and insert values

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.