Excel batch convert to TXT-EXCEL convert to TXT-Java EXCEL to txt

Source: Internet
Author: User
Tags extension rar

I found a Java code on the Internet for converting EXCEL to txt, and changed it to convert EXCEL to TXT in batches:

 

Of course, we need to use three jar package here, you can go to the idea of the file, decompress the package can be pulled from the poi-3.1-FINAL-20080629.jar poi-scratchpad-3.1-FINAL-20080629.jar poi-contrib-3.1-FINAL-20080629.jar three packages. Add it to the database of your project. (If a file without an extension is found to be decompressed after downloading, add the extension RAR and decompress it)

The Java classes used are as follows:

 

Excelreader. Java

 

Package convert; </P> <p> Import Java. io. *; <br/> Import Java. util. date; <br/> Import Org. apache. poi. hssf. usermodel. hssfcell; <br/> Import Org. apache. poi. hssf. usermodel. hssfdateutil; <br/> Import Org. apache. poi. hssf. usermodel. hssfrow; <br/> Import Org. apache. poi. hssf. usermodel. hssfsheet; <br/> Import Org. apache. poi. hssf. usermodel. hssfworkbook; </P> <p> public class excelreader {</P> <p> // create a file input stream </P> <p> privat E bufferedreader reader = NULL; </P> <p> // file type </P> <p> private string filetype; </P> <p> // file binary input stream </P> <p> private inputstream is = NULL; </P> <p> // current sheet </P> <p> private int currsheet; </P> <p> // current position </P> <p> private int currposition; </P> <p> // number of sheets </P> <p> private int numofsheets; </P> <p> // hssfworkbook </P> <p> hssfworkbook workbook = NULL; </P> <p> // separate cells by spaces </P> <p> Private Static string exce Rochelle line_delimiter = ""; </P> <p> // set the maximum number of columns </P> <p> Private Static int max_excel_columns = 64; </P> <p> // constructor creates an excelreader </P> <p> Public excelreader (string inputfile) throws ioexception, exception {</P> <p> // determines whether the parameter is null or meaningless </P> <p> If (inputfile = NULL | inputfile. trim (). equals ("") {</P> <p> throw new ioexception ("no input file specified "); </P> <p >}</P> <p> // get the filename suffix and assign it to filetype </P> <p> This. filetyp E = inputfile. substring (inputfile. lastindexof (". ") + 1); </P> <p> // set the starting behavior to 0 </P> <p> currposition = 0; </P> <p> // set the current position to 0 </P> <p> currsheet = 0; </P> <p> // create a file input stream </P> <p> is = new fileinputstream (inputfile ); </P> <p> // determine the file format </P> <p> If (filetype. equalsignorecase ("xls") {</P> <p> // create an hssfworkbook for reading an Excel file </P> <p> workbook = new hssfworkbook (is ); </P> <p> // set the number of sheets </P> <p> numofsheets = workbook. getnum Berofsheets (); </P> <p >}</P> <p> else {</P> <p> throw new exception ("file type not supported "); </P> <p >}</P> <p> // The Readline function reads a row of a file. </P> <p> Public String readline () throws ioexception {</P> <p> // if it is an xls file, the file is read through the API provided by poi </P> <p> If (filetype. equalsignorecase ("xls") {</P> <p> // obtain the Current Sheet Based on the currsheet value </P> <p> hssfsheet sheet = workbook. getsheetat (currsheet); </P> <p> // determines whether the current row ends with the previous sheet. <br/> // System. out. println (sheet. getlastrownum (); </P> <p> If (currposition> Sheet. getlastrownum () {</P> <p> // The current row position is cleared. <br/> currposition = 0; </P> <p> // determine whether sheet exists. </P> <p> If (currsheet! = Numofsheets-1) {</P> <p> currsheet ++; </P> <p> // obtain the next sheet <br/> Sheet = workbook. getsheetat (currsheet); </P> <p >}else {<br/> return NULL; <br/>}</P> <p> // obtain the current row number </P> <p> int ROW = currposition; </P> <p> currposition ++; </P> <p> // read data from the current row </P> <p> return Getline (sheet, row ); </P> <p >}</P> <p> return NULL; </P> <p >}</P> <p> // The Getline function returns a row of sheet data. </P> <p> private string Getline (hssfsheet sheet, in T row) {</P> <p> // obtain a sheet row based on the number of rows </P> <p> hssfrow rowline = sheet. getrow (ROW); </P> <p> // create a character to create a buffer </P> <p> stringbuffer buffer = new stringbuffer (); </P> <p> // obtain the number of columns of the current row <br/> int filledcolumns; <br/> try {<br/> filledcolumns = rowline. getlastcellnum (); <br/>} catch (nullpointerexception e) {<br/> return "the number of retrieved rows is empty! "; <Br/>}</P> <p> hssfcell cell = NULL; </P> <p> // cyclically traverse all columns </P> <p> for (INT I = 0; I <filledcolumns; I ++) {</P> <p> // obtain the current cell </P> <p> cell = rowline. getcell (short) I); </P> <p> string cellvalue = NULL; </P> <p> If (cell! = NULL) {</P> <p> // determine the type of the current cell </P> <p> switch (cell. getcelltype () {</P> <p> // if the current cell type is Numeric </P> <p> case hssfcell. cell_type_numeric: {</P> <p> // determines whether the current cell is date </P> <p> If (hssfdateutil. iscelldateformatted (cell) {</P> <p> // if it is of the date type, obtain the date value of the cell </P> <p> date = cell. getdatecellvalue (); </P> <p> // converts a date to a string in the local format </P> <p> cellvalue = cell. getdatecellvalue (). tostring (); </P> <p >}</P> <p> // If it is a pure number </P> <p> else {</P> <p> // obtain the value of the current cell </P> <p> integer num = new INTEGER (INT) cell </P> <p>. getnumericcellvalue (); </P> <p> cellvalue = string. valueof (Num); </P> <p >}</P> <p> break; </P> <p >}</P> <p> // if the current cell type is strin </P> <p> case hssfcell. cell_type_string: </P> <p> // obtain the current cell string </P> <p> cellvalue = cell. getrichstringcellvalue (). tostring (). replaceall ("'", "'' "); </P> <p> break; </P> <p> // default cel L value </P> <p> default: </P> <p> cellvalue = ""; </P> <p >}</P> <p >}else {</P> <p> cellvalue = ""; </P> <p >}</P> <p> // insert a delimiter between each field </P> <p> buffer. append (cellvalue ). append (excel_line_delimiter); </P> <p >}</P> <p> // returns data of the row as a string </P> <p> return buffer. tostring (); </P> <p >}</P> <p> // close the function execution stream close operation </P> <p> Public void close () {</P> <p> // If is not empty, the inputsteam file input stream is disabled. </P> <p> If (is! = NULL) {</P> <p> try {</P> <p> is. close (); </P> <p >}catch (ioexception e) {</P> <p> is = NULL; </P> <p >}</P> <p> // close the input stream of the bufferedreader file if the reader is not empty </P> <p> If (reader! = NULL) {</P> <p> try {</P> <p> reader. close (); </P> <p >}catch (ioexception e) {</P> <p> reader = NULL; </P> <p >}</P> <p>

 

 

Main. Java

 Package convert; </P> <p> Import Java. io. file; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; </P> <p> public class main {<br/> Private Static file [] getfiles (string path) {<br/> file = new file (PATH); <br/> // get the folder list <br/> file [] array = file. listfiles (); <br/> return array; <br/>}</P> <p> Public static void main (string [] ARGs) throws ioexception {<br/> // todo Code application logic here <br/> file [] files = getfiles ("D: // LSS // data //"); <br/> for (INT I = 0; I <files. length; I ++) {<br/> string filepath = files [I]. getpath (); <br/> string filename = files [I]. getname (); <br/> string newfilename = filename. substring (0, filename. indexof ('. '); <br/> string S = ""; <br/> try {</P> <p> excelreader ER = new excelreader (filepath ); </P> <p> string line = ER. readline (); </P> <p> while (Li Ne! = NULL) {</P> <p> S = S + LINE + "/R/N"; <br/> line = ER. readline (); </P> <p >}</P> <p> er. close (); </P> <p >}catch (exception e) {</P> <p> E. printstacktrace (); </P> <p >}< br/> // system. out. println (s); <br/> file newfile = new file ("D: // LSS // data //" + newfilename + ". TXT "); <br/> fileoutputstream fops = new fileoutputstream (newfile); <br/> fops. write (S. getbytes (); </P> <p >}< br/>}</P> <p>

 

Related Article

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.