Use Apache poi and Java to obtain Excel files without the need for MS-Office ActiveX Ole

Source: Internet
Author: User
Tags ole
*
* Created on 2005/07/18
* Use poi-2.5.1-final-20050804.jar
*/
Package com.nova.colimas.common.doc;
Import org. Apache. Poi. hssf. usermodel. hssfworkbook;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
/**
* Deal with Excel files.
* @ Author Tyrone
*
*/
Public class excelprocess {
 
Public static string run (string filename ){
String text = NULL;
Try {

Int sheetnum = 0;
Hssfsheet sheet = NULL;
Hssfrow ROW = NULL;
Hssfworkbook workbook = new hssfworkbook (New fileinputstream (filename ));
For (sheetnum = 0; sheetnum <Workbook. getnumberofsheets (); sheetnum ++ ){
Sheet = Workbook. getsheetat (sheetnum );
Int lastrow = 0;
Int rownum = 0;
Lastrow = sheet. getlastrownum ();
For (rownum = 0; rownum <lastrow; rownum ++ ){
Row = sheet. getrow (rownum );
If (row! = NULL ){
Short firstcell = row. getfirstcellnum ();
Short lastcell = row. getlastcellnum ();
Short cellnum;
Hssfcell cell = NULL;
For (cellnum = firstcell; cellnum <lastcell; cellnum ++ ){
Cell = row. getcell (cellnum );
If (cell! = NULL & cell. getcelltype () = hssfcell. cell_type_numeric)
TEXT = text + cell. getnumericcellvalue ();
Else if (cell! = NULL & cell. getcelltype () = hssfcell. cell_type_string ){
TEXT = text + cell. getstringcellvalue ();
}
}
TEXT = text + "/N ";
}
}
} Catch (exception e ){
System. Out. println (E );
}
Return text;
}/* The input region is filename.xls.
Public static void main (string [] ARGs ){
Try {
Fileoutputstream out = new fileoutputstream ("resultexcel.txt ");
Out. Write (excelprocess. Run (ARGs [0]). getbytes ());
Out. Flush ();
Out. Close ();
} Catch (exception ex ){
System. Out. println (ex. tostring ());
}
}
}

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.