1. 程式人生 > >jfinal excel表導出

jfinal excel表導出

初始 ati output 獲取 void print 調用 ext class

在自己的WEB項目中要用到導出Excel,所以結合網絡上的資源寫了一個自己的export 工具類。

說明:

  • JFinal 環境

  • WEB項目

  • JAVA後臺生成非JS插件

好了,直接擼代碼

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.最終效果

技術分享

技術分享

jfinal excel表導出