Java后台Excel表导出
Posted 一个九
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java后台Excel表导出相关的知识,希望对你有一定的参考价值。
导出数据到Excel:
1. 将数据查询出来返回给导出jsp页面。
//导出数据 @RequestMapping(value ="/chongLog_info_xls",produces="text/html;charset=UTF-8") public String consume_info_xls(String q_start_date,String q_end_date,Model map,HttpServletRequest request,String user_type,String q_tel,String status){ String q_str=""; if(q_tel!=null && !q_tel.equals("")){ q_str+=" and a.tel like ‘%"+q_tel+"%‘"; } if(StringUtils.isNotBlank(q_start_date)){ q_str+=" and to_char(a.insert_date,‘yyyy-MM-dd‘) >=‘"+q_start_date+"‘"; } if(StringUtils.isNotBlank(q_end_date)){ q_str+=" and to_char(a.insert_date,‘yyyy-MM-dd‘) <=‘"+q_end_date+"‘"; } String sql="select a.name a__name,a.tel a__tel,c.money c__money,c.insert_date c__insert_date,c.balance c__balance,c.balance+c.money c__after" + " from t_fg_user a,T_CHONG_LOG c where a.id=c.user_id " + " and 1=1 "+q_str+" order by c.insert_date desc"; List list=proxy_jyyzService.findall(sql); map.addAttribute("list", list); return "bg/finance/chongLog_info_xls"; }
2. 导出功能jsp文件配置
chongLog_info_xls.jsp:
<%@ page contentType="application/x-msexcel;" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@page import="java.util.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> td{ vnd.ms-excel.numberformat:@; border:0.5pt solid #c5c5c5; height:30px; word-break:break-all; word-wrap:break-word; } #tab_title td{background:#f5f5f5;} </style> </head> <% response.setHeader("Content-Disposition","filename=emp.xls"); String menu_id=request.getParameter("menu_id"); String cookie_menu=request.getParameter("cookie_menu"); %> <body> <%=AddExcelHead()%> <!-- 内容面板--开始 --> <table cellpadding="0" cellspacing="0" border="1" style="border:0px red solid;table-layout: fixed;"> <tr valign="middle"> <td style="font-size:18px;font-weight:700;border:none" width="30" height="50" align="center" colspan="7"> <div>系统充值明细</div> </td> </tr> <tr id="tab_title"> <td width=‘80‘>序号</td> <td width=‘80‘>昵称</td> <td width=‘100‘>手机号</td> <td width=‘100‘>充值金额</td> <td width=‘80‘>充值前金额</td> <td width=‘100‘>充值后金额</td> <td width=‘150‘>充值时间</td> </tr> <c:set value="0" var="t_money"></c:set> <c:set value="0" var="t_amount"></c:set> <c:forEach var="bk" items="${list }" varStatus="status"> <c:set value="${t_money+ bk.c__money}" var="t_money" ></c:set> <c:set value="${t_amount+ 1}" var="t_amount" ></c:set> <tr> <td>${status.index+1} <td>${bk.a__name} <td>${bk.a__tel} <td>${bk.c__money} <td>${bk.c__balance} <td>${bk.c__after} <td>${bk.c__insert_date} </c:forEach> <tr> <td colspan=‘7‘ width=‘100‘ style="text-align:left;font-size:14px;"> 合计:共 <span style="color:Red">${t_amount}</span> 个记录,充值总金额为 <span style="color:Red"><fmt:formatNumber type="number" value="${t_money}" pattern="0.00" maxFractionDigits="2"/> </span> 元。 </td> </tr> </table> <%=AddExcelbottom()%> </body> </html> <%! private static String AddExcelHead(){ StringBuffer sb = new StringBuffer(); sb.append("<html xmlns:x="urn:schemas-microsoft-com:office:excel">"); sb.append(" <head>"); sb.append(" <!--[if gte mso 9]><xml>"); sb.append("<x:ExcelWorkbook>"); sb.append("<x:ExcelWorksheets>"); sb.append("<x:ExcelWorksheet>"); sb.append("<x:Name>Sheet</x:Name>"); sb.append("<x:WorksheetOptions>"); sb.append("<x:Print>"); sb.append("<x:ValidPrinterInfo />"); sb.append(" </x:Print>"); sb.append("</x:WorksheetOptions>"); sb.append("</x:ExcelWorksheet>"); sb.append("</x:ExcelWorksheets>"); sb.append("</x:ExcelWorkbook>"); sb.append("</xml>"); sb.append("<![endif]-->"); sb.append(" </head>"); sb.append("<body>"); return sb.toString(); } private static String AddExcelbottom(){ StringBuffer sb = new StringBuffer(); sb.append("</body>"); sb.append("</html>"); return sb.toString(); } %>
以上是关于Java后台Excel表导出的主要内容,如果未能解决你的问题,请参考以下文章
如何让java利用POI导出excel表,并在Excel表中根据表格的数据生成柱形图。要求柱形图是动态的。