excel表格的导入导出
将数据导出为excel表格:
Maven依赖:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency>
代码示例:
public String exportExcel(String excelName,HttpSession session){ List<CustomerVo> list = (List<CustomerVo>) session.getAttribute("customerVo"); //创建新excel文档,07版本之前均可以这么写 HSSFWorkbook workBook = new HSSFWorkbook(); //新建工作表 HSSFSheet sheet = workBook.createSheet("文档"); //第一行数据 HSSFRow row = sheet.createRow(0); // 创建一个盛放所有列的数组 HSSFCell cell[] = new HSSFCell[9]; for(int i = 0; i < cell.length; i++){ //取第一行第一列 cell[i] = row.createCell(i); } //给第一行所有列赋值 cell[0].setCellValue("编号"); cell[1].setCellValue("公司名称"); cell[2].setCellValue("公司负责人名称"); cell[3].setCellValue("地址"); cell[4].setCellValue("手机"); cell[5].setCellValue("座机"); cell[6].setCellValue("简介"); cell[7].setCellValue("备注"); cell[8].setCellValue("创建时间"); for(int i=0;i<list.size();i++){ Customer cus = list.get(i); HSSFRow dataRow = sheet.createRow(i+1); //创建盛放所有列的数组 HSSFCell dataCell[] = new HSSFCell[9]; for(int j = 0; j < dataCell.length; j++){ //取得第一行所有列 dataCell[j] = dataRow.createCell(j); } dataCell[0].setCellValue(cus.getId()); dataCell[1].setCellValue(cus.getComname()); dataCell[2].setCellValue(cus.getCompanyperson()); dataCell[3].setCellValue(cus.getComaddress()); dataCell[4].setCellValue(cus.getComphone()); dataCell[5].setCellValue(cus.getCamera()); dataCell[6].setCellValue(cus.getPresent()); dataCell[7].setCellValue(cus.getRemark()); dataCell[8].setCellValue(cus.getAddtime()); //创建样式 HSSFCellStyle cellStyle = workBook.createCellStyle(); //日期 HSSFDataFormat format= workBook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("yyyy年MM月dd日")); dataCell[8].setCellStyle(cellStyle); } try { // 创建存放excel表格的位置 File file = new File("D:\\worksapce\\excel\\"+excelName+".xlsx"); FileOutputStream fos = new FileOutputStream(file); workBook.write(fos); fos.close(); } catch (Exception e) { e.printStackTrace(); } session.removeAttribute("customerInfo"); return "customer"; }
代码分析:
参数:excelName 这是前台传到后台用于给文件取名的值
数据:存储的数据是通过查询后将数据放入与对象,到执行导出功能时将数据从域对象中拿出数据。使用完毕后将该对象从域中移除掉
代码整体分为两个部分:表头,和表数据
所以将这两部分分开进行参数绑定
最后同过 HSSFWorkbook 对象通过流将数据导出到表格中
excel表格导入数据库:
注意事项:
前台表单提交需要在form表单添加 ENCTYPE="multipart/form-data" 属性
异常提醒:提交文件后缀是:.xls
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents.
You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
原因分析:xls是二进制结构,而xlsx是xml类型,采用的是xml的压缩方式,使其占用空间更小。
代码示例:
public String addArchives(MultipartFile files, HttpServletRequest request) { int i = 0; List<Archives> list = new ArrayList<Archives>(); try{ CommonsMultipartFile cm = (CommonsMultipartFile)files; InputStream inputStream = cm.getInputStream(); //创建新excel文档 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream); //循环工作表 System.out.println(hssfWorkbook.getNumberOfSheets()); for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){ //获取指定索引的页 HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if(hssfSheet == null){ continue; } //循环当前页中的具体行(第0行是表头) for(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++){ //根据索引获取具体的行,第一行为表头所以索引从1开始 HSSFRow hssfRow = hssfSheet.getRow(rowNum); if(hssfRow != null){ //获取当前行指定索引的列对象 HSSFCell dnum = hssfRow.getCell(0); HSSFCell landline = hssfRow.getCell(1); HSSFCell school = hssfRow.getCell(2); HSSFCell zhuanye = hssfRow.getCell(3); HSSFCell sosperson = hssfRow.getCell(4); HSSFCell biyedate = hssfRow.getCell(5); HSSFCell zzmm = hssfRow.getCell(6); HSSFCell minzu = hssfRow.getCell(7); HSSFCell xueli = hssfRow.getCell(8); HSSFCell email = hssfRow.getCell(9); HSSFCell empFk = hssfRow.getCell(10); HSSFCell remark = hssfRow.getCell(11); HSSFCell hirdate = hssfRow.getCell(12); //创建样式 HSSFCellStyle cellStyle = hssfWorkbook.createCellStyle(); Archives archives = new Archives(); archives.setDnum(dnum.getStringCellValue()); archives.setLandline(landline.getStringCellValue()); archives.setSchool(school.getStringCellValue()); archives.setZhuanye(zhuanye.getStringCellValue()); archives.setSosperson(sosperson.getStringCellValue()); archives.setBiyedate(biyedate.getDateCellValue()); archives.setZzmm(zzmm.getStringCellValue()); archives.setMinzu(minzu.getStringCellValue()); archives.setXueli(xueli.getStringCellValue()); archives.setEmail(email.getStringCellValue()); archives.setEmpFk((int)empFk.getNumericCellValue()); archives.setRemark(remark.getStringCellValue()); archives.setHiredate(hirdate.getDateCellValue()); list.add(archives); } } } archivesMapper.batchInsert(list); }catch(Exception e){ e.printStackTrace(); }
代码分析:
参数:files前台提交文件
过程:首先将 MultipartFile 强转为 CommonsMultipartFile 对象 然后获取流
然后 HSSFWorkbook 通过流将数据读取出来,通过遍历将数据取出然后放入集合中传入数据持久层进行数据存储。