码迷,mamicode.com
首页 > 其他好文 > 详细

【7.0】操作excel

时间:2017-07-17 10:08:49      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:val   employee   ==   static   mat   usermod   print   system   获取   

 

 

 

1.解析结果全部为String格式:

package com.guangzhouhuayu.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
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;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 作者:人生若只如初见
 * 时间:2017/7/14
 * 描述:
 */
public class ExcelUtils {
    /**
     * 解析 后缀.xls
     * @param is
     * @return
     * @throws Exception
     */
    public static List<List<String>> readXls(InputStream is) throws Exception{

        //HSSFWorkbook表示整个excel对象
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        List<List<String>> result = new ArrayList<List<String>>();

        //获取excel中的每一页 hssfSheet对象
        for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if(hssfSheet == null) {
                continue;
            }
            //如果不为空 处理当前sheet  获取每一行 hssfRow对象
            List<String> rowList;
            //获取标题行列数
            int lastCellNum = 0;
            //1.获取标题行
            HSSFRow hssfRow = hssfSheet.getRow(0);
            if(hssfRow != null){
                rowList = new ArrayList<String>();
                //获取标题行列数
                lastCellNum = hssfRow.getLastCellNum();
                //获取标题行的每一列 cell对象
                for(int i = 0; i < lastCellNum; i++){
                    HSSFCell cell = hssfRow.getCell(i);
                    if(cell == null) {
                        rowList.add(null);
                        continue;
                    } else{
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String cellVal = cell.getStringCellValue();
                        rowList.add(cellVal);
                    }
                }
                result.add(rowList);
            }
            //2.获取其他行
            for(int rowNum =1; rowNum <= hssfSheet.getLastRowNum(); rowNum++ ){
                hssfRow = hssfSheet.getRow(rowNum);
                if(hssfRow == null){
                    continue;
                }
                rowList = new ArrayList<String>();
                //获取每一列 cell对象
                for(int i = 0; i < lastCellNum; i++){
                    HSSFCell cell = hssfRow.getCell(i);
                    if(cell == null) {
                        rowList.add(null);
                        continue;
                    }

                    //如果cell != null, 设置取值格式为String类型
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    String cellVal = cell.getStringCellValue();
                    rowList.add(cellVal);



                }
                result.add(rowList);
            }
        }
        return result;
    }

    /**
     * 解析 后缀.xlsx
     * @param is
     * @return
     * @throws Exception
     */
    public static List<List<String>> readXlsx(InputStream is) throws Exception{
        //XSSFWorkbook表示整个excel对象
        XSSFWorkbook XSSFWorkbook = new XSSFWorkbook(is);
        List<List<String>> result = new ArrayList<List<String>>();

        //获取excel中的每一页 XSSFSheet对象
        for(int numSheet = 0; numSheet < XSSFWorkbook.getNumberOfSheets(); numSheet++){
            XSSFSheet XSSFSheet = XSSFWorkbook.getSheetAt(numSheet);
            if(XSSFSheet == null) {
                continue;
            }
            //如果不为空 处理当前sheet  获取每一行 XSSFRow对象
            List<String> rowList;
            //获取标题行列数
            int lastCellNum = 0;
            //1.获取标题行
            XSSFRow XSSFRow = XSSFSheet.getRow(0);
            if(XSSFRow != null){
                rowList = new ArrayList<String>();
                //获取标题行列数
                lastCellNum = XSSFRow.getLastCellNum();
                //获取标题行的每一列 cell对象
                for(int i = 0; i < lastCellNum; i++){
                    XSSFCell cell = XSSFRow.getCell(i);
                    if(cell == null) {
                        rowList.add(null);
                        continue;
                    } else{
                        //如果cell != null, 设置取值格式为String类型
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String cellVal = cell.getStringCellValue();
                        rowList.add(cellVal);
                    }
                }
                result.add(rowList);
            }
            //2.获取其他行
            for(int rowNum =1; rowNum <= XSSFSheet.getLastRowNum(); rowNum++ ){
                XSSFRow = XSSFSheet.getRow(rowNum);
                if(XSSFRow == null){
                    continue;
                }
                rowList = new ArrayList<String>();
                //获取每一列 cell对象
                for(int i = 0; i < lastCellNum; i++){
                    XSSFCell cell = XSSFRow.getCell(i);
                    if(cell == null) {
                        rowList.add(null);
                        continue;
                    }
                    //如果cell != null, 设置取值格式为String类型
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    String cellVal = cell.getStringCellValue();
                    rowList.add(cellVal);
                }
                result.add(rowList);
            }
        }


        return result;
    }

}

  

 

 

 

 

 

 

 

