在本身的WEB项目中要用到导出Excel,因此结合网络上的资源写了一个本身的export 工具类。javascript
说明:html
JFinal 环境java
WEB项目web
JAVA后台生成非JS插件sql
好了,直接撸代码typescript
1.设置文件保存路径private static final String FILEPATH = PathKit.getWebRootPath() + File.separator + "upload" + File.separator ;//路径为webRoot/upload/
2.设置 文件名
public static String getTitle(){ Date date = new Date(); SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); String title=FILEPATH+dateFormat.format(date)+"_统计报表.xls"; return title; }
3.前台页面调用
<p Alignment="left"><a href="/admin/pay/export">导出数据</a>
4.contr oller处理并renderFile回去
public void export(){ String sql = "select * from `order`"; Map<String, String> titleData = new HashMap<String, String>();//标题,后面用到 titleData.put("order_no", "帐单号"); titleData.put("good_code", "商品编码"); titleData.put("size", "尺码"); titleData.put("number", "数量"); titleData.put("type", "类型"); titleData.put("order_time", "时间"); File file = new File(ExcelExportUtil.getTitle()); file = ExcelExportUtil.saveFile(titleData, sql, file); this.renderFile(file); }
5.工 具类全部代码
package com.feng.util; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.jfinal.kit.PathKit; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; public class ExcelExportUtil { private static final String FILEPATH = PathKit.getWebRootPath() + File.separator + "upload" + File.separator ; public static String getTitle(){ Date date = new Date(); SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); String title=FILEPATH+dateFormat.format(date)+"_统计报表.xls"; return title; } public static File saveFile(Map<String, String> headData, String sql, File file) { // 建立工做薄 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); // sheet:一张表的简称 // row:表里的行 // 建立工做薄中的工做表 HSSFSheet hssfSheet = hssfWorkbook.createSheet(); // 建立行 HSSFRow row = hssfSheet.createRow(0); // 建立单元格,设置表头 建立列 HSSFCell cell = null; // 初始化索引 int rowIndex = 0; int cellIndex = 0; // 建立标题行 row = hssfSheet.createRow(rowIndex); rowIndex++; // 遍历标题 for (String h : headData.keySet()) { //建立列 cell = row.createCell(cellIndex); //索引递增 cellIndex++; //逐列插入标题 cell.setCellValue(headData.get(h)); } // 获得全部记录 行:列 List<Record> list = Db.find(sql); Record record = null; if (list != null) { // 获取全部的记录 有多少条记录就建立多少行 for (int i = 0; i < list.size(); i++) { row = hssfSheet.createRow(rowIndex); // 获得全部的行 一个record就表明 一行 record = list.get(i); //下一行索引 rowIndex++; //刷新新行索引 cellIndex = 0; // 在有全部的记录基础之上,便利传入进来的表头,再建立N行 for (String h : headData.keySet()) { cell = row.createCell(cellIndex); cellIndex++; //按照每条记录匹配数据 cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString()); } } } try { FileOutputStream fileOutputStreane = new FileOutputStream(file); hssfWorkbook.write(fileOutputStreane); fileOutputStreane.flush(); fileOutputStreane.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return file; } }
5.最终效果apache