POI 读取excel数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013234928/article/details/73249960
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.8</version>
		</dependency>
package com.ibu.test.client;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * POI操作EXCEL对象 
 * HSSF:操作Excel 97(.xls)格式  每个sheet页不能超过65536条的限制
 * XSSF:操作Excel 2007 OOXML (.xlsx)格式,条数增加了,但是导出过程中,内存占用率却高于HSSF. 
 * SXSSF:从POI3.8 beta3开始支持,基于XSSF,低内存占用
 * 
 * @date: 2017年6月14日
 */
public class ExcelPOIUtil {
    private static List< Map<String,Object>> dataList = new ArrayList< Map<String,Object>>();

    /**
     * XSSF:操作Excel  读取  (.xlsx)格式
     * @date: 2017年6月14日
     */
    public static List< Map<String,Object>> readExcelOfXSSF(String path){
        try{
            FileInputStream input = new FileInputStream(new File(path)); // 读取的文件路径
            XSSFWorkbook wb = new XSSFWorkbook(new BufferedInputStream(input));

            int sheet_numbers = wb.getNumberOfSheets();// 获取表的总数
            int count = 0;//统计导出的数据个数
            Map<String,Object> map=null;
            List<List<String>> rowList=null;
            for(int i = 0; i < sheet_numbers; i++){// 遍历所有表
                XSSFSheet sheet = wb.getSheetAt(i); // 获取 某个表
                count = count + sheet.getLastRowNum();
                System.out.println("------>>>---正在读取Excel表数据,当前表:" + sheet.getSheetName());
                map = new HashMap<String,Object>();
                map.put("tableNo", i+"");
                map.put("tableName", sheet.getSheetName());
                
                rowList=new ArrayList<List<String>>();

                List<String> colunsList=null;
                for(int rows = 1; rows <= sheet.getLastRowNum(); rows++){// 有多少行
                    XSSFRow row = sheet.getRow(rows);// 取得某一行 对象
                    
                    colunsList=new ArrayList<String>();
                    for( int columns=0;columns<row.getLastCellNum();columns++){//读取所有列  
                        XSSFCell  cell = row.getCell(columns); 
                        String columnsValue="";
                        if(cell!=null){   
                            switch ( cell.getCellType()) {    
                             case XSSFCell.CELL_TYPE_STRING: // 字符串     
                                 columnsValue = cell.getStringCellValue();    
                                 break;   
                             case XSSFCell.CELL_TYPE_NUMERIC: // 数字                            
                                    double strCell = cell.getNumericCellValue();  
                                    columnsValue=strCell+"";
                                 break;  
                             case XSSFCell.CELL_TYPE_BLANK: // 空值     
                                 break;     
                             default:     
                                 System.out.print("\n---单元格格式不支持---");     
                                 break;     
                             } 
                        }
                        colunsList.add(columnsValue);
                    }
                    
                    rowList.add(colunsList);
                }
                map.put("datas", rowList);
                dataList.add(map);
            }

            System.out.println("共有数据:" + count + "条");
        }catch(Exception e){
            e.printStackTrace();
        }
        return dataList;
    }

    public  static void writeFile(String content,String path){
        try{
            File file = new File(path);
            if(!file.exists()) file.createNewFile();
            FileOutputStream out = new FileOutputStream(file, false); // 如果追加方式用true
            out.write(content.toString().getBytes("utf-8"));// 注意需要转换对应的字符集
            out.close();
        }catch(IOException ex){
            System.out.println(ex.getStackTrace());
        }
    }

    public static void main(String[] args){
        try{
            List< Map<String,Object>> list=ExcelPOIUtil.readExcelOfXSSF("E:\\a.xls");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

猜你喜欢

转载自blog.csdn.net/u013234928/article/details/73249960