jfinal框架实现导出数据库

maven

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

 前端代码

<p Alignment="left"><a href="/admin/customer/export">导出数据</a>

  工具类

package com.jfinal.club._admin.customer;

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 CustomerExcelExportUtil {

    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("select * from customer");
        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;
    }
}

controller

public void export(){
        String sql = "select * from `customer`";
        Map<String, String> titleData = new HashMap<String, String>();//标题,后面用到
        titleData.put("uid", "客户ID");
        titleData.put("pid", "上级ID");
        titleData.put("loginid", "登录名");
        titleData.put("tel", "手机号");
        titleData.put("level", "客户等级");
        titleData.put("status", "账户状态");
        titleData.put("realname", "真实姓名");
        titleData.put("merchant", "是否为商家");
        titleData.put("usdt", "USDT");
        titleData.put("generation", "代数");
        titleData.put("path", "路径");
        titleData.put("balance", "保证金");
        titleData.put("num", "直推人数");
        titleData.put("nums", "团队人数");
        titleData.put("resumption", "是否复投");
        titleData.put("cdeal", "可进行OTC交易的奖励");
        titleData.put("ntasks", "总任务数");
        titleData.put("extasks", "任务状态");
        titleData.put("cassets", "链上资产");
        titleData.put("reson", "锁仓金额");
        titleData.put("djprice", "冻结金额");
        titleData.put("zgkc", "最高库存");
        titleData.put("xzc", "新用户注册");
        titleData.put("mrtz", "每日投资总额");
        titleData.put("created_time", "创建时间");
        File file = new File(CustomerExcelExportUtil.getTitle());
        file = CustomerExcelExportUtil.saveFile(titleData, sql, file);
        this.renderFile(file);
    }

修改一下工具类的sql文件改成要导出的数据库的表就可以了

猜你喜欢

转载自www.cnblogs.com/fanxu3/p/11484299.html