POI导入导出Excel模板···
2015-04-20 16:35
225 查看
HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
以XSSFWorkbook为例:
XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
以XSSFWorkbook为例:
//导出 public void getExcelByBoiler() { try { /***************没有模板********************************/ // 第一步,创建一个webbook,对应一个Excel文件 XSSFWorkbook work = new XSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet XSSFSheet sheet = work.createSheet("学生表一"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short XSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 XSSFCellStyle style = work.createCellStyle(); style.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 XSSFCell cell = row.createCell((short) 0); cell.setCellValue("学号"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("姓名"); cell.setCellStyle(style); /*****************没有模板END**************************************/ /******************有模板*******************************/ // // 取得excel模板路径 // String path =ServletActionContext.getServletContext().getRealPath("") // + "/excelTemplate/统计.xlsx"; //这个是我的excel模板 // InputStream in = new FileInputStream(new File(path)); // //取得excel模板 // XSSFWorkbook work = new XSSFWorkbook(in); // // 得到excel的第1张表 // XSSFSheet sheet = work.getSheetAt(0); // // 取得第一行 // XSSFRow row = sheet.getRow(0); // // 取得第一行第一列的单元格 // XSSFCell cell = row.getCell(0); // // 给第一行第一列的单元格设值 // cell.setCellValue("第一行第一列"); // // 取得第二行 // row = sheet.createRow(1);// 得到行 // cell = row.createCell(0);// 得到第1个单元格 // cell.setCellValue("第二行第一列"); // // cell.setCellStyle(columnOne);// 填充样式 // // cell = row.createCell(1); // cell.setCellValue("第二行第二列"); // // cell.setCellStyle(columnOne1);// 填充样式 // row = sheet.createRow(2);// 得到行 // cell = row.createCell(1); // cell.setCellValue("第三行第二列"); /*********************有模板END**********************************/ /*********************显示图片**********************************/ //获取批注对象 // XSSFClientAnchor的参数说明: // 参数 说明 // dx1 第1个单元格中x轴的偏移量 // dy1 第1个单元格中y轴的偏移量 // dx2 第2个单元格中x轴的偏移量 // dy2 第2个单元格中y轴的偏移量 // col1 第1个单元格的列号 // row1 第1个单元格的行号 // col2 第2个单元格的列号 // row2 第2个单元格的行号 //(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) // 显示图片 ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); String InputimagePath = ServletActionContext.getServletContext().getRealPath("").concat(FINAL_FOLDER_PATH).concat("20150202175722.png"); BufferedImage bufferImg = ImageIO.read(new File(InputimagePath)); ImageIO.write(bufferImg,"JPG",byteArrayOut); //设置图片大小,位置 XSSFClientAnchor anchor = new XSSFClientAnchor(5,0,500,122,(short) 0, 5,(short)10,15); //创建 XSSFDrawing patri = sheet.createDrawingPatriarch(); patri.createPicture(anchor ,work.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG)); // 显示第二张图片 byteArrayOut = new ByteArrayOutputStream(); InputimagePath = ServletActionContext.getServletContext().getRealPath("").concat(FINAL_FOLDER_PATH).concat("20150202175754.png"); bufferImg = ImageIO.read(new File(InputimagePath)); ImageIO.write(bufferImg,"JPG",byteArrayOut); //设置图片大小,位置 anchor = new XSSFClientAnchor(5,0,500,122,(short) 11, 5,(short)21,15); //创建 patri = sheet.createDrawingPatriarch(); patri.createPicture(anchor ,work.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG)); /*********************显示图片END**********************************/ /****************************输出流*****************************************/ FileOutputStream fout = new FileOutputStream("E:/students.xlsx"); work.write(fout); fout.close(); downLoadFile("E:/students.xlsx"); } catch (FileNotFoundException e) { System.out.println("文件路径错误"); e.printStackTrace(); } catch (IOException e) { System.out.println("文件输入流错误"); e.printStackTrace(); } }
//导入 public void download(String path, HttpServletResponse response) throws IOException { try { // path是指欲下载的文件的路径。 File file = new File(path); // 取得文件名。 String filename = file.getName(); // 以流的形式下载文件。 InputStream fis = new BoundedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的header response.addHeader("content-disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("content-length", "" + file.length()); OutputStream toclient = new BufferedOutputStream( response.getOutputStream()); response.setContentType("application/vnd.ms-excel ;charset=gb2312"); toclient.write(buffer); toclient.flush(); toclient.close(); } catch (IIOException ex) { ex.printStackTrace(); } } public void downLoadFile(String filePth) { HttpServletResponse response =ServletActionContext.getResponse(); HttpServletRequest request =ServletActionContext.getRequest(); try { //得到当前路径 //StringfilePath=request.getSession().getServletContext().getRealPath(File.separator); File temFile = new File(filePth); //判断文件是否存在 if(!temFile.exists()){ response.getWriter().write("ERROR:File Not Found"); return ; } //处理文件名得位置(若服务器为linux和windows的处理方法不同) String fileName =filePth.substring(filePth.lastIndexOf(File.separator)+1); //设置头文件,名称和内容的编码不同,否则会出现乱码。 response.setHeader("Content-Disposition", "attachment; filename="+new String((fileName).getBytes("gbk"),"UTF-8")); response.setContentType("application/x-download"); OutputStream ot=response.getOutputStream(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(temFile)); BufferedOutputStream bos = new BufferedOutputStream(ot); byte[] buffer = new byte[4096]; int length = 0; while((length = bis.read(buffer)) > 0){ bos.write(buffer,0,length); } bos.close(); bis.close(); ot.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 获取单元格数据内容为字符串类型的数据 * * @param cell Excel单元格 * @return String 单元格数据内容 */ @SuppressWarnings("unused") private String getStringCellValue(HSSFCell cell) { String strCell = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: strCell = String.valueOf(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: strCell = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: strCell = ""; break; default: strCell = ""; break; } if (StringUtils.equals(strCell, "") || strCell == null) { return ""; } if (cell == null) { return ""; } return strCell; }
相关文章推荐
- JAVA使用poi进行EXCEL模板导入导出,XSSFCell数据类型
- Excel基于POI利用反射返回Bean导入、Excel基于POI导出、Excel利用jxls导出、Excel模板导出
- java-导入到Excel_POI根据模板(excel表头)导出excel
- web中使用POI导入导出EXCEL文件的例子
- Java中使用poi导入、导出Excel
- POI根据EXCEL模板,修改内容导出新EXCEL (只支持HSSF)
- POI 导入导出Excel文件到数据库
- Struts2.18 Poi-3.7 Excel 导入导出
- 测试POI的Excel导出导入
- poi 导入导出excel
- java poi 通过excel模板导出并且生成多个sheet
- Java-Maven-POI 简单导入导出Excel通用工具,默认使用基于poi实现
- poi导出word2003(动态数据,不用模板,且生成word能再次导入)
- poi excel导入导出 java
- 基于POI的Excel导入导出(JAVA实现)
- poi excel导入导出
- 在springmvc项目中使用poi导入导出excel
- 基于POI的导入导出Excel
- 真实需求:关于excel模板的导入导出