POI实现EXCEL文件的下载

本文介绍如何使用Java编写代码实现从服务器读取并下载模板文件的功能,包括使用流操作打开模板,创建响应对象,并封装下载弹框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考代码

@GET
	@Path("/downloadTemplate")
	@Produces("*/*")
	public String downloadTemplate(){
		System.out.println("请进!");
		try {
			Message message = PhaseInterceptorChain.getCurrentMessage();
			HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
			String templatePath = request.getServletContext().getRealPath("/") + "\\make\\xlsprint\\";
			//通过流打开模板grade.xls
			HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(templatePath + "userTemplate.xls")));
			//打开一个工作表
			HSSFSheet sheet = wb.getSheetAt(0);
			HSSFRow nRow = null;
			HSSFCell nCell = null;
			//设置大标题
			nRow = sheet.getRow(0);
			nCell = nRow.getCell(1);
			nCell.setCellStyle(nCell.getCellStyle());
			// 生成流对象
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
			// 将excel写入流
	        wb.write(byteArrayOutputStream);
	        // 利用工具类DownloadBase,封装弹出下载框
	        String outFileName = "用户信息表.xls";
	        DownloadBase down = new DownloadBase();
	        //获取response对象
	        HttpServletResponse response = (HttpServletResponse) message.get(AbstractHTTPDestination.HTTP_RESPONSE);
	        down.download(byteArrayOutputStream, response, outFileName);
		} catch (Exception e) {
			return StringParam.returnPageFalse();
		}
		return StringParam.returnPageTrue();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值