java实现下载功能Controller层代码
2017-12-17 15:17
369 查看
public class DownloadExcelController(HttpServletRequest request,HttpServletResponse response){
//创建HSSFWorkbook对象,对应一个EXCEL文件
HSSFWorkbook wb=new HSSFWorkbook();
//创建一个HSSFSheet,对应excel中的页
HSSFSheet sheet=wb.createSheet("学生列表");
//创建HSSFRow,对应excel中的行
HSSFRow row=sheet.createRow(0);
//创建HSSFCell,对应excel中的列
HSSFCell cell=sheet.createCell(0);
cell.setCellValue("ID");
cell=row.createCell(1);
cell.setCellValue("NAME");
cell=row.createCell(2);
cell.setCellValue("AGE");
//往excel中循环添加数据
for(int i=0;i<5;i++){
row=sheet.createRow(i+1);
cell=row.createCell(0);
cell.setCellValue(100+i);
cell=row.createCell(1);
cell.setCellValue("NAME"+i);
cell=row.createCell(2);
cell.setCellValue(20+i);
}
//把wb写入磁盘
//OutputStream os=new FileOutputStream("d:test.xls");
//设置响应类型
response.setContentType("application/octet;charset="UTF-8");
//获取用户浏览器信息
String browser=request.getHeader("User-Agent");
String filename=URLEncoder.encode("学生列表","UTF-8")
//火狐浏览器
if(browser.toLowerCase().contains("firefox")){
filename=new String("学生列表".getBytes("UTF-8"),"iso8859-1")
}
//设置浏览器的处理方式
response.addHeader("Content-Dispotion","attachment";filename="学生列表.xls")
OutputStream os=response.getOutputStream();
wb.write(os);
os.flush;
}
//创建HSSFWorkbook对象,对应一个EXCEL文件
HSSFWorkbook wb=new HSSFWorkbook();
//创建一个HSSFSheet,对应excel中的页
HSSFSheet sheet=wb.createSheet("学生列表");
//创建HSSFRow,对应excel中的行
HSSFRow row=sheet.createRow(0);
//创建HSSFCell,对应excel中的列
HSSFCell cell=sheet.createCell(0);
cell.setCellValue("ID");
cell=row.createCell(1);
cell.setCellValue("NAME");
cell=row.createCell(2);
cell.setCellValue("AGE");
//往excel中循环添加数据
for(int i=0;i<5;i++){
row=sheet.createRow(i+1);
cell=row.createCell(0);
cell.setCellValue(100+i);
cell=row.createCell(1);
cell.setCellValue("NAME"+i);
cell=row.createCell(2);
cell.setCellValue(20+i);
}
//把wb写入磁盘
//OutputStream os=new FileOutputStream("d:test.xls");
//设置响应类型
response.setContentType("application/octet;charset="UTF-8");
//获取用户浏览器信息
String browser=request.getHeader("User-Agent");
String filename=URLEncoder.encode("学生列表","UTF-8")
//火狐浏览器
if(browser.toLowerCase().contains("firefox")){
filename=new String("学生列表".getBytes("UTF-8"),"iso8859-1")
}
//设置浏览器的处理方式
response.addHeader("Content-Dispotion","attachment";filename="学生列表.xls")
OutputStream os=response.getOutputStream();
wb.write(os);
os.flush;
}
相关文章推荐
- Java代码使用FTPClient实现ftp文件上传下载和删除的功能
- java实现51cto网站的自动签到与获取下载积分等功能代码
- Java中实现FTP上传下载文件的功能,完整代码
- Java中实现FTP上传下载文件的功能,完整代码
- Java实现FTP文件的上传和下载功能的实例代码
- Java中实现FTP上传下载文件的功能,完整代码
- 利用Java 代码创建ftp客户端并实现上传下载等功能
- java代码实现打包多个文件下载功能
- java文件上传下载功能实现代码
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- 站点中,实现: 在线签名 功能 (示例代码下载)
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Jsp/Java代码分离.实现页面真正的代码分离 测试代码是否按想法运行,下载原代码
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载][续]
- Asp.net 2.0 自定义控件开发[实现自动计算功能(AutoComputeControl)][示例代码下载]
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载)
- AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载)
- 用java实现上传下载功能