欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

org.apache.poi 读取 excel xls xlsx 博客分类: 编程技术 javacodepoiapache 

程序员文章站 2024-02-12 15:16:46
...

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()));