需求:将用户的违约金信息导出为excel表格格式

步骤

1. 数据库中增加按钮的值(注意上级编号要和页面隐藏域中的相等)

DZ内容(页面加载时根据SJBH查询数据库内容,读取DZ字段信息并加载样式及方法)

<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="getWyj();">违约金查询</a>

页面加载后菜单栏出现“违约金查询”按钮

2. 创建js方法

sf_wyjcx.js

function getWyj(){
var userNode = frames['threeModel'].$("#topUserList").datagrid("getSelected");
if (!userNode) {
pubAlertMessage({
"msg" : "请选择用户信息!",
"type" : BDXC_ALERT_TYPE.INFO
});
return;
}
var yhbh = userNode.YHBH; var postData = {
'jmjs.yhbh' : yhbh
};
var paramJson = {
"winDivId" :"getWyjWin",
"title" :"违约金查询",
"width" :920,//300, // 790
"height" :400, //
"iconCls" :"icon-edit",
"inDivId" :"getWyj",
"loadUrl" :"UserPayAction!getWyj.action",
"postData" :postData,
"watingTime":100,
"defineFun":"exportExcel("+yhbh+")",
"initFunc":""
};
//pubLoadWinPage(paramJson);
pubLoadWyj(paramJson);
}
function pubLoadWyj(paramJson) {
var watingStartJson = {
"time" : paramJson.watingTime
};
pubWatingStart(watingStartJson);
$.ajax({
url : paramJson.loadUrl,
type : 'post',
data : paramJson.postData,
dataType : 'html',
error : function (e) {
pubWatingEnd();
pubAlertMessage({
"msg" : "加载页面失败!",
"type" : BDXC_ALERT_TYPE.ERROR
});
},
success : function (data) {
pubWatingEnd();
var winDiv = $('#' + paramJson.winDivId);
if (winDiv.length == 0) {
$('<div id="' + paramJson.winDivId + '"></div>').appendTo(document.body);
}
var canelFun = "pubWinClose('" + paramJson.winDivId + "','');";
if (paramJson.canelFun) {
canelFun = "pubWinClose('" + paramJson.winDivId + "','" + paramJson.canelFun + "');";
}
var inDivId = paramJson.inDivId || paramJson.winDivId + 'Content';
paramJson.inDivId = inDivId;
paramJson.winHtml = '<div class="easyui-layout" fit="true">' +
'<div id="' + paramJson.inDivId + '" region="center" border="false" class="winDataContent">' + data +
'</div>';
if (!paramJson.isNormal) {
paramJson.winHtml += '<div region="south" border="false" class="winOpLine" style="float:right">' +
'<a id="btnCancel" class="easyui-linkbutton" icon="icon-cancel" href="javascript:' + canelFun + '">关闭</a>' +
'<a id="btnEp" class="easyui-linkbutton" icon="icon-ok" href="javascript:' + paramJson.defineFun + '" >导出</a>' +
'</div>';
}
paramJson.winHtml += '</div>';
pubInitWindow(paramJson);
}
});
}
//导出表格
function exportExcel(yhbh){
var url = "UserPayAction!getWyjExcel.action?yhbh="+yhbh;
$.messager.confirm("操作提示","是否导出违约金信息?",function(data){
if(data){
location.href = url;
}
});
}

3. 点击菜单栏的“违约金查询”首先加载用户所有的违约金信息

后台方法如下:

UserPayAction.java

    // 查询违约金
public String getWyj(){
jmjs.setYhbh(jmjs.getYhbh());
List list=userPayService.getWyjlist(jmjs);
jmjs.setList(list);
return "addwyj";
}

UserPayDaoImpl.java

