jfinal框架批量导出数据到Excel

1.配置controller

public void export(){
        Map<String, String[]> searchMap = new HashMap<String,String[]>(getParaMap());
        String sql = "select b.name,b.sex,b.card_number,b.address,b.lxdh,b.check_flag,b.sjly,d.value jflx,c.name jfdd,o.je,o.jfnf,o.lrr,o.create_time,e.value yhlx from zklt_sjjl o,zklt_id_card_info b,zklt_area c,sys_dct d,sys_dct e,sys_user f where o.sfzh=b.card_number and o.jfdd=c.id and o.jflx=d.key and b.yhlx=e.key and o.creater=f.id and d.group_id='82088c59e3584c27830c8a831d355150' and e.group_id='93bfd0284e0e47ddaf4f08f429b691d1' and f.status=1 and o.yxbs='1' and b.yxbs='1' and c.yxbs='1' ";
        String[] colCode =new String[] {"name","sex","card_number","address","jflx","jfdd","je","lrr","create_time","yhlx","lxdh","sjly"};
        String[] colName = new String[] {"姓名","性别", "身份证号", "地址", "缴费类型", "缴费地点", "金额", "填报人", "填报日期", "备注","联系电话","数据来源","说明"};
        File file = new File(ExcelExportUtil.getTitle());
        file = ExcelExportUtil.saveFile(colCode,colName, sql, file);
        renderFile(file);
    }

2.controller中使用的工具类:

 

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.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.IndexedColors;

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(String[] colCode,String[] codName, 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++;
            HSSFCellStyle cellStyle1 = hssfWorkbook.createCellStyle();
            cellStyle1.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
            cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            cellStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            cellStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            cellStyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            cellStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
            HSSFFont font = hssfWorkbook.createFont();
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle1.setFont(font);
            // 遍历标题
            for (String h : codName) {
                //创建列
                cell = row.createCell(cellIndex);
                //索引递增
                cellIndex++;
                //逐列插入标题
                cell.setCellValue(h);
                cell.setCellStyle(cellStyle1);
            }
            // 得到所有记录 行:列
            List<Record> list = Db.find(sql);
            Record record = null;
            HSSFCellStyle cellStyle2 = hssfWorkbook.createCellStyle();
            cellStyle2.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
            cellStyle2.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
            cellStyle2.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
            cellStyle2.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
            cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局:居中
            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 : colCode) {
                        cell = row.createCell(cellIndex);
                        cellIndex++;
                        //按照每条记录匹配数据
                        cell.setCellValue(record.get(h) == null ? "" : record.get(h).toString());
                        cell.setCellStyle(cellStyle2);
                    }
                }

//设置第2行第13列单元格的内容
                hssfSheet.getRow(1).createCell(12).setCellValue("数据来源:1-读卡器采集; 2-手工录入;3-底册;4-ocr");
            }
            //自动调整每一列宽度
            for(int i=0;i<colCode.length;i++) {
                hssfSheet.autoSizeColumn(i);
                hssfSheet.setColumnWidth(i, hssfSheet.getColumnWidth(i) * 17 / 10);
            }
            hssfSheet.autoSizeColumn(12);
            hssfSheet.setColumnWidth(12, hssfSheet.getColumnWidth(12) * 17 / 10);
            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;
        }
}
 

3.页面HTML按钮

<form action="#(ctx)/admin/zklt/payInfo/export" method="post">
                        <div class="form-group">
                            <div class="col-md-3">
                                <label class="col-sm-5 control-label">缴费年份</label>
                                <div class="col-sm-7">
                                    <input type="text" id="ssnf" name="ssnf" value="#(ssnf?ssnf:'')" class="form-control" readonly="readonly"
                                            onFocus="WdatePicker({isShowClear:false,dateFmt:'yyyy',startDate:'%y'})"> 
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-3">
                                <label class="col-sm-5 control-label">身份证号</label>
                                <div class="col-sm-7">
                                    <input id="sfzh" name="sfzh" class="form-control" type="text">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-9 text-right">
                                <button class="btn btn-mint" type="button" οnclick="refreshTable();"><i class="glyphicon glyphicon-search"></i> 搜索</button>&nbsp;
                                <button class="btn btn-mint" type="submit"><i class="glyphicon glyphicon-arrow-up"></i> 导出</button>
                            </div>
                        </div>
                    </form>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iamlzjoco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值