读取execl 2003 2007

这是转自csdn

帅杰 de blog

1.引poi的jar包

  csdn有免费资源

2.代码

package com.shuaijie;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;

public class ReadExcel{   //poi读取excel数据
       
     public static void read(String fileName,boolean flag) throws Exception {  
         Workbook wb = null;  
         if(flag){//2003  
             File f = new File("D:/XXX.xls");  
               
             FileInputStream is = new FileInputStream(f);  
             POIFSFileSystem fs = new POIFSFileSystem(is);     
             wb = new HSSFWorkbook(fs);  
             is.close();  
         }else{//2007  
             wb = new XSSFWorkbook("D:/XXX.xlsx");  
         }  
         read(wb);  
     }  
       
      
     public static void read(InputStream is,boolean flag) throws Exception {  
         Workbook wb = null;  
           
         if(flag){//2003  
             wb = new HSSFWorkbook(is);  
         }else{//2007  
             wb = new XSSFWorkbook(is);  
         }  
           
         read(wb);  
     }  
       
      
     public static void read(Workbook wb) throws Exception {  
         try {  
               
             for (int k = 0; k < wb.getNumberOfSheets(); k++) {  
  
                 Sheet sheet = wb.getSheetAt(k);  
                 int rows = sheet.getPhysicalNumberOfRows();  
  
                 for (int r = 0; r < rows; r++) {  
                     // 定义 row  
                     Row row = sheet.getRow(r);  
                     if (row != null) {  
                         int cells = row.getPhysicalNumberOfCells();  
  
                         for (short c = 0; c < cells; c++) {  
                             Cell cell = row.getCell(c);  
                             if (cell != null) {  
                                 String value = null;  
  
                                 switch (cell.getCellType()) {  
  
                                 case Cell.CELL_TYPE_FORMULA:  
                                     value = "FORMULA value=" + cell.getCellFormula();  
                                    break;  
  
                                 case Cell.CELL_TYPE_NUMERIC:  
                                     if(HSSFDateUtil.isCellDateFormatted(cell)){  
                                         value = "DATE value=" 
                                             + cell.getDateCellValue();  
                                     }else{  
                                         value = "NUMERIC value=" 
                                                 + cell.getNumericCellValue();  
                                     }  
                                       
                                     break;  
  
                                 case Cell.CELL_TYPE_STRING:  
                                     value = "STRING value=" 
                                             + cell.getStringCellValue();  
                                     break;  
                                       
                                 case Cell.CELL_TYPE_BOOLEAN:  
                                     value = "BOOLEAN value=" 
                                             + cell.getBooleanCellValue();  
                                       
                                      
                                     cell.getDateCellValue();  
                                       
                                     break;  
 
                                 default:  
                                 }  
                                  
                                 System.out.println(value);  
  
                             }  
                         }  
                     }  
                 }  
             }  
         } catch (Exception e) {  
  
             e.printStackTrace();  
         }  
  
    }  
  
      
     public static void main(String[] args) throws Exception {  
        
  
         File f = new File("D:/XXX.xlsx");  
           
         FileInputStream is = new FileInputStream(f);  
           
         System.out.println(f.getName());  
           
         read(is,false);  
           
          
    }  
 

你可能感兴趣的:(exec)