excelimportimpl.java

来自「这是一个用java三层框架做的ISS学员管理系统」· Java 代码 · 共 86 行

JAVA
86
字号
package org.pontifex.commons.fileupload;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.pontifex.util.StringUtil;

public class ExcelImportImpl implements ExcelImport {
	
	private Excel workbook;
	
	private File file ;
	
	Map error ;
	
	public ExcelImportImpl(){
		
	}
	
	public ExcelImportImpl(File file){
		this.file = file ;
	}
	

	public boolean isExcelFile() throws ExcelException, IOException {
		if(file==null||!file.exists())
			throw new ExcelException("The file is Empty !");
		if(file.isDirectory())
			return false;
		String fileName = file.getName();
		if(StringUtil.isEmpty(fileName))
			throw new ExcelException("The file name is Empty !");
		String aa = fileName.substring(fileName.lastIndexOf("."));
		if(aa.equals(".xls"))return true;
		return false;
	}
	
	private boolean isExcelFile(File file) throws ExcelException, IOException{
		if(read(file))
			return isExcelFile();
		return false;
	}

	public boolean read(File inputfile) throws IOException {
		if(inputfile==null||!inputfile.exists())
			return false;
		file = inputfile ;
		return true;
	}
	
	public Excel processExcel() throws ExcelException, IOException{
		return processExcel(file);
	}
	
	private Excel processExcel(File file) throws ExcelException, IOException{
		if(!isExcelFile(file))return null;
		InputStream inputStream = null ;
		try{
			inputStream = new FileInputStream(file);	
	        workbook = new Excel(inputStream);
	        error = new HashMap();
	        validate();
	        return workbook;
		}finally{
			if(inputStream!=null)inputStream.close();
		}
	}
	
	private void validate() {
		
	}

	public static void main(String[] args) throws IOException{
		ExcelImport excelImport = new ExcelImportImpl();
		excelImport.read(new File("e:\\新建 Microsoft Excel 工作表.xls"));
		System.out.println(excelImport.isExcelFile());
		Excel excel= excelImport.processExcel();
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?