Java后台代码实现POI文件的导入导出

前言

工作中常常会用到POI的导入导出功能,今天为大家详细介绍一下平时用到的最多的导入Excel表格数据和导出数据为Excel表格的相关代码操作!本案例是SpringBoot项目,废话不多说上代码!

1.Controller层代码

//相关导包
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.baidu.entity.Student;
import com.baidu.mapper.StudentMapper;
import com.baidu.utils.ExcelUtils;
import com.baidu.utils.FileUtils;

@Controller
public class StudentController {

	@Autowired
	private StudentMapper studentMapper;

	//导出数据到Excel表格内
	@RequestMapping("/exportExcel")
	public void exportExcel(HttpServletResponse response,Student stu){
		//查询要导出数据集合
		List<Student> list = studentMapper.getExcelList();
		String [] columnNames= {"编号","姓名","性别","生日"};
		String [] columns= {"sid","sname","sex","birthday"};
		ExcelUtils.exportExcel(response, list, columnNames, columns, "工作簿", "汇总");
	}
	
	//将Excel表格信息导入到数据库
	@RequestMapping("/uploadFile")
	@ResponseBody
	public boolean uploadFile(HttpServletRequest request,Student stu,MultipartFile myfile,HttpServletResponse response) {
		try {
			//调用工具类FileUtils上传,前台传来的文件形参为myfile
			String newFileName = FileUtils.upload(myfile, request);
			String realPath = request.getSession().getServletContext().getRealPath("file");
			//将Excel文件的内容放入到String类型的双层数组中
			String[][] strings = ExcelUtils.readexcell(realPath+File.separator+newFileName, 1);
			//循环数组,依次拿取数组内的数据放入到实体类的对象里,以便于添加数据到数据库的表单内
			for (int i = 0; i < strings.length; i++) {
				String [] stuData=strings[i];
				Student user=new Student();
				//从索引为1的开始拿数据,因为Excel表格内第一行是主键id,添加数据不需要id,不过如果在这写了也无所谓,没影响,添加的sql注意就好了.
				user.setSname(stuData[1]);
				user.setSex(stuData[2]);
				user.setBirthday(stuData[3]);
				studentMapper.addStu(user);
			}
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}	
}

2.用到的几个工具类

总共用到了三个工具类,工具类里面的方法可能不是都有用,不过我记不清楚用的哪几个了,就全部复制过来了,有兴趣可以仔细研究下,不过工具类,粘贴拿过去用就好了,多几个方法也问题不大.

第一个:ExcelUtils

//工具类所在包名
package com.XXX.utils;
//导包
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.poifs.filesystem.POIFSFileSystem;
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.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
 * poi操作excel
 * @author Administrator
 */
public class ExcelUtils {
	/**
	 * ***************导入文件,读取导入的文件内容***********
	 * (3:读取excel数据)读取excel文件的数据信息
	 * ExcelUtils.readexcell(文件路径+File.separator+newFileName, 1)
	 * 文件名的拼接:路径+/+新的文件名字,1
	 * @param filepath 文件路径
	 * @param startrow 读取的开始行
	 * @Result 返回一个二维数组(第一维放的是行,第二维放的是列表)
	 * @throws Exception
	 */
	public static String[][] readexcell(String filepath,int startrow) throws Exception{
		// 判断文件是否存在
        File file = new File(filepath);
        if (!file.exists()) {
            throw new IOException("文件" + filepath + "W不存在!");
        }
		//获取sheet
        Sheet sheet = getSheet(filepath);
		// 得到总行数
		int rowNum = sheet.getLastRowNum()+1;
		// 根据第一行获取列数
		Row row = sheet.getRow(0);
		//获取总列数
		int colNum = row.getPhysicalNumberOfCells();
		//根据行列创建二维数组
		String[][] content = new String[rowNum-startrow][colNum];
		String[] cols = null;
		//通过循环,给二维数组赋值
		for (int i = startrow; i < rowNum; i++) {
			row = sheet.getRow(i);
			cols = new String[colNum];
			for (int j = 0; j < colNum; j++) {
				//获取每个单元格的值
				cols[j] = getCellValue(row.getCell(j));
				//把单元格的值存入二维数组
				content[i - startrow][j] =cols[j];
			}
		}
		return content;
	}
	

