public class ExportExcelInfo extends BaseAction {
private String filename;
@SuppressWarnings("unused")
private static final long serialVersionUID = 1L;
private static final LogDebug logDebug = new LogDebug(ExportExcelInfo.class);
public String getfilename() {
return filename;
}
public void setfilename(String filename) {
this.filename = filename;
}
public String execute() throws Exception{
this.getResponse().setContentType("application/vnd.ms-excel");
this.getResponse().setHeader("Content-disposition", "attachment;filename=a.xls");
this.filename="\\template\\a.xls";
String filePath=this.getRequest().getRealPath(this.filename);
byte[] buffer = new byte[16*1024];
int read;
File file = new File(filePath);
InputStream is = new FileInputStream(file);
OutputStream os = this.getResponse().getOutputStream();
while((read = is.read(buffer))>=0){
System.out.println("read:"+read);
os.write(buffer,0,read);
os.flush();
}
os.close();
is.close();
return null;
}
}
千万注意,在上面的输出流,必须用 this.getResponse.getOutPutStream才行,不然将无法把服务器端的文件下载到本地
private String filename;
@SuppressWarnings("unused")
private static final long serialVersionUID = 1L;
private static final LogDebug logDebug = new LogDebug(ExportExcelInfo.class);
public String getfilename() {
return filename;
}
public void setfilename(String filename) {
this.filename = filename;
}
public String execute() throws Exception{
this.getResponse().setContentType("application/vnd.ms-excel");
this.getResponse().setHeader("Content-disposition", "attachment;filename=a.xls");
this.filename="\\template\\a.xls";
String filePath=this.getRequest().getRealPath(this.filename);
byte[] buffer = new byte[16*1024];
int read;
File file = new File(filePath);
InputStream is = new FileInputStream(file);
OutputStream os = this.getResponse().getOutputStream();
while((read = is.read(buffer))>=0){
System.out.println("read:"+read);
os.write(buffer,0,read);
os.flush();
}
os.close();
is.close();
return null;
}
}
千万注意,在上面的输出流,必须用 this.getResponse.getOutPutStream才行,不然将无法把服务器端的文件下载到本地
本文介绍了一个使用Java实现的导出Excel文件的例子。通过设置响应类型和头部信息,可以将服务器上的文件以附件形式提供给客户端下载。该示例还展示了如何读取服务器上的文件并将其发送到客户端。
3369

被折叠的 条评论
为什么被折叠?



