Java——导入

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/whm18322394724/article/details/82730216

  最近做了一些简单的导入导出,也遇到了一些问题,下边简单记录一下。

工具类

package com.jdy.core.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
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.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddressList;


/* 
 * ExcelUtil工具类实现功能: 
 * 导出时传入list<T>,即可实现导出为一个excel,其中每个对象T为Excel中的一条记录. 
 * 导入时读取excel,得到的结果是一个list<T>.T是自己定义的对象. 
 * 需要导出的实体对象只需简单配置注解就能实现灵活导出,通过注解您可以方便实现下面功能: 
 * 1.实体属性配置了注解就能导出到excel中,每个属性都对应一列. 
 * 2.列名称可以通过注解配置. 
 * 3.导出到哪一列可以通过注解配置. 
 * 4.鼠标移动到该列时提示信息可以通过注解配置. 
 * 5.用注解设置只能下拉选择不能随意填写功能. 
 * 6.用注解设置是否只导出标题而不导出内容,这在导出内容作为模板以供用户填写时比较实用. 
 * 本工具类以后可能还会加功能,请关注我的博客: http://blog.csdn.net/lk_blog 
 */  
public  class ExcelUtil<T> {

	Class<T> clazz;

	public ExcelUtil(Class<T> clazz) {
		this.clazz = clazz;
	}

