[Utils]POI实现excel导入导出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Utils]POI实现excel导入导出相关的知识,希望对你有一定的参考价值。
1.分析excel
2.poi工具类
import org.apache.poi.hssf.usermodel.*; 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.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.multipart.MultipartFile; import org.zenzzat.toa.nsfw.user.entity.User; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; /** * excel导入导出工具类 * Created by Zenz. */ public class ExcelUtil { /** * 导出excel格式用户列表 * @param userList 需要导出的用户列表 * @param response 输出响应 */ public static void exportUserExcel(List<User> userList, HttpServletResponse response) { try { //设置文件类型 response.setContentType("application/x-excel"); //设置文件名称,中文用iso-8859-1编码 response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户列表.xls".getBytes(), "ISO-8859-1")); ServletOutputStream outputStream = response.getOutputStream(); //1、创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //1.1、创建合并单元格对象 CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 4);//起始行号,结束行号,起始列号,结束列号 //1.2、头标题样式 HSSFCellStyle headStyle = createCellStyle(workbook, (short) 16); //1.3、列标题样式 HSSFCellStyle colStyle = createCellStyle(workbook, (short) 13); //2、创建工作表 HSSFSheet sheet = workbook.createSheet("用户列表"); //2.1、加载合并单元格对象 sheet.addMergedRegion(cellRangeAddress); //设置默认列宽 sheet.setDefaultColumnWidth(25); //3、创建行 //3.1、创建头标题行;并且设置头标题 HSSFRow headRow = sheet.createRow(0); HSSFCell headCel = headRow.createCell(0); //加载单元格样式 headCel.setCellStyle(headStyle); headCel.setCellValue("用户列表"); //3.2、创建列标题行;并且设置列标题 HSSFRow colRew = sheet.createRow(1); String[] titles = {"用户名", "帐号", "所属部门", "性别", "电子邮箱"}; for (int i = 0; i < titles.length; i++) { HSSFCell colCel = colRew.createCell(i); //加载单元格样式 colCel.setCellStyle(colStyle); colCel.setCellValue(titles[i]); } //4、操作单元格;将用户列表写入excel if (userList != null) { for (int j = 0; j < userList.size(); j++) { HSSFRow row = sheet.createRow(j + 2); HSSFCell cell0 = row.createCell(0); cell0.setCellValue(userList.get(j).getName()); HSSFCell cell1 = row.createCell(1); cell1.setCellValue(userList.get(j).getAccount()); HSSFCell cell2 = row.createCell(2); cell2.setCellValue(userList.get(j).getDept()); HSSFCell cell3 = row.createCell(3); cell3.setCellValue(userList.get(j).getGender() ? "男" : "女"); HSSFCell cell4 = row.createCell(4); cell4.setCellValue(userList.get(j).getEmail()); } } //5、输出 workbook.write(outputStream); workbook.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 创建单元格样式 * @param workbook 工作簿 * @param fontSize 字体大小 * @return 单元格样式 */ private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) { HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //创建字体 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体 font.setFontHeightInPoints(fontSize); //加载字体 style.setFont(font); return style; } /** * 导入excel格式用户列表 * @param userExcel 要导入的excel文件 * @return 读取到用户列表 */ public static List<User> importUserExcel(MultipartFile userExcel) { if (userExcel != null) { String userExcelFileName = userExcel.getOriginalFilename(); //是否是excel if (userExcelFileName.matches("^.+\\\\.(?i)((xls)|(xlsx))$")) { List<User> userList = new ArrayList<>(); try { InputStream inputStream = userExcel.getInputStream(); boolean is03Excel = userExcelFileName.matches("^.+\\\\.(?i)(xls)$"); //1.读取工作簿 Workbook workbook = is03Excel ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream); //2.读取工作表 Sheet sheet = workbook.getSheetAt(0); //3.读取行 if (sheet.getPhysicalNumberOfRows() > 2) { User user; for (int i = 2; i < sheet.getPhysicalNumberOfRows(); i++) { user = new User(); user.setId(UUIDUtil.getUUID()); //4.读取单元格 Row row = sheet.getRow(i); //用户名 Cell cell0 = row.getCell(0); user.setName(cell0.getStringCellValue()); //账号 Cell cell1 = row.getCell(1); user.setAccount(cell1.getStringCellValue()); //所属部门 Cell cell2 = row.getCell(2); user.setDept(cell2.getStringCellValue()); //性别 Cell cell3 = row.getCell(3); user.setGender(cell3.getStringCellValue().equals("男")); //手机号-数字需特殊处理:如果是字符串,直接读取,如果是数字,需要转换为字符串 String mobileNum = ""; Cell cell4 = row.getCell(4); try { mobileNum = cell4.getStringCellValue(); } catch (Exception e) { double mobileDlb = cell4.getNumericCellValue(); mobileNum = BigDecimal.valueOf(mobileDlb).toString(); } user.setMobile(mobileNum); //电子邮箱 Cell cell5 = row.getCell(5); user.setEmail(cell5.getStringCellValue()); //生日 Cell cell6 = row.getCell(6); if (cell6.getDateCellValue() != null) { user.setBirthday(cell6.getDateCellValue()); } //非空字段需额外设置默认值 //默认密码123456 user.setPassword("123456"); //默认状态为 有效 user.setState(User.USER_STATE_VALID); //5.保存用户 userList.add(user); } } //关闭资源 workbook.close(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } return userList; } return null; } return null; } }
3.附导出导入的文件样式
以上是关于[Utils]POI实现excel导入导出的主要内容,如果未能解决你的问题,请参考以下文章