//这是jxls 生成Excel的整套代码,希望对大家有帮助阿
public String creatExcelBylist( String relativelyPath, String templateFileName, List guestMembers, String makerName) {
if(relativelyPath == null || relativelyPath==""){
return null;
}
if (!(relativelyPath.endsWith("//") || relativelyPath.endsWith("/"))) {
relativelyPath += "/";
}
String fileName = createFileName();//生成随机文件
String destFileName = relativelyPath+fileName;
try {
createFileCatalog(relativelyPath);//如果路径不存在则创建一个文件目录
delFile(relativelyPath);//删除指定目录下今天(24小时)之前的文件 //TODO
Map beans = new HashMap();
beans.put("maker", makerName);
beans.put("time", DateUtils.currentTime());
beans.put("members", guestMembers);
XLSTransformer transformer = new XLSTransformer();
// String n = getClass().getResource("/").getFile();
String m = this.getClass().getClassLoader().getResource("").getPath();
long startTime = System.currentTimeMillis();
transformer.transformXLS(m+templateFileName, beans, destFileName);
long endTime = System.currentTimeMillis();
System.out.println((endTime-startTime)/1000);
*//* File file = ResourceUtils.getFile("classpath:excel/roomorder_crs.xls");
InputStream is = new BufferedInputStream(new FileInputStream(file));
transformer.transformXLS(is, beans);*//*
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
/*
*@Function : delFile
*@Description : 删除文件存储目录下x小时之前的文件
*@Input : Excel文件保存地址等
*@Calls by : createExcel(...)
*/
private void delFile(String filePath){
File file = new File(filePath);
if(file.exists()){
for(int i=0;i<file.list().length;i++){
File[] files = file.listFiles();
String[] fileN = file.list();
String fileStr = fileN[i];
String[] hourFile = fileStr.split("_");
if(hourFile.length>2 && DateUtils.isTimePeriodExceed24Hours(hourFile[1],DateUtils.mineTime())){
files[i].delete();
}
}
}
}
/*
*@Function : createFileName
*@Description : 随机生成文件名
*@Calls by : createExcel(...)
*/
private String createFileName() {
return "member_"+DateUtils.mineTime() + "_" + System.currentTimeMillis() + "_" + Math.round(Math.random()*10000)+".xls"; //TODO
}
/*
*@Function : createFileCatalog
*@Description : 创建文件目录
*@Input : Excel文件保存地址等
*@Calls by : createExcel(...)
*/
private void createFileCatalog(String path) {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
}