最近用了下xls生成和导出excel
2014-12-18 16:45
239 查看
注意要引入新的jar
我把jar放到资源里
//生成excel
public static String doExcel(String from, Map beans, String to,HttpServletRequest request) {
String path = request.getSession().getServletContext().getRealPath("//template");//模板地址
String pathdown = request.getSession().getServletContext().getRealPath("//upload");//上传地址
XLSTransformer transformer = new XLSTransformer();
String sfrom = path + File.separator + from;// 模板文件
String sto = pathdown + File.separator + to;// 要生成的文件
try {
transformer.transformXLS(sfrom, beans, sto);
} catch (ParsePropertyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sto;
}
// 下载
public static void doDownLoad(String path, String name,HttpServletResponse response) {
try {
response.reset();
response.setHeader("Content-disposition","attachment;success=true;filename ="+ URLEncoder.encode(name, "utf-8"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
File uploadFile = new File(path);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
// 弹出下载对话框
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/execlbanlace")
public String execlbanlace(HttpSession session, HttpServletRequest request, HttpServletResponse response){
Map<String,String> map = new HashMap<String,String>();
List<userResource> list=NumberDao.SearchRes(map);
Map<String,Object> beans = new HashMap<String,Object>();
beans.put("balancelist",list);
IPTimeStamp its= new IPTimeStamp("");
String fileName = its.getIPTimeStampRand();
String path=doExcel("号码资源模板.xlsx" , beans , "号码资源_"+fileName+".xlsx",request);
// doDownLoad(path, "balance.xls",response);
return null;
}
注意模板内代码要和beans的一致
我把jar放到资源里
//生成excel
public static String doExcel(String from, Map beans, String to,HttpServletRequest request) {
String path = request.getSession().getServletContext().getRealPath("//template");//模板地址
String pathdown = request.getSession().getServletContext().getRealPath("//upload");//上传地址
XLSTransformer transformer = new XLSTransformer();
String sfrom = path + File.separator + from;// 模板文件
String sto = pathdown + File.separator + to;// 要生成的文件
try {
transformer.transformXLS(sfrom, beans, sto);
} catch (ParsePropertyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sto;
}
// 下载
public static void doDownLoad(String path, String name,HttpServletResponse response) {
try {
response.reset();
response.setHeader("Content-disposition","attachment;success=true;filename ="+ URLEncoder.encode(name, "utf-8"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
File uploadFile = new File(path);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
// 弹出下载对话框
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/execlbanlace")
public String execlbanlace(HttpSession session, HttpServletRequest request, HttpServletResponse response){
Map<String,String> map = new HashMap<String,String>();
List<userResource> list=NumberDao.SearchRes(map);
Map<String,Object> beans = new HashMap<String,Object>();
beans.put("balancelist",list);
IPTimeStamp its= new IPTimeStamp("");
String fileName = its.getIPTimeStampRand();
String path=doExcel("号码资源模板.xlsx" , beans , "号码资源_"+fileName+".xlsx",request);
// doDownLoad(path, "balance.xls",response);
return null;
}
注意模板内代码要和beans的一致
相关文章推荐
- asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excel-xls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;
- 关于poi导出大数据量EXCEL导出缓慢的解决方案--生成xml格式的表格文件(注意xls格式为03 07下格式会不一样)
- 从datagrid导出html代码,生成excel文件,给客户端下载
- 总结一下最近一段时间导出数据到Excel的几种方法
- 发现:不要Excel也可生成xls文件 !
- 生成并导出一个Excel,包括用javaxcript、java
- 把数据库查询信息直接生成网页嵌入EXCEL表的格式,并可导出成EXCEL文件
- 自己总结的手动生成gridview导出excel的方法
- 直接通过excel可以识别的文件结构生成xls文件的方法,这样就可以不引用麻烦的ole了 (转)
- Asp.Net下导出/导入规则的Excel(.xls)文件
- 通过excel可识别的xml结构直接生成xls文件
- 导出到excel,将数字转换成三位逗号分隔的样式,清空浏览器客户端的缓存,生成缩略图
- 数据库内容导出到EXCEL生成报表和图形的方法
- 数据库内容导出到EXCEL生成报表和图形的方法
- Asp.Net下导出/导入规则的Excel(.xls)文档
- 总结一下最近一段时间导出数据到Excel的几种方法
- [转]Asp.Net下导出/导入规则的Excel(.xls)文件
- PHP导出MySQL数据库为EXCEL文件格式(.xls)的方法 http://www.kankanblog.com/read.php/144.htm
- 通过excel可识别的xml结构直接生成xls文件
- 通过excel可识别的xml结构直接生成xls文件