添加maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17-beta1</version>
</dependency>
controller层代码,采用时间戳作为文件名,title字符串数组为excel文件的表头,s为JFianl中model的key值
public void excle() throws Exception {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String str = sdf.format(date);
System.out.println(str);
File file = new File(str+"_统计表"+".xlsx");
String filePath=file.getAbsolutePath();
String sql="select * from m_message";
//ider:外部传入一个表头、SQL语句、
String title[]=new String[] {"序号","消息详情","消息类型","消息IP","发送者","发送时间","接收者","消息状态","回复"};
String s[] = new String[] { "id", "message_content","message_type", "message_ip",
"message_name", "message_time", "receiver_name", "message_state","reply"};
ExcleUtils utils=new ExcleUtils();
utils.saveFile(s, sql, file,title);
this.download(file);
}
生成excel工具类
public void saveFile(String[] s, String sql, File file,String[] title) {
// 创建工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// sheet:一张表的简称
// row:表里的行
// 创建工作薄中的工作表
HSSFSheet hssfSheet = hssfWorkbook.createSheet("test");
// 创建行
HSSFRow row = hssfSheet.createRow(0);
// 创建单元格,设置表头 创建列
HSSFCell cell = null;
// 便利表头
for (int i = 0; i < title.length; i++) {
// 创建传入进来的表头的个数
cell = row.createCell(i);
// 表头的值就是传入进来的值
cell.setCellValue(title[i]);
}
List<Record> list = Db.find(sql);
Record record = null;
if (list != null) {
// 获取所有的记录 有多少条记录就创建多少行
for (int i = 0; i < list.size(); i++) {
// row = hssfSheet.createRow(++count);
// 得到所有的行 一个record就代表 一行
record = list.get(i);
row = hssfSheet.createRow(i + 1);
// 在有所有的记录基础之上,便利传入进来的表头,再创建N行
for (int j = 0; j < s.length; j++) {
cell = row.createCell(j);
/*
* String aaaaa=s[j]; String aaa=record.get(s[j]).toString();
*/
// 把每一行的记录再次添加到表头下面 如果为空就为 "" 否则就为值
cell.setCellValue(record.get(s[j]) == null ? "" : record.get(s[j]).toString());
}
}
}
try {
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
FileOutputStream fileOutputStreane = new FileOutputStream(file);
hssfWorkbook.write(fileOutputStreane);
fileOutputStreane.flush();
fileOutputStreane.close();
/* this.downLoad(file, response, false);*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
此时在默认的文件路径下会生成你想要的excel表格
public void download(File file) {
/* String path = getSession().getServletContext().getRealPath("download");
File file = new File(path + "/a.docx");*/
// System.out.println(path);
System.out.println(file.exists());
if (file.exists()) {
/* renderFile(file);*/
render(new TempFileRender(file));
// file.delete();
} else {
renderJson("文件不存在");
}
}
再调用download方法将生成的excel文件下载到本地,此时再浏览器的下载地址和文件保存的地址会出现两个文件,这时需要重写JFinal的render,创建一个类并且继承于rander'
public class TempFileRender extends FileRender{
private String fileName;
private File file;
public TempFileRender(String fileName) {
super(fileName);
this.fileName=fileName;
}
public TempFileRender(File file) {
super(file);
this.file=file;
}
public void render() {
try {
super.render();
} finally {
if(null!=fileName) {
file=new File(fileName);
}
if(null!=file) {
file.delete();
file.deleteOnExit();
}
}
}
}