public class Export {public static final String CSVNAME_COMPETITION="cpc.csv";}
Public class CSV {public static final string endline = "\ n"; public static final map <string, string> headlines = new hashmap <string, string> (); static {// the headlines of the first line of the Excel document. put (export. csvname_competition, formattocsv ("1", "2", "3", "4", "5");} public static string formattocsv (object... params) {stringbuilder sb = new stringbuilder ("\" "+ Params [0] +" \ ""); For (INT I = 1; I <Params. length; I ++) {sb. append (","); If (Params [I] instanceof double & (double) Params [I]). isinfinite () {sb. append ("\" 0.0 \ "");} else {sb. append ("\" "+ Params [I] +" \ "") ;}} sb. append (endline); return sb. tostring ();} public static string getheadline (string filename) {return headlines. get (filename );}}
Import Java. io. bufferedwriter; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; public class output {public static bufferedwriter getwriter (string name, string path) {bufferedwriter BR = NULL; try {mkdirs (PATH ); file file = new file (path + "/" + name); BR = new bufferedwriter (New filewriter (File); // If (! File. exists () {// note that we usually convert the TXT file into an Excel file. Because of the encoding format, we must add the upper/lower line, it can only start. BR. write (new string (New byte [] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); //} BR. write (new string (CSV. getheadline (name ). getbytes ("UTF-8"); BR. flush (); // BR. close ();} catch (ioexception e) {e. printstacktrace ();} return BR;} public static void mkdirs (string path) {file F = new file (PATH); If (! F. isdirectory () F. Delete (); If (! F. exists () F. mkdirs ();}}
@ Testpublic void Excel () throws ioexception {writer = output. getwriter (export. csvname_competition, "E:/testexcel"); writer. write (CSV. formattocsv ("competitor", "quantity", "advertisement", "key"); writer. close ();}
Convert a TXT file to an Excel file with a title bar