	/**
	 * 根据表名获取第一个sheet
	 * @param path d:\\1213.xml
	 * @return 2003-HSSFWorkbook  2007-XSSFWorkbook
	 * @throws Exception 
	 */
	public static Sheet getSheet(String file) throws Exception {
		//文件后缀
		String extension = file.lastIndexOf(".") == -1 ? "" : file.substring(file.lastIndexOf("."));
		//创建输入流
		InputStream is = new FileInputStream(file);
		if (".xls".equals(extension)) {//2003
			//获取工作薄
			POIFSFileSystem fs = new POIFSFileSystem(is);
			return new HSSFWorkbook(fs).getSheetAt(0);
		} else if (".xlsx".equals(extension) || ".xlsm".equals(extension)) {
			return new XSSFWorkbook(is).getSheetAt(0);
		} else {
			throw new IOException("文件(" + file + "),无法识别!");
		}
	}
	  /**
     * 功能:获取单元格的值
     */
    public static String getCellValue(Cell cell) {
        Object result = "";
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                result = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
             // 在excel里,日期也是数字,在此要进行判断 
				if(HSSFDateUtil.isCellDateFormatted(cell)){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					 Date date = cell.getDateCellValue();  
					 result =  sdf.format(date); 
				}else{
					DecimalFormat df=new DecimalFormat("#"); 
					result=df.format(cell.getNumericCellValue());
				}
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                result = cell.getBooleanCellValue();
                break;
            case Cell.CELL_TYPE_FORMULA:
                result = cell.getCellFormula();
                break;
            case Cell.CELL_TYPE_ERROR:
                result = cell.getErrorCellValue();
                break;
            case Cell.CELL_TYPE_BLANK:
                break;
            default:
                break;
            }
        }
        return result.toString();
    }
    /**
     * 导出
     * 根据传入List数据集合导出Excel表格 生成本地excel
     * @param file (输出流路径)d:\\123.xml
     * @param list 任何对象类型的list(数据库直接查询出的)User(id,name,age,sex)
     * @param columnNames(表头名称)(姓名、性别、年龄)
     * @param columns (表头对应的列名)(name,sex,age)注意顺序
     * @param sheetName(sheet名称)
     */
 	@SuppressWarnings("rawtypes")
 	public static void exportExcelByList(String file, List list,String[] columnNames, String[] columns, String sheetName) {
 		OutputStream fos  =null;
 		try {
			//获取输出流
 			fos= new FileOutputStream(file);
			//创建工作薄HSSFWorkbook
 			HSSFWorkbook wb = new HSSFWorkbook();
			//创建表单sheet
 			HSSFSheet sheet = wb.createSheet(sheetName);
			//创建样式对象
 			HSSFCellStyle style = wb.createCellStyle(); // 样式对象
 			//style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
 			//style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平

			//创建行--表头
 			HSSFRow row = sheet.createRow(0);
 			for (int i = 0; i < columnNames.length; i++) {
				//创建列、单元格
 				HSSFCell cell = row.createCell(i);
 				cell.setCellValue(columnNames[i]);
 				cell.setCellStyle(style);
 			}
			//创建数据列
 			for (int i = 0; i < list.size(); i++) {
 				Object o = list.get(i);
				//创建行--数据
 				HSSFRow listRow = sheet.createRow(i + 1);
				//循环列字段数组
 				for (int j = 0; j < columns.length; j++) {
					//创建列
 					HSSFCell listCell = listRow.createCell(j);
 					//根据反射调用方法
 					Method m = o.getClass().getMethod("get" + upperStr(columns[j]));
 					String value =  (String) m.invoke(o);
 					if (value != null) {
 						listCell.setCellValue(value);
 						listCell.setCellStyle(style);
 					} else {
 						listCell.setCellValue("");
 						listCell.setCellStyle(style);
 					}
 					sheet.autoSizeColumn(j+1, true);//自适应,从1开始
 				}
 			}
			//把工作薄写入到输出流
 			wb.write(fos);
 			System.out.println("生成excel成功:"+file);
 		} catch (Exception e) {
 			e.printStackTrace();
 		}finally {
 			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
 	}

 	/**
 	 * *************首先此方法作为导出xecel文件的方法**************
 	 * 根据传入List数据集合导出Excel表格 返回页面选择保存路径的excel
 	 * @param response (响应页面)
 	 * @param list 查询的一个数据集合列表
 	 * @param columnNames 导出后的excel文件的表头
 	 * @param columns 对应数据库里的属性名
 	 * @param sheetName 工作簿的名字
 	 * @param filename  导出的文件名
 	 */
 	@SuppressWarnings("rawtypes")
 	public static void exportExcel(HttpServletResponse response,List list, String[] columnNames, String[] columns,String sheetName, String filename) {
 		OutputStream fos = null;
 		try {
			//响应输出流,让用户自己选择保存路径
 			fos = getOutputStream(response, filename);

 			HSSFWorkbook wb = new HSSFWorkbook();
 			HSSFSheet sheet = wb.createSheet(sheetName);
 			HSSFCellStyle style = wb.createCellStyle(); // 样式对象
 			//style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
 			//style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平

 			HSSFRow row = sheet.createRow(0);
 			for (int i = 0; i < columnNames.length; i++) {
 				HSSFCell cell = row.createCell(i);
 				cell.setCellValue(columnNames[i]);
 				cell.setCellStyle(style);
 			}
 			for (int i = 0; i < list.size(); i++) {
 				HSSFRow listRow = sheet.createRow(i + 1);
 				Object o = list.get(i);
 				for (int j = 0; j < columns.length; j++) {
 					HSSFCell listCell = listRow.createCell(j);
 					Method m = o.getClass().getMethod("get" + upperStr(columns[j]));
 					Object value =  m.invoke(o);
 					if (value != null) {
 						listCell.setCellValue(value + "");
 						listCell.setCellStyle(style);
 					} else {
 						listCell.setCellValue("");
 						listCell.setCellStyle(style);
 					}
 					sheet.autoSizeColumn(j+1, true);//自适应,从1开始
 				}
 			}
 			wb.write(fos);
 		} catch (Exception e) {
 			e.printStackTrace();
 		} finally {
 			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
 	}

 	/**
 	 * 根据response获取输出流
 	 * 2017年9月14日下午5:19:34
 	 * @param response
 	 * @param filename 生成文件的文件名称
 	 * @return
 	 * @throws UnsupportedEncodingException
 	 * @throws IOException
 	 */
	public static OutputStream getOutputStream(HttpServletResponse response, String filename){
		OutputStream fos = null;
		response.setCharacterEncoding("UTF-8");
		response.reset();//清除缓存
		//response.setContentType("octets/stream");
		response.setContentType("application/force-download");// 设置强制下载不打开

		try {
			response.addHeader("Content-Disposition", "attachment;filename="+ new String((filename).getBytes("UTF-8"), "iso8859-1")+ ".xls");
			fos = response.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fos;
	}
 	/**
 	 * 把输入字符串的首字母改成大写
 	 * 
 	 * @param str
 	 * @return
 	 */
 	private static String upperStr(String str) {
 		char[] ch = str.toCharArray();
 		if (ch[0] >= 'a' && ch[0] <= 'z') {
 			ch[0] = (char) (ch[0] - 32);
 		}
 		return new String(ch);
 	}
 	public static void exportBigData(HttpServletResponse response,List list, String[] columnNames, String[] columns,String sheetName, String filename){

		OutputStream os = null;
		try {
			response.setContentType("application/force-download"); // 设置下载类型
			response.setHeader("Content-Disposition","attachment;filename=" + filename); // 设置文件的名称
			os = response.getOutputStream(); // 输出流
			SXSSFWorkbook wb = new SXSSFWorkbook(1000);//内存中保留 1000 条数据,以免内存溢出,其余写入 硬盘
	        //获得该工作区的第一个sheet   
			Sheet sheet1 = wb.createSheet(sheetName); 
	        int excelRow = 0;
	        //标题行
	        Row titleRow = (Row) sheet1.createRow(excelRow++);
			for (int i = 0; i < columns.length; i++) {
				Cell cell = titleRow.createCell(i);  
                cell.setCellValue(columns[i]);
			}
			
			if (list!= null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					//明细行
			        Row contentRow = (Row) sheet1.createRow(excelRow++);
					List<String> reParam = (List<String>) list.get(i);
					for (int j = 0; j < reParam.size(); j++) {
						Cell cell = contentRow.createCell(j);  
		                cell.setCellValue(reParam.get(j));
					}
				}
			}
			wb.write(os);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			} // 关闭输出流
		}
 	}

}