public  List getWyjlist(SfJmjsT vo){
List list = new ArrayList();
StringBuilder sql = new StringBuilder();
List result = null;
sql.append("select a.*,b.*,c.* from (select cnq,sfmj,dj,ysje,sfje,qfje,decode(sfhjwyj,'否',SF_ZNJ_F_CFD('居民',yhbh,?,cnq),znjys)znj," +
"SF_ZNJ_F_TS(yhbh,?,cnq)ts from sf_jmjs_t where zf=0 and qfje>0 and yhbh=? )a," +
"(select sum(qfje) zqf,sum(decode(sfhjwyj,'否',SF_ZNJ_F_CFD('居民',yhbh,?,cnq),znjys))zznj from sf_jmjs_t " +
"where zf=0 and qfje>0 and yhbh=?)b,(select yhbm,yhkh,yhmc,lxdh,fgs,qy,rlz,xq,dz from sf_jmyh_t where zf=0 and yhbh=?)c");
Date da=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(da);
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date date=null;
try {
date = format1.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
list.add(date);
list.add(date);
list.add(vo.getYhbh());
list.add(date);
list.add(vo.getYhbh());
list.add(vo.getYhbh());
try {
result = this.executeSqlList(sql.toString(), list.toArray(), SfJmjsT.class);
} catch (BDXCException e) {
e.printStackTrace();
logger.error(BDXCUtil.getExceptionString(e));
}
return result;
}

返回的数据在页面进行遍历

sf_wyjcx.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript" src="js/common/validate.js"></script>
<script type="text/javascript">
</script>
<form id="getWyjForm" method="post" action="">
<s:token></s:token>
<div>
<table>
<tbody>
<fieldset class="fieldsetCss" style="width:95%;">
<legend class="legendCss" >违约金信息</legend>
<table id="getWyj" border="1">
<tr>
<td align="center">采暖期</td>
<td align="center">用户编码</td>
<td align="center">用户名称</td>
<td align="center">联系电话</td>
<td align="center">地址</td>
<td align="center">收费面积</td>
<td align="center">单价</td>
<td align="center">应收金额</td>
<td align="center">实收金额</td>
<td align="center">欠费金额</td>
<td align="center">违约金</td>
<td align="center">违约金天数</td>
</tr>
        <!--中间栏对含违约金结算信息进行遍历-->
<s:iterator value="jmjs.list" var="b">
<tr>
<td class="contentCss" align="center">${b.cnq }</td>
<td class="contentCss" align="center">${b.yhbm }</td>
<td class="contentCss" align="center">${b.yhmc }</td>
<td class="contentCss" align="center">${b.lxdh }</td>
<td class="contentCss" align="center">${b.dz }</td>
<td class="contentCss" align="center">${b.sfmj }</td>
<td class="contentCss" align="center">${b.dj }</td>
<td class="contentCss" align="center">${b.ysje }</td>
<td class="contentCss" align="center">${b.sfje }</td>
<td class="contentCss" align="center">${b.qfje }</td>
<td class="contentCss" align="center">${b.znj }</td>
<td class="contentCss" align="center">${b.ts }</td>
</tr>
</s:iterator> <tr>
<td class="contentCss" align="center">合计:</td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center"></td>
<td class="contentCss" align="center">${b.zqf }</td>
<td class="contentCss" align="center">${b.zznj }</td>
<td class="contentCss" align="center"></td>
</tr>
</table>
</fieldset>
</tbody>
</table>
</div>
</form>

页面显示效果

4. 导出

UserPayAction.java

 /**
* 导出违约金信息
*/
public String getWyjExcel(){
String yhbh = this.getRequest().getParameter("yhbh");
wyjExcelIs = userPayService.getWyjExcelIs(context,yhbh);
return "wyjExcel";
} /**
* 违约金导出表格名称*/
public String getWyjExcelName() {
String wyjExcelName = null;
try {
wyjExcelName = new String("违约金信息.xls".getBytes(),
"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
logger.error(BDXCUtil.getExceptionString(e));
}
return wyjExcelName;
} public InputStream getWyjExcelIs() {
return wyjExcelIs;
} public void setWyjExcelIs(InputStream wyjExcelIs) {
this.wyjExcelIs = wyjExcelIs;
}

struts.xml

<result name="wyjExcel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">wyjExcelIs</param>
<param name="contentDisposition">
attachment;fileName = "${wyjExcelName}"
</param>
<param name="bufferSize">1024</param>
</result>

UserPayServiceImpl.java

    /**
* 导出违约金信息
*
* @return 违约金信息文件的输入流
*/
public InputStream getWyjExcelIs(BDXCPageContext context, String yhbh) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
HSSFWorkbook workbook = new HSSFWorkbook();
//头标题样式
HSSFCellStyle style1 = workbook.createCellStyle(); // 样式对象
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
HSSFFont f1 = workbook.createFont(); f1.setFontName("宋体");
f1.setFontHeightInPoints((short) 20);//字号
f1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
style1.setFont(f1);
//表头样式
HSSFCellStyle style2 = workbook.createCellStyle(); // 样式对象
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style2.setWrapText(true);
style2.setBorderBottom((short) 1);//下边框
style2.setBorderLeft((short) 1);//左边框
style2.setBorderRight((short) 1);//右边框
style2.setBorderTop((short) 1);//上边框 HSSFFont f2 = workbook.createFont();
f2.setFontName("宋体");
f2.setFontHeightInPoints((short) 12);//字号
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
style2.setFont(f2);
//表体样式
HSSFCellStyle style3 = workbook.createCellStyle(); // 样式对象
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style3.setWrapText(true);
style3.setBorderBottom((short) 1);//下边框
style3.setBorderLeft((short) 1);//左边框
style3.setBorderRight((short) 1);//右边框
style3.setBorderTop((short) 1);//上边框 HSSFFont f3 = workbook.createFont();
f3.setFontName("宋体");
f3.setFontHeightInPoints((short) 10);//字号
style3.setFont(f3); //添加工作表
HSSFSheet sheet = workbook.createSheet("违约金信息");
//设置默认行高
sheet.setDefaultRowHeightInPoints(25);
//设置工作表列宽
for (short j = 0; j < 12; j++) {
if(j==4){
sheet.setColumnWidth(j, (short) (20 * 256));
}else if(j==11){
sheet.setColumnWidth(j, (short) (16 * 256));
}else{
sheet.setColumnWidth(j, (short) (12 * 256));
}
}
//添加表头信息
List<String> headList = new ArrayList<String>();
headList.add("采暖期");
headList.add("用户编码");
headList.add("用户名称");
headList.add("联系电话");
headList.add("地址");
headList.add("收费面积");
headList.add("单价");
headList.add("应收金额");
headList.add("实收金额");
headList.add("欠费金额");
headList.add("违约金");
headList.add("违约金天数");
//第一行 头标题
HSSFRow nRow1 =sheet.createRow(0);
nRow1.setHeight((short) (30 * 20));
HSSFCell ncell1 = nRow1.createCell((short) 0);
ncell1.setCellValue("违约金信息");
ncell1.setCellStyle(style1);
ncell1.setCellType(HSSFCell.CELL_TYPE_STRING);
sheet.addMergedRegion(new Region(0,(short)0,0,(short)11));
//第二行 表头
HSSFRow nRow2 =sheet.createRow(1);
nRow2.setHeight((short) (25 * 20));
for (short k = 0; k < 12; k++) { HSSFCell cell = nRow2.createCell(k);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(style2);
cell.setCellValue(headList.get(k));
}
//表体
SfJmjsT jmjs = new SfJmjsT();
jmjs.setYhbh(Long.parseLong(yhbh));
List list=getWyjlist(jmjs);
for (int i = 2; i < list.size()+2; i++) {
SfJmjsT js = (SfJmjsT) list.get(i-2);
HSSFRow Row =sheet.createRow(i);
Row.setHeight((short) (25 * 20));
//采暖期
HSSFCell xcell = Row.createCell((short)0);
xcell.setCellType(HSSFCell.CELL_TYPE_STRING);
xcell.setCellStyle(style3);
xcell.setCellValue(js.getCnq());
//用户编码
HSSFCell xcel2 = Row.createCell((short)1);
xcel2.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel2.setCellStyle(style3);
xcel2.setCellValue(js.getYhbm());
//用户名称
HSSFCell xcel3 = Row.createCell((short)2);
xcel3.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel3.setCellStyle(style3);
xcel3.setCellValue(js.getYhmc());
//联系电话
HSSFCell xcel4 = Row.createCell((short)3);
xcel4.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel4.setCellStyle(style3);
xcel4.setCellValue(js.getLxdh());
//地址
HSSFCell xcel5 = Row.createCell((short)4);
xcel5.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel5.setCellStyle(style3);
xcel5.setCellValue(js.getDz());
//收费面积
HSSFCell xcel6 = Row.createCell((short)5);
xcel6.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel6.setCellStyle(style3);
xcel6.setCellValue(js.getSfmj());
//单价
HSSFCell xcel7 = Row.createCell((short)6);
xcel7.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel7.setCellStyle(style3);
xcel7.setCellValue(js.getDj());
//应收金额
HSSFCell xcel8 = Row.createCell((short)7);
xcel8.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel8.setCellStyle(style3);
xcel8.setCellValue(js.getYsje());
//实收金额
HSSFCell xcel9 = Row.createCell((short)8);
xcel9.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel9.setCellStyle(style3);
xcel9.setCellValue(js.getSfje());
//欠费金额
HSSFCell xcel10 = Row.createCell((short)9);
xcel10.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel10.setCellStyle(style3);
xcel10.setCellValue(js.getQfje());
//违约金
HSSFCell xcel11 = Row.createCell((short)10);
xcel11.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel11.setCellStyle(style3);
xcel11.setCellValue(js.getZnj());
//违约金天数
HSSFCell xcel12 = Row.createCell((short)11);
xcel12.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel12.setCellStyle(style3);
xcel12.setCellValue(js.getTs());
}
//表尾 合计
SfJmjsT hjJs = (SfJmjsT) list.get(0);
HSSFRow lastRow =sheet.createRow(list.size()+2);
lastRow.setHeight((short) (25 * 20));
//采暖期
HSSFCell xcell = lastRow.createCell((short)0);
xcell.setCellType(HSSFCell.CELL_TYPE_STRING);
xcell.setCellStyle(style2);
xcell.setCellValue("合计:");
//用户编码
HSSFCell xcel2 = lastRow.createCell((short)1);
xcel2.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel2.setCellStyle(style2);
xcel2.setCellValue("");
//用户名称
HSSFCell xcel3 = lastRow.createCell((short)2);
xcel3.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel3.setCellStyle(style2);
xcel3.setCellValue("");
//联系电话
HSSFCell xcel4 = lastRow.createCell((short)3);
xcel4.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel4.setCellStyle(style2);
xcel4.setCellValue("");
//地址
HSSFCell xcel5 = lastRow.createCell((short)4);
xcel5.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel5.setCellStyle(style2);
xcel5.setCellValue("");
//收费面积
HSSFCell xcel6 = lastRow.createCell((short)5);
xcel6.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel6.setCellStyle(style2);
xcel6.setCellValue("");
//单价
HSSFCell xcel7 = lastRow.createCell((short)6);
xcel7.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel7.setCellStyle(style2);
xcel7.setCellValue("");
//应收金额
HSSFCell xcel8 = lastRow.createCell((short)7);
xcel8.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel8.setCellStyle(style2);
xcel8.setCellValue("");
//实收金额
HSSFCell xcel9 = lastRow.createCell((short)8);
xcel9.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel9.setCellStyle(style2);
xcel9.setCellValue("");
//欠费金额
HSSFCell xcel10 = lastRow.createCell((short)9);
xcel10.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel10.setCellStyle(style2);
xcel10.setCellValue(hjJs.getZqf());
//违约金
HSSFCell xcel11 = lastRow.createCell((short)10);
xcel11.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
xcel11.setCellStyle(style2);
xcel11.setCellValue(hjJs.getZznj());
//违约金天数
HSSFCell xcel12 = lastRow.createCell((short)11);
xcel12.setCellType(HSSFCell.CELL_TYPE_STRING);
xcel12.setCellStyle(style2);
xcel12.setCellValue(""); workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ByteArrayInputStream(out.toByteArray());
}

效果(有些版本的谷歌浏览器导出时可能不跳出弹窗,直接下载,避免这种情况可以使用火狐浏览器)

excel表格

利用POI进行Excel的导出的更多相关文章

  1. 利用POI获取Excel中图片和图片位置

    利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833 ...

  2. SpringBoot集成文件 - 集成POI之Excel导入导出

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能.本文主要介绍通过Spr ...

  3. 纳税服务系统【用户模块之使用POI导入excel、导出excel】

    前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...

  4. Java之POI的excel导入导出

    一.Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显示MS Office文件.这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Offic ...

  5. 【原创】POI操作Excel导入导出工具类ExcelUtil

    关于本类线程安全性的解释: 多数工具方法不涉及共享变量问题,至于添加合并单元格方法addMergeArea,使用ThreadLocal变量存储合并数据,ThreadLocal内部借用Thread.Th ...

  6. Java利用POI生成Excel强制换行

    前一段时间在做一个学校排课系统时,有一个地方需要利用把课程表生成excel汇出给客户,由于之前用excel都只是简单的应用,在单元格里都是用自动换行,而这次可能需要用到手动强制换行. 于是我在网上找了 ...

  7. 基于POI的Excel导入导出(JAVA实现)

    今天做了个excel的导入导出功能,在这记录下. 首先现在相关poi的相关jar包,资源链接:http://download.csdn.net/detail/opening_world/9663247 ...

  8. POI实现Excel导入导出

    我们知道要创建一张excel你得知道excel由什么组成,比如说sheet也就是一个工作表格,例如一行,一个单元格,单元格格式,单元格内容格式…这些都对应着poi里面的一个类. 一个excel表格: ...

  9. Java 利用poi生成excel表格

    所需jar包,如下所示 写一个excel工具类 ExcelUtils .java import java.lang.reflect.Field; import java.util.Iterator; ...

随机推荐

  1. 排序小记【2】对 struct 的排序

    有了前面的内容,对于一般的排序已经没有问题了,但是有时候排序的要求可能会有点刁... 举个简单的例子,应该是NOIP2009的分数线划定,差不多算是一个比较高级的排序(吧). 多关键字排序(?) 我一 ...

  2. 「HNOI2013」游走

    「HNOI2013」游走 题目描述 一个无向连通图,顶点从 \(1\) 编号到 \(N\) ,边从 \(1\) 编号到 \(M\) .小 \(Z\) 在该图上进行随机游走,初始时小 \(Z\) 在 \ ...

  3. ZOJ 3057 Beans Game 博弈论 sg函数

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3057 典型的sg函数,数据范围卡得真好啊 代码 #include<c ...

  4. hdu 1384 Intervals (差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. 【思路】Gym - 101173F - Free Figurines

    套娃形成一些链形结构,给你套娃的初始状态和目标状态,问你需要几步(将最外层套娃打开,以及将一整套套娃塞进一个空套娃都算一步)才能达到. 容易发现,只有每条链链尾的匹配段可以不拆,其他的都得拆开. #i ...

  6. vue组件续和前端工程化

    1.3 插槽 slot template: ` <button> <slot></slot> </button> ` <my-button> ...

  7. lightoj 1229 - Treblecross 博弈论

    思路:SG函数 枚举先手的每一个位置是否有必胜. 1)如果出现了XXX则必胜: 2)如果出现了XX或X.X则必败: 3)否则计算后手的sg值和. 代码如下: #include<iostream& ...

  8. Problem E: 调用函数,整数逆序输出

    #include<stdio.h> int reverse(int number)//定义函数 { ;//result用于储存结果 ) { result=result*; i=number ...

  9. [NOIP2011]聪明的质检员

    [问题描述] 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有$n$个矿石,从 1 到$n$逐一编号,每个矿石都有自己的重量$w_i$以及价值$v_i$.检验矿产的流程是: 1. 给 ...

  10. 【数据结构】【平衡树】浅析树堆Treap

    [Treap] [Treap浅析] Treap作为二叉排序树处理算法之一,首先得清楚二叉排序树是什么.对于一棵树的任意一节点,若该节点的左子树的所有节点的关键字都小于该节点的关键字,且该节点的右子树的 ...