Convert a TXT file to an Excel file with a title bar

Source: Internet
Author: User
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

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.