JSP导入导出Excel功能 您所在的位置:网站首页 jquery导出筛选出的表格数据 JSP导入导出Excel功能

JSP导入导出Excel功能

2023-11-20 05:36| 来源: 网络整理| 查看: 265

    导入导出功能需求是这样的:按照条件查询出结果,然后将这些结果以excel形式导出;修改字段信息后(主键不允许修改)导入即覆盖原字段信息,完成更新。本例是借助poi完成的,将poi-3.9.jar导入到WEB-INF下的lib的文件夹,与此一起导入的还有commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar,用于io输入和文件上传。

一、导出

功能:选择时间条件——导出——选择保存路径——保存,如下图所示

实现过程:写一个创建excel的方法,先创建一个excel文件,创建好工作表,表头;然后将数据内容填充到里面,数据是根据查询的结果而来;最后保存excel文件至服务器的一个临时位置(每次会覆盖),用户下载的时候从这个位置下载到客户端。

Excel创建代码:

public static void createExcel(List list) throws SQLException{ // 创建一个Excel文件 HSSFWorkbook workbook = new HSSFWorkbook(); // 创建一个工作表 HSSFSheet sheet = workbook.createSheet("sku表"); // 添加表头行 HSSFRow hssfRow = sheet.createRow(0); // 设置单元格格式居中 HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 添加表头内容 HSSFCell headCell = hssfRow.createCell(0); headCell.setCellValue("服务SKU"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(1); headCell.setCellValue("内外部判别"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(2); headCell.setCellValue("产品名称"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(3); headCell.setCellValue("规格型号"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(4); headCell.setCellValue("发票名称"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(5); headCell.setCellValue("状态"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(6); headCell.setCellValue("生命周期"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(7); headCell.setCellValue("生成时间"); headCell.setCellStyle(cellStyle); // 添加数据内容 for (int i = 0; i < list.size(); i++) { hssfRow = sheet.createRow((int) i + 1); Sku student = list.get(i); // 创建单元格,并设置值 HSSFCell cell = hssfRow.createCell(0); cell.setCellValue(student.getSku()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(1); cell.setCellValue(student.getNwjduge()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(2); cell.setCellValue(student.getProname()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(3); cell.setCellValue(student.getSername()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(4); cell.setCellValue(student.getInvname()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(5); cell.setCellValue(student.getState()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(6); cell.setCellValue(student.getLife()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(7); cell.setCellValue(student.getCreatetime()); cell.setCellStyle(cellStyle); } // 保存Excel文件 try { OutputStream outputStream = new FileOutputStream("/usr/java/tomcat/apache-tomcat-7.0.85/temp/sku.xls"); workbook.write(outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } }

下载代码:

//从服务器指定位置下载Excel文件 服务SKU码

二、导入

功能:导入——选择文件——上传,如下图所示

实现过程:用户先上传文件至服务器的临时位置,然后写一个读取excel的方法,程序从服务器的临时位置读取excel文件;然后循环工作表和行,将单元格的内容存入集合;再上传至临时表(每次先清空),再根据sku(主键)更新正式数据库即可完成导入文件更新字段信息。

上传代码,使用了fileupload的jar包:

Excel读取代码:

public static List readExcel() { List list = new ArrayList(); HSSFWorkbook workbook = null; try { // 读取Excel文件 //String sep = System.getProperty("file.separator"); InputStream inputStream = new FileInputStream("/usr/java/tomcat/apache-tomcat-7.0.85/temp/sku.xls"); workbook = new HSSFWorkbook(inputStream); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } // 循环工作表 for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = workbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 循环行 for (int rowNum = 1; rowNum


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有