栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【java学习】excel读写

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【java学习】excel读写

1,com.alibaba.easyexcel 1)Demo
        
            com.alibaba
            easyexcel
            3.1.1
        
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

public class ExcelListener extends AnalysisEventListener {

    List dataList = new ArrayList<>();
    @Override
    public void invoke(ExcelData excelData, AnalysisContext analysisContext) {        
        dataList.add(networkExcelData);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("读取完毕!");
    }

    public List getDataList(){
        return  this.dataList;
    }
}
@Data
public class ExcelData {

    @ExcelProperty(value = "序号", index = 0)
    private String id;

    @ExcelProperty(value = "名称", index = 1)
    private String name;

    @ExcelProperty(value = "省份", index = 2)
    private String province;
}
        ExcelListener listener = new  ExcelListener();
        EasyExcel.read(EXCEL_PATH, ExcelData.class, listener).sheet(EXCEL_SHEET_NAME).doRead();
        List dataList = listener.getDataList();
2)Apache POI如何获取Excel合并单元格的值
	
	public String getMergedRegionValue(Sheet sheet ,int row , int column){
		int sheetMergeCount = sheet.getNumMergedRegions();
		
		for(int i = 0 ; i < sheetMergeCount ; i++){
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();
			
			if(row >= firstRow && row <= lastRow){
				
				if(column >= firstColumn && column <= lastColumn){
					Row fRow = sheet.getRow(firstRow);
					Cell fCell = fRow.getCell(firstColumn);
					
					return getCellValue(fCell) ;
				}
			}
		}
		
		return null ;
	}
	
	
	public boolean isMergedRegion(Sheet sheet , int row , int column){
		int sheetMergeCount = sheet.getNumMergedRegions();
		
		for(int i = 0 ; i < sheetMergeCount ; i++ ){
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			int firstRow = ca.getFirstRow();
			int lastRow = ca.getLastRow();
			
			if(row >= firstRow && row <= lastRow){
				if(column >= firstColumn && column <= lastColumn){
					
					return true ;
				}
			}
		}
		
		return false ;
	}
	
	
	public String getCellValue(Cell cell){
		
		if(cell == null) return "";
		
		if(cell.getCellType() == Cell.CELL_TYPE_STRING){
			
			return cell.getStringCellValue();
			
		}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){
			
			return String.valueOf(cell.getBooleanCellValue());
			
		}else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
			
			return cell.getCellFormula() ;
			
		}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			
			return String.valueOf(cell.getNumericCellValue());
			
		}
		
		return "";
	}
2,org.apache.poi
        
            org.apache.poi
            poi-ooxml
            4.1.1
        
package com.trans;

import org.apache.poi.ss.usermodel.CellType;
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.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadExcel {

    private File file;
    //k-colIndex Object-value
    private List> valueMaps = new ArrayList<>();

    public ReadExcel(File file) {
        this.file = file;
    }
    public ReadExcel read(){
        try {
            FileInputStream inputStream = new FileInputStream(file);
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet xssfSheet = workbook.getSheetAt(0);
            //不获取表头数据
            for (int rowIndex=Constant.TABLE_NUM; rowIndex <= xssfSheet.getLastRowNum(); rowIndex++){
                getDataFromRow(xssfSheet.getRow(rowIndex));
            }
        }catch (Exception e){
            //文件错误
        }
        return this;
    }
    private void getDataFromRow(XSSFRow row){
        if (isRowEmpty(row)){
            return;
        }
        Map valueMap = new HashMap<>();
        for(int i=0; i
            if (!isCellEmpty(row.getCell(i))) {
                valueMap.put(i, getLabelCellValue(row.getCell(i)));
            }
        }
        valueMaps.add(valueMap);
    }

    private boolean isRowEmpty(XSSFRow row) {
        if (null == row) {
            return true;
        }
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            XSSFCell cell = row.getCell(i);
            if (null != cell && cell.getCellTypeEnum() != CellType.BLANK) {
                return false;
            }
        }
        return true;
    }

    private boolean isCellEmpty(XSSFCell cell) {
        return null == cell || cell.toString().length() == 0;
    }

    public List> getValueMaps() {
        return valueMaps;
    }

    private String getLabelCellValue(XSSFCell xssfCell) {
        String cellValue;
        if (null != xssfCell) {
            //判断cell类型
            if (xssfCell.getCellTypeEnum() == CellType.NUMERIC || xssfCell.getCellTypeEnum() == CellType.FORMULA) {
                if (new Double(xssfCell.getNumericCellValue()).intValue() == new Double(xssfCell.getNumericCellValue())) {
                    cellValue = String.valueOf(new Double(xssfCell.getNumericCellValue()).intValue());
                } else {
                    cellValue = xssfCell.getNumericCellValue().toString();
                }
            } else if (xssfCell.getCellTypeEnum() == CellType.BOOLEAN) {
                cellValue = String.valueOf(xssfCell.getBooleanCellValue());
            } else if (xssfCell.getCellTypeEnum() == CellType.STRING) {
                cellValue = xssfCell.getStringCellValue();
            } else {
                cellValue = xssfCell.toString();
            }

        } else {
            cellValue = "";
        }
        return cellValue.trim();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029035.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号