您的当前位置:首页正文

SSM处理EXCEL

2024-11-27 来源:个人技术集锦

我的代码段是处理教师信息,将教师EXCEL输入数据库

我写了两中代码

第一种,能用,但是小程序访问后台时不能正常访问,原因是  multipart 的配置文件与小程序起冲突,于是我放弃第一种。

第二种,简单,就是从后台接收到普通文件,然后转成multipart文件,然后继续调用之前方法。

jsp 代码段

	<form class="form-inline" id="form_excel" name="form_excel" role="form"
								    
                    action="${pageContext.request.contextPath}/admin/addStudent"
								method="post" enctype="multipart/form-data">
	<div class="form-group" style="padding-top: 2%">
		<label class="sr-only" for="file_excel">文件输入 </label>
        <input type="file" id="file_excel" name="file_excel">
	</div>
		<button type="submit" class="btn btn-default"  style="margin-top: 2%">提交 
         </button>
	</form>

   在与后台进行数据交互时需要添加依赖包

	<dependency>
		<groupId>org.apache.httpcomponents</groupId>
		<artifactId>httpmime</artifactId>
		<version>4.3.2</version>
	</dependency>

开始后台代码

import org.apache.http.entity.ContentType;

这个包
@RequestMapping(value="AddTeacher",method= {RequestMethod.POST})
	public ModelAndView DoExcelTeacher(HttpServletRequest request) throws Exception {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
		List<FileItem> items = new ArrayList<FileItem>();
		try {
			items = upload.parseRequest(request);
		} catch (FileUploadException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		File file = new File("");
		for(FileItem fi : items) {
			File fullFile = new File(new String(fi.getName().getBytes(), "utf-8")); // 解决文件名乱码问题,获得文件内容
			file = new File("/home/wenruo/Desktop/userInfo", fullFile.getName()); // 为文件设置存储路径
			fi.write(file);
		}
		FileInputStream fileInputStream = new FileInputStream(file);
		MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
				ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
		ModelAndView mv = new ModelAndView();
		String readResult =null;
		try {
			//调用ITeacherService 下的方法,完成增加教师
			readResult = iAdminService.addTeacherInfo(multipartFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv = readTeacherInfo();
		mv.addObject("readResult", readResult);//返回信息
		return mv;
	}

addTeacher()接口

public String addTeacherInfo(MultipartFile file) throws Exception;

实现方法

public String addTeacherInfo(MultipartFile file) throws Exception {
		//创建处理EXCEL的类
		ReadTeacherExcel readExcel=new ReadTeacherExcel();
		//解析excel,获取上传的事件单
		List<Teacher> teacherList = null;
		int insertResult = 0;//记录插入数
		String insertMsg = "";
		try {
			teacherList = readExcel.getExcelInfo(file);	//调用函数,获取到装有Teacher对象的teacherList集合

			for(Teacher s :teacherList) {	
				s.setTeacherPassword(Common.eccryptMD5(s.getTeacherPassword()));
				insertResult++;
				System.out.println(s.toString());  //输出每条插入的数据
			}
			iAdminDao.addTeacherInfo(teacherList);	//调用函数,完成写入数据库操作
			if(insertResult ==0) {
				insertMsg = "载入数据库失败";
			}else if(insertResult == teacherList.size()){
				insertMsg = "全部载入数据库";
			}else {
				insertMsg = "部分载入数据库";
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(insertMsg);
			System.err.println("接受excel表格中的数据失败!");
		}
		for(Teacher s : teacherList) {
			System.out.println("打印excel中的数据"+s.toString());
		}

		return insertMsg;
	}

getExcelInfo(file)

核心代码段

package cn.edu.tit.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import cn.edu.tit.bean.Admin;
import cn.edu.tit.bean.Teacher;
/**
 * 读取教师EXCEL表
 * @author 
 * */
public class ReadTeacherExcel {
	// 总行数
	private int totalRows = 0;
	// 总条数
	private int totalCells = 0;
	// 错误信息接收器
	private String errorMsg;
	// 构造方法
	public ReadTeacherExcel() {
	}
	// 获取总行数
	public int getTotalRows() {
		return totalRows;
	}
	// 获取总列数
	public int getTotalCells() {
		return totalCells;
	}
	// 获取错误信息
	public String getErrorInfo() {
		return errorMsg;
	}

	/**
	 * 读EXCEL文件,获取信息集合
	 * 
	 * @param fielName
	 * @return
	 */
	public List<Teacher> getExcelInfo(MultipartFile mFile) {
		String fileName = mFile.getOriginalFilename();// 获取文件名
		List<Teacher> teacherList = new ArrayList<Teacher>();
		try {
			// 验证文件名是否合格
			if (!validateExcel(fileName)) {
				return null;
			}
			// 根据文件名判断文件是2003版本还是2007版本
			boolean isExcel2003 = true;
			if (isExcel2007(fileName)) {
				isExcel2003 = false;
			}
			teacherList = createExcel(mFile.getInputStream(), isExcel2003);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return teacherList;
	}

	/**
	 * 根据excel里面的内容读取信息
	 * @param is    输入流
	 * @param isExcel2003 excel是2003还是2007版本
	 * @throws IOException
	 */
	public List<Teacher> createExcel(InputStream is, boolean isExcel2003) {
		List<Teacher> teacherList = new ArrayList<Teacher>();
		try {
			Workbook wb = null;
			if (isExcel2003) {// 当excel是2003时,创建excel2003
				wb = new HSSFWorkbook(is);
			} else {// 当excel是2007时,创建excel2007
				wb = new XSSFWorkbook(is);
			}
			teacherList = readExcelValue(wb);// 读取Excel里面的信息
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (Teacher teacher2 : teacherList) {
			System.out.println(teacher2.toString());
		}
		return teacherList;
	}

	/**
	 * 读取Excel里面的信息
	 * 
	 * @param wb
	 * @return
	 */
	private List<Teacher> readExcelValue(Workbook wb) {
		// 得到第一个shell
		Sheet sheet = wb.getSheetAt(0);
		// 得到Excel的行数
		this.totalRows = sheet.getPhysicalNumberOfRows();
		// 得到Excel的列数(前提是有行数)
		if (totalRows > 1 && sheet.getRow(0) != null) {
			this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
		}
		List<Teacher> teacherList = new ArrayList<Teacher>();

		/**
		 * 核心主体,处理EXCEL文件,读取excel文件信息
		 * 此处方法为循环处理
		 * 第一层循环为循环行
		 * 第二层循环为循环列
		 * 
		 * 此处对EXCEL表的格式限制为:第一行第一列开始为数据,并且列次递增数据分别为
		 * 工号、教师名、教师密码、教师性别、教育背景、教师职称、教师电话、电子邮箱
		 * */
		// 循环Excel行数

		for (int r = 0; r < totalRows; r++) {
			Row row = sheet.getRow(r);
			if (row == null) {
				continue;
			}				
			Teacher teacher = new Teacher();
			//循环Excel的列
			for (int c = 0; c < this.totalCells; c++) {
				Cell cell = row.getCell(c);
				/**
				 * 切记判空,否则在getvalue()处将报空指针
				 * */
				if(cell == null)
				{
					continue;
				}
				switch (c) {
				case 0:
					teacher.setEmployeeNum(getValue(cell));//设置工号
					break;
				case 1:
					teacher.setTeacherName(getValue(cell));//设置教师名
					break;
				case 2:
					teacher.setTeacherPassword(getValue(cell));//设置教师密码
					break;
				case 3:
					teacher.setTeacherGender(getValue(cell));//设置教师性别
					break;	
				case 4:
					teacher.setEducationBackground(getValue(cell));//设置教育背景
					break;
				case 5:
					teacher.setProfessionalTitles(getValue(cell));//设置教师职称
					break;
				case 6:
					teacher.setTelephone(getValue(cell));//设置教师电话
					break;
				case 7:
					teacher.setEmail(getValue(cell));//设置教师电子邮箱
					break;
				case 8:
					teacher.setFaceImg(null);
					break;
				case 9:
					teacher.setTeacherNickName(null);
					break;
				case 10:
					teacher.setStatus(null);;
					break;
				case 11:
					teacher.setTeacherCategory(null);;
					break;
				default:
					break;
				}
			}
			if(teacher.getEmployeeNum()!=null)
			{
				teacherList.add(teacher);
			}
		}
		return teacherList;
	}

	/**
	 * 获取单元格信息
	 * 数据库存储的均为VARCHAR类型数据
	 * 将所有数据返回均设定为String类型数据
	 */
	private String getValue(Cell cell) {
	    if (cell.getCellType() == cell.CELL_TYPE_BOOLEAN) {
	          // 返回布尔类型的值
	          return String.valueOf(cell.getBooleanCellValue());
	      } else if (cell.getCellType() == cell.CELL_TYPE_NUMERIC) {
	          // 返回数值类型的值
	    	  return NumberToTextConverter.toText(cell.getNumericCellValue());
	      } else {
	          // 返回字符串类型的值
	          return String.valueOf(cell.getStringCellValue());
	      }
	}

	/**
	 * 验证EXCEL文件
	 */
	public boolean validateExcel(String fileName) {
		//文件名为空、不是2003、不是2007
		if (fileName == null || !(isExcel2003(fileName) || isExcel2007(fileName))) {
			errorMsg = "文件名不是excel格式";
			return false;
		}
		return true;
	}

	//是否是2003的excel,返回true是2003
	public static boolean isExcel2003(String fileName) {
		return fileName.matches("^.+\\.(?i)(xls)$");
	}

	//是否是2007的excel,返回true是2007
	public static boolean isExcel2007(String fileName) {
		return fileName.matches("^.+\\.(?i)(xlsx)$");
	} } 

 

显示全文