/ * * CREATED ON 2004-11-5 * * Java reads Excel file * / package readxls;
Import java.io.fileinputstream; import java.io.fileoutputstream;
Import java.text.decimalformat;
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel. HSSFWORKBOOK;
/ ** * * @Author shiwt * * java reads Excel file * * A level of an Excel file: Excel file -> Worksheet -> Row -> cells correspond to POI, for: Workbook-> Sheet-> row -> Cell * * / public class poitest {
Public static string outputfile = "e: //test/tels.xls";
Public static string filetoberead = "e: //test/tels.xls";
Public void createExcel () {Try {// Create a new Excel workbook hssfworkbook workbook = new hssfworkbook (); // Construction a worksheet in the Excel workbook, named the default value // To create a new one "Benefit Indicators" worksheet, its statement is: // hssfsheet sheet = workbook.createsheet; hssfsheet sheet = workbook.createsheet (); // Create a row in the index 0 position (top line HSSFROW ROW = Sheet.Createrow ((Short) 0); // Create a single element (left upper end) hssfcell cell = row.createcell (short) 0); / / Define cell type Cell.setcellType (HSSFCELL.CELL_TYPE_STRING); // Enter some content in cell Cell.SetcellValue ("SWEATER"); // New output file stream FileOutputStream Fout = new fileoutputstream (OutputFile); // Put the corresponding Excel work Book Disk Workbook.write (fout); fout.flush (); // Operation, close file fout.close (); system.out.println ("File Generate ...");
} Catch (Exception E) {system.out.println ("Run XLCREATE ():" E);}}
/ ** * * reads Excel, traversing various points to get information, and determines if it is a mobile phone number, and display the correct mobile phone number * * * Note: 1.Sheet, start with Workbook.getnumberofsheets () -1 End 2.Row, * starts at 0 (GetFirstrownum), end 3.Cell, * starts with 0 start (getFirstCellnum), endlastcellnum, ended, unknown reason, the length of the display, may Length * / public void readExcel () {// will be expressed as 13922433397E10 mobile phone number transformation to 13922433397, not necessarily the best conversion method decimalformat df = new decimalformat; try {// creation References to Excel workbook files HSSFWORKWORK WORKBOOK = New HSSFWORKBOOK (New FileInputStream (Filetoberead)); //system.out // .println ("=== SheetSnum ===" Workbook.getNumberofsheets ()); // Get sheet number for (int numSheets = 0; numSheets for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet .getLastRowNum (); rowNumOfSheet ) {if (! null = aSheet.getRow (rowNumOfSheet)) {HSSFRow aRow = aSheet.getRow (rowNumOfSheet); //System.out.println(">>>GetfirstCellnum<<<" // arow.getfirstcellnum ()); //system.out.println (">>>getlastCellnum<<<" // arow.getlastcenum ()); For (short cellnumofrow = 0; cellnumofrow <= arow .getlastcellnum (); cellnumofrow ) { if (null = aRow.getCell (cellNumOfRow)!) {HSSFCell aCell = aRow.getCell (cellNumOfRow); int cellType = aCell.getCellType (); //System.out.println(cellType); switch (cellType) {case 0 : // numeric string strchool = df.format (Acell .getNumericcellValue ()); system.out.println (strCell); Break; case 1: // string strchool = Acell.getstringcellValue (); system.out.println (strCell); Break; default: //system.out.println ("Format is not read"); // Other format data} } } } } } } catch (exception e) {system.out.println ("readExcelerror" E); } Public static void main (string [] args) {poitest poi = new poitest (); //poi.createexcel (); poi.readexcel (); }