⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceltomodelimpl.java

📁 一个简单的excel导入到数据库中。的实例。。。看看吧。。。。。。
💻 JAVA
字号:
package com.javayjm.excel.file.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import com.javayjm.excel.config.RuturnConfig;
import com.javayjm.excel.config.RuturnPropertyParam;
import com.javayjm.excel.file.ExcelToModel;

public class ExcelToModelImpl implements ExcelToModel {

	private File excelFile = null;

	private RuturnConfig excelConfig = null;

	private Map valueMap = null;

	private List fixityList = null;

	public ExcelToModelImpl(File excelFile, RuturnConfig excelConfig,
			Map valueMap) {
		this.excelConfig = excelConfig;
		this.excelFile = excelFile;
		this.valueMap = valueMap;
	}

	/*
	 * 对于配置文件中,指定取固定值的属性,先取到List中.无则为空
	 */
	private void setFixity() {
		List fixityList = new ArrayList();
		Map pmap = this.excelConfig.getPropertyMap();
		for (Iterator it = pmap.values().iterator(); it.hasNext();) {
			RuturnPropertyParam propertyBean = (RuturnPropertyParam) it.next();
			if (propertyBean.getFixity().toUpperCase().trim().equals("YES")) {
				fixityList.add(propertyBean);
			}
		}
		this.fixityList = fixityList;
	}

	public List getModelList() {
		this.setFixity();
		List modelList = new ArrayList();
		try {
			Workbook book;

			book = Workbook.getWorkbook(this.excelFile);
			Sheet sheet = book.getSheet(0);

			// editBook = Workbook.createWorkbook(file, book);

			//System.out.println("rows = " + sheet.getRows() + "  cols ="+ sheet.getColumns());
			for (int i = 1; i < sheet.getRows(); i++) {
				Object obj = this.getModelClass(excelConfig.getClassName());
				BeanWrapper bw = new BeanWrapperImpl(obj);
				// 对excel每一列的值进行取值.
				for (int j = 0; j < sheet.getColumns(); j++) {

					// System.out.println("i = " + i + " j =" + j);
					// 取得Excel表头
					String excelTitleName = sheet.getCell(j, 0).getContents()
							.trim();
					// 取得Excel值
					String value = sheet.getCell(j, i).getContents().trim();
					//					
					// 取得配置属性
					RuturnPropertyParam propertyBean = (RuturnPropertyParam) excelConfig
							.getPropertyMap().get(excelTitleName);

					//System.out.println("i = " + i + "  j =" + j + " value ="+ value + " title = " + excelTitleName);

					if (propertyBean != null) {
						System.out.println("propertyName = "
								+ propertyBean.getName());
						// //做出判断,是否需要 Text/Value 值转换.
						if (propertyBean.getCodeTableName().length() > 1) {

							String valueKey = propertyBean.getCodeTableName()
									.trim()
									+ value;
							//System.out.println("valueKey = " + valueKey);
							Object obj1 = this.valueMap.get(valueKey);
							if (obj1 == null) {
								value = "";
							} else {
								value = obj1.toString();
							}
						}
						if (value == null || value.length() < 1) {
							value = propertyBean.getDefaultValue();
						}
						bw.setPropertyValue(propertyBean.getName(), value);

					}

				}

				/*
				 * 设置属性中的固定值.
				 */
				for (Iterator it = this.fixityList.iterator(); it.hasNext();) {
					RuturnPropertyParam propertyBean = (RuturnPropertyParam) it
							.next();
					Object value = this.valueMap.get(propertyBean.getName());
					if (value == null||value.toString().length()<1) {
						value = propertyBean.getDefaultValue();
					} 					
					bw.setPropertyValue(propertyBean.getName(), value);
				}
				modelList.add(obj);
			}

		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return modelList;
	}

	@SuppressWarnings("unused")
	private Object getModelClass(String className) {
		Object obj = null;
		try {
			obj = Class.forName(className).newInstance();
			System.out.println("init Class = " + className);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
}

⌨️ 快捷键说明

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