java导出excel到服务器并下载到chrom浏览器

java导出excel到服务器并下载到chrom浏览器,第1张

java导出excel到服务器并下载到chrom浏览器

不废话,直接上代码。。。

js jsp:

jsp:



js:
function export1() {	
	var fileName =$("#fileName ").val();
	var confirm = window.confirm("是否确定导出‘"+fileName +"’excel文件","取消");
	if(!/confirm/i){
		return false;
	}
    window.location.href = ${px}+ "/export1?fileName ="+fileName ;
}

java:

	
	@ResponseBody
	@GetMapping("/export1")
	public void export1(@RequestParam Map params, String fileName , HttpServletResponse res) {
		// 查询列表数据
		List list = listService.list(sql);

		String[] headers = { "", "", "" };

        //服务器路径
		String str = System.getProperty("user.dir").replace("\", "/") + "/src/main/resources/templateFiles/";
		String path = str + fileName + "_" + System.currentTimeMillis() + ".xls";

		// 导出
		ExcelUtil.excelExport(fileName, headers, list, path, res);
	}

excelUtil:

	
	public static void excelExport(String fileName, String[] headers, List list,
			String path, HttpServletResponse res) {
		// 创建excel
		HSSFWorkbook workbook = new HSSFWorkbook();

		// 创建一张工作表
		HSSFSheet sheet = workbook.createSheet(fileName);
		sheet.setColumnWidth(0, 5000);

		// 创建第一行的第一个单元格--标题
		HSSFRow row = sheet.createRow(0);
		for (int j = 0; j < headers.length; j++) {
			HSSFCell cell = row.createCell(j);
			cell.setCellValue(headers[j]);
			sheet.setColumnWidth((short) j, (short) 8000);
		}

		// 创建下一行
		for (short i = 0; i < list.size(); i++) {
			row = sheet.createRow(i + 1);
			row.createCell(0).setCellValue(list.get(i).get字段1());
			row.createCell(1).setCellValue(list.get(i).get字段2());
			row.createCell(2).setCellValue(list.get(i).get字段3());
		}

		// 下载路径
		OutputStream out = null;
		try {
			out = new FileOutputStream(path);
			workbook.write(out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 下载到浏览器
		downLoadFile(path, res, fileName);
		// 删除下载到服务器的源文件
		File file = new File(path);
		file.delete();
	}


    
	private static void downLoadFile(String path, HttpServletResponse res, String fileName) {
		File file = new File(path);// 下载到服务器的文件
		res.setContentType("application/vnd.ms-excel");// excel
		// 文件名
		try {
			res.setHeader("Content-Disposition",
					"attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1") + "_" + System.currentTimeMillis() + ".xls");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		res.setContentLength((int) file.length());
		byte[] buffer = new byte[4096];
		BufferedOutputStream output = null;
		BufferedInputStream input = null;
		try {
			output = new BufferedOutputStream(res.getOutputStream());
			input = new BufferedInputStream(new FileInputStream(file));
			int n = -1;
			// 遍历,开始下载
			while ((n = input.read(buffer, 0, 4096)) > -1) {
				output.write(buffer, 0, n);
			}
			output.flush();
			res.flushBuffer();
		} catch (Exception e) {
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

over!!!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5609646.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存