对于jfinal导出的源码

对于导出,推荐看这个博客 http://blog.csdn.net/confusion_ct/article/details/31374529
当然这个导出来的可能只是一条数据 那么我们就可以在便利的时候便利多行,另外还有个问题是循环的时候一定要循环从1开始 因为从0的话他就会把表头覆盖
以下是我的源码:

//poi导出
public void downloadExcl(){
// Demo1();
Date d=new Date();
System.out.println("121");
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
String title=dateFormat.format(d);
System.out.println(title);
File file = new File(title+"_统计表"+".xls");
String sql="select * from bee_product"; //表名
//ider:外部传入一个表头、SQL语句、
String s[] = new String[] { "id", "company_id", "product_name",
"term", "term_status", "base_profit", "extra_profit1","extra_profit2","coupon_pools","is_soldout","total_amount",
"current_amount","bought","min_investment","max_investment",
"is_virtual","is_show","del_flag","start_date","end_date",
"is_continue","is_continue_product","continue_status",
"wlc_product_id","product_code","product_tip","product_desc","xieyi_hylink",
"agreement_id","remark","product_type","product_place","buy_hylink","group_relation",
"is_redeem","borrow_page"}; //表字段 因为他是从sql字段里取出来的数据
String s1[] = new String[] { "id", "公司id", "产品名称",
"期限(天)", "单位时间", "基础收益率", "额外收益1","额外收益2","卡券","是否在售","发行总额度",
"发行数量","购买人数","最小起投金额","最大起投金额",
"是否虚拟","是否显示","是否删除","开始时间","下架时间",
"订单续投","产品续投","选择续投状态",
"玖富产品id","产品编号","温馨提示","产品说明","协议的链接",
"各种证书协议id","评论","产品类型","产品所在位置","购买链接","相同或类似产品的关联",
"是否允许提前赎回 ","借款信息页码"}; //这个是我们因为我们想要中文表头 因为是从字段里取出来值,为了不冲突,故有写一个,另外起个变量名,然后便利表头的时候便利他的变量名
saveFile(s,sql, file ,s1);
renderFile(file);
}
//新增一行就累加
private int count = 0;
public void saveFile(String[] s,String sql, File file,String[] s1) {
// 创建工作薄
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// sheet:一张表的简称
// row:表里的行
// 创建工作薄中的工作表
HSSFSheet hssfSheet = hssfWorkbook.createSheet("产品表");
// 创建行
HSSFRow row = hssfSheet.createRow(0);
// 创建单元格,设置表头 创建列
HSSFCell cell = null;
//便利表头
for (int i = 0; i < s1.length; i++) {
//创建传入进来的表头的个数
cell = row.createCell(i);
//表头的值就是传入进来的值
cell.setCellValue(s1[i]);
}
//新增一个行就累加
row = hssfSheet.createRow(++count);
// 得到所有记录 行:列
List<Record> list = Db.find(sql);
Record record = null;
if (list != null) {
//获取所有的记录 有多少条记录就创建多少行
for (int i = 1; i < list.size(); i++) { //i一定要从1开始,从0的话会覆盖表头
/* //把得到的所有行便利出来*/
row = hssfSheet.createRow(i); //这一行是我便利的,自行添加的 要不然只会出来一条数据
// 得到所有的行 一个record就代表 一行
record = list.get(i);
//在有所有的记录基础之上,便利传入进来的表头,再创建N行
for (int j = 0; j < s.length; j++) {
cell = row.createCell(j);
//把每一行的记录再次添加到表头下面 如果为空就为 "" 否则就为值
cell.setCellValue(record.get(s[j])==null?"":record.get(s[j]).toString());
}
}
}
try {
FileOutputStream fileOutputStreane = new FileOutputStream(file);
hssfWorkbook.write(fileOutputStreane);
fileOutputStreane.flush();
fileOutputStreane.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值