第二个:FileUtils

//工具类所在包名
package com.XXX.utils;
//导包
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

public class FileUtils {

	//文件上传功能调用(1:上传文件FileUtils.upload)
	public static String upload(MultipartFile myfile,HttpServletRequest request) throws IllegalStateException, IOException {
				//获取源文件的名称 和类型 例如 abc.jpg;
				String oldname = myfile.getOriginalFilename();
				String fileType = oldname.substring(oldname.lastIndexOf("."));
				//文件重新命名,定义新文件名
				String newName = UUID.randomUUID().toString()+fileType;
				//定义文件上传路径
				//获取当前项目的根路径
				//D:\stsworkspace\fileUpDown\src\main\webapp
				//(2:获取文件路径)String path =request.getSession().getServletContext().getRealPath("file");
				String path =request.getSession().getServletContext().getRealPath("file");
				//String path ="D:\\stsworkspace\\fileUpDown\\src\\main\\webapp\\files";
				//创建文件
				//(3:路径+/+文件名,1)
				File file = new File(path+File.separator+newName);
				//完成文件复制
				myfile.transferTo(file);
				return newName;
	}
	
	//文件下载功能
	public  static void downLoad(String fileName,HttpServletRequest request,HttpServletResponse response) throws IOException {
		//设置http传输格式
				//设置下载的文件名
				response.setContentType("application/force-download");// 设置强制下载不打开
				// 设置文件名
			 	response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
				//获取文件路径
				String path = request.getServletContext().getRealPath("file/image");
				//通过文件名获取文件,更加文件名 加文件的路径 创建文件对象
				File file = new File(path+File.separator+fileName);
				//获取输入流对象,
				InputStream is = new FileInputStream(file);
				//获取输出流对象
				ServletOutputStream outputStream = response.getOutputStream();
				//输入流转换为输出流对象
				int b;
				while((b=is.read())!=-1) {
					outputStream.write(b);
				}
				outputStream.close();
	}
}

你可能感兴趣的:(随笔)