問題描述
我有一個(gè)創(chuàng)建 Excel 文檔的 JSP 文件.
I have a JSP file which creates an Excel document.
我想動(dòng)態(tài)設(shè)置要下載的文件名.
I want to dynamically set the name of the file to be downloaded.
這就是我將文件名設(shè)置為test.xsl"的方式:
This is how I set the file name to "test.xsl":
<% response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=" + "test.xsl" );
%>
如何將文件名設(shè)置為 test-${今天的日期}.xsl(即 test-20100805.xsl)?
How can I set the file name to be test-${today's date}.xsl ( i.e. test-20100805.xsl ) ?
推薦答案
String fname = MessageFormat.format(
"test-{0,date,yyyyMMdd}.xsl", new Object [] { new Date() } );
response.setHeader("Content-Disposition","attachment; filename=" + fname );
我認(rèn)為這應(yīng)該適合你.
大括號中的文本告訴 MessageFormat
類從給定數(shù)組中插入值 0
,使用格式將其格式化為 date
yyyyMMdd
(例如 20161231
表示 2016 年 12 月 31 日).
The text in the braces tells the MessageFormat
class to insert value 0
from the given array, format it as a date
using the format yyyyMMdd
(e.g. 20161231
for Dec 31st 2016).
這篇關(guān)于如何在 JSP 中動(dòng)態(tài)設(shè)置標(biāo)頭值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!