public void exportExecel(){
//判断用户类型
BookUserInfo user = getUser();
try {
if(user!=null && user.isIsadmin()){
Pager<BookInfoVo> pager =
bookInfoService.getPagerBySql(null, null, page, 99999, sort, order);
List<BookInfoVo> bookInfoList= pager.getDatas();
if(null!=bookInfoList && bookInfoList.size()>0){
String filePath =
request.getSession().getServletContext().getRealPath("/export/exportboo
k.xls");
File file = new File(filePath);
InputStream is = new FileInputStream(file);
创建excel工作簿
HSSFWorkbook wb = new HSSFWorkbook(is);
//获取第一个sheet(页)
Sheet sheet = wb.getSheetAt(0);
//Row行 Cell方格 Row 和 Cell 都是从0开始计数的
int i=1;
for(BookInfoVo book : bookInfoList){
// 创建一行,在页sheet上
Row row = sheet.createRow(i);
row.createCell(0).setCellValue(book.getId());
row.createCell(1).setCellValue(book.getBookname());
row.createCell(2).setCellValue(book.getBookprice().doubleValue());
row.createCell(3).setCellValue(book.getBookauthor());
row.createCell(4).setCellValue(book.getPublishs());
row.createCell(5).setCellValue(ObjectFormatUtil.formatDateStr(book.g
etReleasedate()));
row.createCell(6).setCellValue(book.getLibName());
row.createCell(7).setCellValue(book.getTypeName());
i++;
}
SimpleDateFormat sdformat = new
SimpleDateFormat("yyyyMMdHHmmss");
String saveFileName = "exportbook-
"+sdformat.format(new Date())+"-"+user.getId()+".xls";
String savePath =
request.getSession().getServletContext().getRealPath("/export");
File saveFile = new File(savePath,saveFileName);
OutputStream fos = new FileOutputStream(saveFile);
BufferedOutputStream buffout=new
BufferedOutputStream(fos);
// 把上面创建的工作簿输出到文件中
wb.write(buffout);
buffout.flush();
buffout.close();
downloadLocal(saveFile.getAbsolutePath(),saveFileName);
}else{
//无数据可导入
message = "{\"error\":false,\"message\":"+"没有数据导
出"+"}";
outJsonPlainString(response, message);
}
}else{
//未登陆或者无权限 ( 只有管理员才可以操作)
message = "{\"error\":\"true\",\"message\":\"无权限导出,
请联系管理员\"}";
outJsonPlainString(response, message);
}
} catch (Exception e) {
e.printStackTrace();
message = "{\"error\":\"true\",\"message\":\"导出失败\"}";
outJsonPlainString(response, message);
}
}
/**
* @Title: downloadLocal
* @Description: 下载本地的文件
* @param @param fileName 文件的名字,outFileName 下载的默认保存文件名
* @return void
* @throws
*/
public void downloadLocal(String filePath,String outFileName) throws
FileNotFoundException {
// 读到流中
InputStream inStream = new FileInputStream(filePath);// 文件的存
放路径
String downFileName = null;
try {
downFileName = URLEncoder.encode(outFileName, "UTF-8");
} catch (Exception e) {
downFileName = outFileName;
}
// 设置输出的格式
response.reset();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;
filename=\""+downFileName+"\"");
// 循环取出流中的数据
byte[] b = new byte[1024*1024];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Jsp页面
function exportExcel(){
location="<c:url value='/book/exportExecelBook.action'/>";
}
<a href="javascript:exportExcel()">导出到Excel</a>
excel
最新推荐文章于 2024-02-21 11:11:44 发布