java 导出文件到Excel 及前端下载

反序列化文件,将结果导出到Excel

public boolean exportToExcel(String exportPath, String FilePath, String projectName) {
        try {
        	// 反序列化文件
            File file = new File(FilePath);
            Model Model = (Model) KryoSerializer.deserializer(new FileInputStream(file), Model.class);
            List<ModelChild> mc = Model.getModelList();

            String[] titles = {"标题头1", "标题头1", "标题头1", "标题头1", "标题头1"};
            // 创建文件对象
            XSSFWorkbook workbook = new XSSFWorkbook();

            // 创建表对象
            XSSFSheet sheet = workbook.createSheet("例子");
            // 创建标题栏(第一行)参数为行下标  行下标从0开始
            XSSFRow titleRow = sheet.createRow(0);
            // 在标题栏中写入数据
            for (int i = 0; i < titles.length; i++) {
                // 创建单元格
                XSSFCell cell = titleRow.createCell(i);
                cell.setCellValue(titles[i]);
            }
            int rowNum = 1;
            for (ModelChild m : mc) {
                // 遍历创建行
                XSSFRow row = sheet.createRow(rowNum);
                row.createCell(0).setCellValue(m.getRuleName());
                row.createCell(1).setCellValue(m.getFilepath());
                row.createCell(2).setCellValue(m.getLine());
                row.createCell(3).setCellValue(m.getDescript());
                rowNum++;
            }
            // 文件保存
            workbook.write(new FileOutputStream(exportPath + "-例子.xlsx"));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        doFileResponse(HttpServletResponse response, exportPath, projectName);
        return true;
    }

前端下载

/**
	 * 文件前端下载
	 * @param response  
	 * @param filePath   文件所在的路径
	 * @param projectName  要保存的文件名
	 */
	private void doFileResponse(HttpServletResponse response, String filePath, String projectName) {
		if (filePath != null) {
			File file = new File(filePath);
			if (!file.exists()) {
				throw new ExcelUploadException("文件下载失败");
			}
		}
		InputStream fis;
		try {
			log.info("要下载的文件地址是:" + filePath);
			fis = new BufferedInputStream(new FileInputStream(filePath));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition",
					"attachment;filename=" + java.net.URLEncoder.encode(projectName + ".xlsx", "UTF-8"));
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
			// 删除目录下的文件
			FileUtils.deleteQuietly(new File(filePath));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值