Create an Excel file and insert a value in java
Public class test {
/**
* Exported CVS file
* @ Param exportData the data set to be imported into Excel
* @ Param rowMapper Excel Header
* @ Param outputPath: location where the Excel file is saved
* @ Return Excel File
*/
@ SuppressWarnings ("rawtypes ")
Public static File createCSVFile (List exportData, LinkedHashMap rowMapper, String outputPath ){
File csvFile = null;
BufferedWriter csvFileoutputStream = null;
Try {
String temp = "Comparison and Analysis of Dynamic Data update statistical table _";
CsvFile = File. createTempFile (temp, ". csv", new File (outputPath ));
// GB2312 enable the correct delimiter ","
CsvFileoutputStream = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (csvFile), "GB2312"), 1024 );
// Write the file header
For (Iterator propertyIterator = rowMapper. entrySet (). iterator (); propertyIterator. hasNext ();){
Java. util. Map. Entry propertyEntery = (java. util. Map. Entry) propertyIterator. next ();
CsvFileoutputStream. write (propertyEntery. getValue (). toString ());
If (propertyIterator. hasNext ()){
CsvFileoutputStream. write (",");
}
}
// Line feed
CsvFileoutputStream. newLine ();
// Write File Content
For (Iterator iterator = exportData. iterator (); iterator. hasNext ();){
Object row = iterator. next ();
For (Iterator propertyIterator = rowMapper. entrySet (). iterator (); propertyIterator. 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 Excel Header
LinkedHashMap map = new LinkedHashMap ();
Map. put ("1", "Serial Number ");
Map. put ("2", "court name ");
Map. put ("3", "number of courts under jurisdiction ");
Map. put ("4", "number of courts with data ");
Map. put ("5", "Number of automatically aggregated cases ");
Map. put ("6", "Number of successful warehouse receiving cases ");
Map. put ("7", "number of failed warehouse receiving cases ");
Map. put ("8", "New on the current day ");
Map. put ("9", "completed on the current day ");
Map. put ("10", "number of courts ");
Map. put ("11", "New on the current day ");
Map. put ("12", "completed on the current day ");
Map. put ("13", "number of courts with data ");
/**
* ExportData Excel Data Set
* Note that the number of cells in the row is the same as that in the header (but not required)
*/
ListExportData = new ArrayList(); // Excel Data Set
Map row1 = new LinkedHashMap (); // Each piece of data must be consistent with the header (cyclically, and added to exporData in turn)
Row1.put ("1", "10001"); // row ("cell index, starting from 0", "cell stored value ");
Row1.put ("2", "Beijing High Court ");
Row1.put ("3", "2000 ");
Fig ("4", "200 ");
Row1.put ("4", "451 ");
Fig ("6", "45612 ");
Row1.put ("7", "59525 ");
Row1.put ("8", "8518 ");
Row1.put ("9", "1569 ");
Fig ("10", "235 ");
Fig ("11", "125 ");
Row1.put ("12", "125 ");
Row1.put ("13", "125 ");
ExportData. add (row1 );
Test. createCSVFile (exportData, map, "F :/");
}
}