最近公司让我做一个报表的打包下载功能,以前从来没有接触过,从问了下度娘,实现其功能的方式很多,其中我找到一篇博文就写得非常好,本人也是根据博文所讲做的,但是现在找不到他的链接地址了,,在这里就本人所做的项目所需的功能,简单罗列下。
使用到的主要类为:
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WritableCellFormat;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
一、首先说下业务需求:主要是实现多个报表的下载,多个报表的样式和字段长度各不相同
二、功能实现:
在service中
String path = GlobalContext.contextPath + "/uploadtemp";//这个路径为生成的excel文件临时存储地址
//整个功能实现大致分为一下几步:
//1、创建excel临时存储文件夹
createFile(path);
//2、生成Excel文件;
createExcelFile(reportInfo,path,type);//参数reportInfo为生成excel所需要的数据,path是excel文件临时存放的地址,type是根据业务需求所需要的参数,
//3、生成.zip文件;
FileItem fileItem = craeteZipPath(path);
//4、删除临时目录下所有的文件;
File file = new File(path);
//删除文件;
deleteExcelPath(file);
//5、重新创建文件;
file.mkdirs();
/**
* 创建文件夹;
* @param path
* @return
*/
public String createFile(String path){
File file = new File(path); //得到路径下的文件
if(!file.exists()){ //不存在就创建文件夹
//创建文件;
boolean bol = file.mkdirs();
if(bol){
System.out.println(path+" 路径创建成功!");
}else{
System.out.println(path+" 路径创建失败!");
}
}else{
System.out.println(path+" 文件已经存在!");
}
return path;
}
/**
* 在指定目录下创建Excel文件;
* @param path
* @throws IOException
* @throws WriteException
* @throws RowsExceededException
*/
public void createExcelFile(ReportInfo reportInfo,String path,String types) throws Exception{
//删掉的都是业务条件,不需要多做解释,
<del>String date = reportInfo.getYear()+"-"+reportInfo.getMonth();//
String [] typeArr = null;
//判断是不是全选
if(SysUtils.isBlank(types)){//全选
List<CodeitemInfo> clist = codeitemMapper.listByCodesetNo("RP001");
for (CodeitemInfo codeitemInfo : clist) {</del>
//生成excel
NewExcelUtil.strategy(reportsMapper, codeitemInfo, path, date);//调用工具类,生成excel文件;其中只有path是需要的,其他都是业务条件
<del>}
}else{
typeArr = types.split(",");
for (String str : typeArr) {
if(SysUtils.isBlank(str)){
continue;
}
//excel的标题和内容-----tablename,title
CodeitemInfo codeitem = codeitemMapper.detailBy2No("RP001", str);
//生成excel
NewExcelUtil.strategy(reportsMapper, codeitem, path, date);
}
}</del>
}
/**
* 生成.zip文件; 这是关键方法
* @param path
* @throws IOException
*/
public FileItem craeteZipPath(String path) throws IOException{
FileItem fileItem = WebUtils.newFileItem("用户统计报表.zip", null);//创建zip文件
ZipOutputStream zipOutputStream = null;
zipOutputStream = new ZipOutputStream(fileItem.getOutputStream());//得到文件输出流
zipOutputStream.setEncoding("gbk");//设置编码方式
File[] files = new File(path).listFiles(); //得到path路径下已经创建的excel临时文件
FileInputStream fileInputStream = null;
byte[] buf = new byte[1024];
int len = 0;
if(files!=null && files.length > 0){ //
for(File excelFile:files){ //遍历文件
String fileName = excelFile.getName();
fileInputStream = new FileInputStream(excelFile);
//放入压缩zip包中;
zipOutputStream.putNextEntry(new ZipEntry(fileName));
//读取文件;
while((len=fileInputStream.read(buf)) >0){
zipOutputStream.write(buf, 0, len);
}
//关闭;
zipOutputStream.closeEntry();
if(fileInputStream != null){
fileInputStream.close();
}
}
}
if(zipOutputStream !=null){
zipOutputStream.close();
}
return fileIte