7.0操作excel

Posted chxbar.cn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7.0操作excel相关的知识,希望对你有一定的参考价值。

 

 

 

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的主要内容,如果未能解决你的问题,请参考以下文章

尝试将 Vlookup 片段添加到我的 Excel 宏

What's new in C# 7.0

力软敏捷开发框架7.0版本更新资料

What's new in C# 7.0

使用While循环导出Excel

如何用java导入Excel数据到数据库?