	public   List<T> importExcel(String sheetName, InputStream input) {
		int maxCol = 0;
		List<T> list = new ArrayList<T>();
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(input);
			HSSFSheet sheet = workbook.getSheet(sheetName);
			if (!sheetName.trim().equals("")) {
				sheet = workbook.getSheet(sheetName);// 如果指定sheet名,则取指定sheet中的内容.
			}
			if (sheet == null) {
				sheet = workbook.getSheetAt(0); // 如果传入的sheet名不存在则默认指向第1个sheet.
			}
			int rows = sheet.getPhysicalNumberOfRows();

			if (rows > 0) {// 有数据时才处理
				// Field[] allFields = clazz.getDeclaredFields();// 得到类的所有field.
				List<Field> allFields = getMappedFiled(clazz, null);

				Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();// 定义一个map用于存放列的序号和field.
				for (Field field : allFields) {
					// 将有注解的field存放到map中.
					if (field.isAnnotationPresent(ExcelVOAttribute.class)) {
						ExcelVOAttribute attr = field.getAnnotation(ExcelVOAttribute.class);
						int col = getExcelCol(attr.column());// 获得列号
						maxCol = Math.max(col, maxCol);
						// System.out.println(col + "====" + field.getName());
						field.setAccessible(true);// 设置类的私有字段属性可访问.
						fieldsMap.put(col, field);
					}
				}
				
				for (int i = 1; i < rows; i++) {// 从第2行开始取数据,默认第一行是表头.
					HSSFRow row = sheet.getRow(i);
					// int cellNum = row.getPhysicalNumberOfCells();
					// int cellNum = row.getLastCellNum();
					int cellNum = maxCol;
					T entity = null;
					//System.err.println("cellNum="+cellNum);
					for (int j = 0; j <= cellNum; j++) {
						//System.err.println("j="+j);
						if (row==null) {
							continue;
						}
						HSSFCell cell = row.getCell(j);
						if (cell == null) {
							continue;
						}
						int cellType = cell.getCellType();
						String c = "";
						Integer val;
						//System.err.println("cellType="+cellType);
						if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
							val=(int)cell.getNumericCellValue();
							c = String.valueOf(val);
						} else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
							c = String.valueOf(cell.getBooleanCellValue());
						} else {
							c = cell.getStringCellValue();
						}
						//System.err.println("c="+c);
						if (c == null || c.equals("")) {
							continue;
						}
						
						entity = (entity == null ? clazz.newInstance() : entity);// 如果不存在实例则新建.
						// System.out.println(cells[j].getContents());
						Field field = fieldsMap.get(j);// 从map中得到对应列的field.
						if (field == null) {
							continue;
						}
						// 取得类型,并根据对象类型设置值.
						Class<?> fieldType = field.getType();
						//System.err.println("fieldType="+fieldType.toString());
						if (String.class == fieldType) {
							field.set(entity, String.valueOf(c));
						} else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) {
							field.set(entity, Integer.valueOf(c));
						} else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) {
							field.set(entity, Long.valueOf(c));
						} else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) {
							field.set(entity, Float.valueOf(c));
						} else if ((Short.TYPE == fieldType) || (Short.class == fieldType)) {
							field.set(entity, Short.valueOf(c));
						} else if ((Double.TYPE == fieldType) || (Double.class == fieldType)) {
							field.set(entity, Double.valueOf(c));
						} else if (Character.TYPE == fieldType) {
							if ((c != null) && (c.length() > 0)) {
								field.set(entity, Character.valueOf(c.charAt(0)));
							}
						}else{
							System.out.println("未知的字段数据类型fieldType="+fieldType.getClass().toString());
						}

					}
					if (entity != null) {
						list.add(entity);
					}
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 对list数据源将其里面的数据导入到excel表单
	 * 
	 * @param sheetName
	 *            工作表的名称
	 * @param output
	 *            java输出流
	 */
	@SuppressWarnings("resource")
	public boolean exportExcel(List<T> lists[], String sheetNames[], OutputStream output) {
		if (lists.length != sheetNames.length) {
			System.out.println("数组长度不一致");
			return false;
		}

		HSSFWorkbook workbook = new HSSFWorkbook();// 产生工作薄对象

		for (int ii = 0; ii < lists.length; ii++) {
			List<T> list = lists[ii];
			String sheetName = sheetNames[ii];

			List<Field> fields = getMappedFiled(clazz, null);

			HSSFSheet sheet = workbook.createSheet();// 产生工作表对象

			workbook.setSheetName(ii, sheetName);

			HSSFRow row;
			HSSFCell cell;// 产生单元格
			HSSFCellStyle style = workbook.createCellStyle();
			style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
			style.setFillBackgroundColor(HSSFColor.GREY_40_PERCENT.index);
			row = sheet.createRow(0);// 产生一行
			// 写入各个字段的列头名称
			for (int i = 0; i < fields.size(); i++) {
				Field field = fields.get(i);
				ExcelVOAttribute attr = field.getAnnotation(ExcelVOAttribute.class);
				int col = getExcelCol(attr.column());// 获得列号
				cell = row.createCell(col);// 创建列
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 设置列中写入内容为String类型
				cell.setCellValue(attr.name());// 写入列名

				// 如果设置了提示信息则鼠标放上去提示.
				if (!attr.prompt().trim().equals("")) {
					setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, col, col);// 这里默认设了2-101列提示.
				}
				// 如果设置了combo属性则本列只能选择不能输入
				if (attr.combo().length > 0) {
					setHSSFValidation(sheet, attr.combo(), 1, 100, col, col);// 这里默认设了2-101列只能选择不能输入.
				}
				cell.setCellStyle(style);
			}

			int startNo = 0;
			int endNo = list.size();
			// 写入各条记录,每条记录对应excel表中的一行
			for (int i = startNo; i < endNo; i++) {
				row = sheet.createRow(i + 1 - startNo);
				T vo = (T) list.get(i); // 得到导出对象.
				for (int j = 0; j < fields.size(); j++) {
					Field field = fields.get(j);// 获得field.
					field.setAccessible(true);// 设置实体类私有属性可访问
					ExcelVOAttribute attr = field.getAnnotation(ExcelVOAttribute.class);
					try {
						// 根据ExcelVOAttribute中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
						if (attr.isExport()) {
							cell = row.createCell(getExcelCol(attr.column()));// 创建cell
							cell.setCellType(HSSFCell.CELL_TYPE_STRING);
							cell.setCellValue(field.get(vo) == null ? "" : String.valueOf(field.get(vo)));// 如果数据存在就填入,不存在填入空格.
						}
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
		}

		try {
			output.flush();
			workbook.write(output);
			output.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Output is closed ");
			return false;
		}

	}

	/**
	 * 对list数据源将其里面的数据导入到excel表单
	 * 
	 * @param sheetName
	 *            工作表的名称
	 * @param sheetSize
	 *            每个sheet中数据的行数,此数值必须小于65536
	 * @param output
	 *            java输出流
	 */
	@SuppressWarnings("unchecked")
	public boolean exportExcel(List<T> list, String sheetName, OutputStream output) {
		// 此处 对类型进行转换
		List<T> ilist = new ArrayList<>();
		for (T t : list) {
			ilist.add(t);
		}
		List<T>[] lists = new ArrayList[1];
		lists[0] = ilist;

		String[] sheetNames = new String[1];
		sheetNames[0] = sheetName;

		return exportExcel(lists, sheetNames, output);
	}

	/**
	 * 将EXCEL中A,B,C,D,E列映射成0,1,2,3
	 * 
	 * @param col
	 */
	public static int getExcelCol(String col) {
		col = col.toUpperCase();
		// 从-1开始计算,字母重1开始运算。这种总数下来算数正好相同。
		int count = -1;
		char[] cs = col.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			count += (cs[i] - 64) * Math.pow(26, cs.length - 1 - i);
		}
		return count;
	}

	/**
	 * 设置单元格上提示
	 * 
	 * @param sheet
	 *            要设置的sheet.
	 * @param promptTitle
	 *            标题
	 * @param promptContent
	 *            内容
	 * @param firstRow
	 *            开始行
	 * @param endRow
	 *            结束行
	 * @param firstCol
	 *            开始列
	 * @param endCol
	 *            结束列
	 * @return 设置好的sheet.
	 */
	public static HSSFSheet setHSSFPrompt(HSSFSheet sheet, String promptTitle, String promptContent, int firstRow, int endRow, int firstCol, int endCol) {
		// 构造constraint对象
		DVConstraint constraint = DVConstraint.createCustomFormulaConstraint("DD1");
		// 四个参数分别是:起始行、终止行、起始列、终止列
		CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
		// 数据有效性对象
		HSSFDataValidation data_validation_view = new HSSFDataValidation(regions, constraint);
		data_validation_view.createPromptBox(promptTitle, promptContent);
		sheet.addValidationData(data_validation_view);
		return sheet;
	}

	/**
	 * 设置某些列的值只能输入预制的数据,显示下拉框.
	 * 
	 * @param sheet
	 *            要设置的sheet.
	 * @param textlist
	 *            下拉框显示的内容
	 * @param firstRow
	 *            开始行
	 * @param endRow
	 *            结束行
	 * @param firstCol
	 *            开始列
	 * @param endCol
	 *            结束列
	 * @return 设置好的sheet.
	 */
	public static HSSFSheet setHSSFValidation(HSSFSheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
		// 加载下拉列表内容
		DVConstraint constraint = DVConstraint.createExplicitListConstraint(textlist);
		// 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
		CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
		// 数据有效性对象
		HSSFDataValidation data_validation_list = new HSSFDataValidation(regions, constraint);
		sheet.addValidationData(data_validation_list);
		return sheet;
	}

	/**
	 * 得到实体类所有通过注解映射了数据表的字段
	 * 
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private List<Field> getMappedFiled(Class clazz, List<Field> fields) {
		if (fields == null) {
			fields = new ArrayList<Field>();
		}

		Field[] allFields = clazz.getDeclaredFields();// 得到所有定义字段
		// 得到所有field并存放到一个list中.
		for (Field field : allFields) {
			if (field.isAnnotationPresent(ExcelVOAttribute.class)) {
				fields.add(field);
			}
		}
		if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class)) {
			getMappedFiled(clazz.getSuperclass(), fields);
		}

		return fields;
	}
	
	
	public static void main(String[] args) {
		
		 // 初始化数据  
        List<StudentVO> list = new ArrayList<StudentVO>();  
  
        StudentVO vo = new StudentVO();  
        vo.setId(1);  
        vo.setName("李坤");  
        vo.setAge(26);  
        vo.setClazz("五期提高班");  
        vo.setCompany("天融信");  
        list.add(vo);  
  
        FileOutputStream out = null;  
        try {  
            out = new FileOutputStream("G:\\success3.xls");  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
        ExcelUtil<StudentVO> util = new ExcelUtil<StudentVO>(StudentVO.class);// 创建工具类.  
        util.exportExcel(list, "学生信息", out);// 导出  
        System.out.println("----执行完毕----------");
	}


}

html

1.
  

<form id="upload" class="batchImport fr" enctype="multipart/form-data" method="post">
 <div class="row cl">
    <div class="formControls col-5">
        <input name="excelPath" id="excelPath" type="file" accept="application/msexcel" />
    </div>
    <div class="col-4"></div>
 </div>   
 <div class="row cl mt-30 mb-30">
    <div class="col-6 col-offset-3">
        <input type="button" class="fr btn btn-primary radius" id="importExcel" value="导入"/>
        <input class="btn btn-default layer_cloce" type="button" value="&nbsp;&nbsp;取消&nbsp;&nbsp;">
    </div>
 </div>
</form>

js

  $(function () {
        $("#importExcel").bind("click",function(){
            debugger;
            var excelPath = $("#excelPath").val();
            var roomId=$("#roomId").val();
            if(excelPath == null || excelPath == ''){
                alert("请选择要上传的Excel文件");
                return;
            }else{
                var fileExtend = excelPath.substring(excelPath.lastIndexOf('.')).toLowerCase();
                if(fileExtend == '.xls'){
                    $("#upload").ajaxSubmit({
                        url:"${ctx}/admin/position/import.action?roomId=roomId",
                        success: function(ret) {
                            layer.msg(ret.msg);
                            parent.window.location.reload();
                            $('#upload')[0].reset()
                        },
                        error:function(ret){
                            layer.msg(ret.msg);
                            alert("error");
                            $('#upload')[0].reset()
                        }

                    });
                }else{
                    alert("文件格式需为'.xls'格式");
                    return;
                }
            }
        });
    });

Controller

	@RequestMapping("/import")
	@ResponseBody
	public CommonStatus importExcel(@RequestParam(value = "excelPath", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response,String roomId) {
		String test=roomId;

		boolean isAllow = false;
		String fileName = file.getOriginalFilename();
		int index = fileName.lastIndexOf(".");
		if (index < 0) {
			return faile("非法文件");
		}
		String suffux = fileName.substring(index);
		for (String s : WebConstant.ALLOW_IMPORT) {
			if (s.equals(suffux)) {
				isAllow = true;
				break;
			}
		}
		if (!isAllow) {
			return faile("非法文件");
		}

		int num = 0;
		try {

			FileInputStream fis = (FileInputStream) file.getInputStream();
			ExcelUtil<Position> util = new ExcelUtil<Position>(Position.class);
			List<Position> list = util.importExcel("Sheet1", fis);
			for (Position p : list) {
				if (p == null) {
					continue;
				}
				//判断文件名称是否已经有了

				// 写入文件

				p.setId(UUID.randomUUID().toString().substring(5));
				//如果导入的这张表关联了其他表,可以做如下操作
				if(StringUtils.isNotEmpty(roomId)){
					Room room=roomService.selectById(roomId);
					p.setRoomId(roomId);
				}

				int i = service.insertSelective(p);
				num += i;

			}
		} catch (IOException e) {
			log.info("导入excel异常e={}", e);
			return faile("导入异常");
		}

		log.info("成功导入数据行数:" + num);

		String path = request.getSession().getServletContext().getRealPath("excel");
		SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
		String urlnew = sdf.format(new Date());
		fileName = urlnew + fileName;
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}

		// 保存
		try {
			file.transferTo(targetFile);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("保存文件失败e=", e);
		}

		return success("导入成功");
	}

实体

package com.jdy.model;

import com.jdy.core.model.BaseEntity;
import com.jdy.core.util.ExcelVOAttribute;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import java.util.Date;

@SuppressWarnings("serial")
public class Position extends BaseEntity {
	@ExcelVOAttribute(name = "工位", column = "A")
	private String no;
	
	@ExcelVOAttribute(name = "桌号", column = "B")
	private String deskNo;
	
	@ExcelVOAttribute(name = "椅号", column = "C")
	private String chairNo;
	
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getDeskNo() {
		return deskNo;
	}
	public void setDeskNo(String deskNo) {
		this.deskNo = deskNo;
	}
	public String getChairNo() {
		return chairNo;
	}
	public void setChairNo(String chairNo) {
		this.chairNo = chairNo;
	}
	
}

1.
  

猜你喜欢

转载自blog.csdn.net/whm18322394724/article/details/82730216