2.解析格式按类型转换:

package com.chx.test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

public class ResolveExcel {
	private List<List<String>> readXls(InputStream is) throws Exception{
		 
		//HSSFWorkbook表示整个excel对象
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
		List<List<String>> result = new ArrayList<List<String>>();
		
		//获取excel中的每一页 hssfSheet对象
		for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
			if(hssfSheet == null) {
				continue;
			}
			//如果不为空 处理当前sheet  获取每一行 hssfRow对象
			List<String> rowList;
			//获取标题行列数
			int lastCellNum = 0;
			//1.获取标题行
			HSSFRow hssfRow = hssfSheet.getRow(0);
			if(hssfRow != null){
				rowList = new ArrayList<String>();
				//获取标题行列数
				lastCellNum = hssfRow.getLastCellNum();
				//获取标题行的每一列 cell对象
				for(int i = 0; i < lastCellNum; i++){
					HSSFCell cell = hssfRow.getCell(i);
					if(cell == null) {
						rowList.add(null);
						continue;
					} else{
						//如果cell != null, 设置取值格式为String类型
						//cell.setCellType(Cell.CELL_TYPE_STRING);
						//String cellVal = cell.getStringCellValue();
						rowList.add(getCellStringValue(cell));
					}
				}
				result.add(rowList);
			}
			//2.获取其他行
			for(int rowNum =1; rowNum <= hssfSheet.getLastRowNum(); rowNum++ ){
				hssfRow = hssfSheet.getRow(rowNum);
				if(hssfRow == null){
					continue;
				}
				rowList = new ArrayList<String>();
				//获取每一列 cell对象
				for(int i = 0; i < lastCellNum; i++){
					HSSFCell cell = hssfRow.getCell(i);
					if(cell == null) {
						rowList.add(null);
						continue;
					}
					
					//如果cell != null, 设置取值格式为String类型
					//cell.setCellType(Cell.CELL_TYPE_STRING);
					//String cellVal = cell.getStringCellValue();
					rowList.add(getCellStringValue(cell));
					
					
					
				}
				result.add(rowList);
			}
		}		
		
		
		return result;
	}
	
	public String getCellStringValue(HSSFCell cell) {      
        String cellValue = "";      
        switch (cell.getCellType()) {      
        case HSSFCell.CELL_TYPE_STRING://字符串类型   
            cellValue = cell.getStringCellValue();      
            if(cellValue.trim().equals("")||cellValue.trim().length()<=0)      
                cellValue=" ";      
            break;      
        case HSSFCell.CELL_TYPE_NUMERIC: //数值类型   
            cellValue = String.valueOf(cell.getNumericCellValue());      
            break;      
        case HSSFCell.CELL_TYPE_FORMULA: //公式   
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);      
            cellValue = String.valueOf(cell.getNumericCellValue());      
            break;      
        case HSSFCell.CELL_TYPE_BLANK:      
            cellValue=" ";      
            break;      
        case HSSFCell.CELL_TYPE_BOOLEAN:      
            break;      
        case HSSFCell.CELL_TYPE_ERROR:      
            break;      
        default:      
            break;      
        }      
        return cellValue;      
    }   
	
	public static void main(String[] args) throws Exception {
		String path = "e:\\employee.xls";
		InputStream is = new FileInputStream(path);
		ResolveExcel resolveExcel = new ResolveExcel();
		List<List<String>> result = resolveExcel.readXls(is);
		for(List<String> list : result){
			for(String str : list){
				System.out.print(str + ",  ");
			}
			System.out.println();
		}
	}
}

  

 

【7.0】操作excel

标签:val   employee   ==   static   mat   usermod   print   system   获取   

原文地址:http://www.cnblogs.com/chxbar/p/7192732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!