org.apache.poi 读取 excel xls xlsx

http://poi.apache.org/download.html#POI-3.7

 

import java.io.FileInputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellReference;
 
Workbook wb = WorkbookFactory.create(new FileInputStream("d:/test/1.xlsx"));              
Sheet sheet = wb.getSheetAt(0);                                                           
int count = 0;                                                                            
for(Row row : sheet){                                                                     
	count++;                                                                              
	for(Cell cell : row){                                                                 
		CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
        System.out.print(cellRef.formatAsString());                                       
        System.out.print(" - ");                                                          
                                                                                          
        switch (cell.getCellType()) {                                                     
            case Cell.CELL_TYPE_STRING:                                                   
                System.out.println(cell.getRichStringCellValue().getString());            
                break;                                                                    
            case Cell.CELL_TYPE_NUMERIC:                                                  
                if (DateUtil.isCellDateFormatted(cell)) {                                 
                    System.out.println(cell.getDateCellValue());                          
                } else {                                                                  
                    System.out.println(cell.getNumericCellValue());                       
                }                                                                         
                break;                                                                    
            case Cell.CELL_TYPE_BOOLEAN:                                                  
                System.out.println(cell.getBooleanCellValue());                           
                break;                                                                    
            case Cell.CELL_TYPE_FORMULA:                                                  
                System.out.println(cell.getCellFormula());                                
                break;                                                                    
            default:                                                                      
                System.out.println();                                                     
        }                                                                                 
	}                                                                                     
}                                                                                         
System.out.println("total:"+count);                                                       
 

double转string,如3.0020002001E10转30020002001

NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
System.out.println(nf.format(cell.getNumericCellValue()));
 

你可能感兴趣的:(java,apache,code,poi)