jsp:
<a id="excelExport"></a>
js:
var excel=function(){ var node = $('#groupConfig').tree('getSelected'); if(node!=null) var groupConfigSearch =node.id; else node=null; var data = { orgCode:groupConfigSearch, helpType:$("#searchType").combobox('getValue'), name:$("#searchName").val(), youthTel:$("#searchTel").val() }; $.ajax({ url:basepath+'/hk/importExcel.do', type:'POST', dataType:'json', data:data, success:function(data) { console.log(data); console.log(data.data); console.log(data.success); $("#excelExport").attr("href",data.data); document.getElementById("excelExport").click(); $('#searchDiv').form('clear'); } }); }
sql:
<select id="getExcel" parameterClass="hk" resultClass="com.fjxhx.business.people.model.HkModel" > select id,bankId,youthName,youthTel, projectName,appropriationDate,appropriationMoney,subsidizeMonth,firstDate,everyMoney,isCountryOrg, monthlyMoney,sumTheoryMoney,sumRealMoney,sumBalance,notOntime,youthId, (select b.value from b_status b where b.content=a.helpType and b.code='helpType') as helpType, (select b.value from b_status b where b.content=a.projectPlace and b.code='projectPlace') as projectPlace, (select c.orgName from ybc_org c where c.orgCode=a.orgCode) as orgCode from ybc_hk a where 1=1 <dynamic> <isNotEmpty prepend="and" property="orgCode"> orgCode like '%$orgCode$%' </isNotEmpty> <isNotEmpty prepend="and" property="helpType"> helpType = #helpType# </isNotEmpty> <isNotEmpty prepend="and " property="youthName"> youthName like concat('%', #youthName#, '%') </isNotEmpty> <isNotEmpty prepend="and" property="youthTel"> youthTel like concat('%', #youthTel#, '%') </isNotEmpty> </dynamic> ORDER BY id DESC </select>
action:
public void importExcel(){ try { BaseDao dao = getDao(); List<Object> list = dao.selectForList("hk.getExcel", model); String basepath=ServletActionContext.getServletContext().getRealPath("templateFile"); String s1=basepath+"/hk.xls";//模板 String s2=basepath+"/hkTemp.xls"; ExcelUtil.exportToExcel(list, "第一页",s1,s2); this.setData("servlet/downloadServlet?path="+s2); this.setSuccess(true); this.send(); } catch (Exception e) { e.printStackTrace(); this.setData("操作失败"); this.setSuccess(false); this.send(); } }
servelt:
package com.fjxhx.business.system.servlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class downloadServlet extends HttpServlet { /** * Constructor of the object. */ public downloadServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path=request.getParameter("path"); /** * 如果下载成功删除模版文件 */ System.out.println(path); if(download(path, response)) deleteFile(path); } public static boolean deleteFile(String fileName) { File file = new File(fileName); // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除 if (file.exists() && file.isFile()) { if (file.delete()) { System.out.println("删除单个文件" + fileName + "成功!"); return true; } else { System.out.println("删除单个文件" + fileName + "失败!"); return false; } } else { System.out.println("删除单个文件失败:" + fileName + "不存在!"); return false; } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } private boolean download(String path, HttpServletResponse response) { boolean flag=false; try { // path是指欲下载的文件的路径。 File file = new File(path); // 取得文件名。 String filename = file.getName(); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(path)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream( response.getOutputStream()); response.setContentType("application/vnd.ms-excel;charset=utf-8"); toClient.write(buffer); toClient.flush(); toClient.close(); flag=true; } catch (IOException ex) { ex.printStackTrace(); } return flag; } }
web.xml
<servlet-mapping> <servlet-name>downloadServlet</servlet-name> <url-pattern>/servlet/downloadServlet</url-pattern> </servlet-mapping> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>downloadServlet</servlet-name> <servlet-class>com.fjxhx.business.system.servlet.downloadServlet</servlet-class> </servlet>
模板:
中文
model里的属性
jar:
相关推荐
Java导出数据到Excel文件中,支持多页签形式,如通过Java导出一个名为“各部门人员列表”,然后在文件中有三个页签,分别为“研发部”、“综合部”、“财务部”。其中这三个页签里面的数据就是通过Java导出到Excel...
一个将数据库数据导出生成excel文件的工具,支持各种数据库
Excelvc—VC++ 灵活操作Excel导入导出数据,不但可以生成Excel文件,还可以从Excel文件导入数据到VC的ListCtrl控件中
QT导出EXCEL,从tableWidget列表获取数据导出到excel文件。导出过程使用了excel模板文件,可以从tableWidget读取固定的几列数据导出到excel文件中。其中excel事先设置好文件格式,后续直接读数写数即可。开发过程...
支持从数据库中查询到数据,以多级表头形式导入到Excel文件中。比如:一级表头:学号、姓名、各科成绩。二级表头:语文、数学、英语(二级表头在各科成绩下面)。
1、Java导出动态数据为Excel文件,具体示例可以参考:https://img-blog.csdnimg.cn/1cc86ee5dffa48669e2b97283585fad2.png 2、项目使用SpringBoot,Ali3.0.5版本的easyexcel框架。 3、资源内有具体的使用说明和易...
可以把数据库中的数据导出,生成excel文件,或者把txt文件按规定格式导出excel文件的引用jar包
怎么定时自动将SQL数据导出为Excel文件
ThinkPHP实现数据导出为Excel文件的PHPExcel类库文件
30万的数据量,生成多个excel最后打包成zip下载,是工程,直接导入就行
本文实例为大家分享了Winform实现导入导出Excel文件的具体代码,供大家参考,具体内容如下 /// /// 导出Excel文件 /// /// /// <param name=dataSet></param> /// 数据集 /// 导出后是否打开文件 /// ...
SQL Server导出表到EXCEL文件的存储过程,整理sql如何把表导出
使用脚本或者批处理工具,完美的将读取到的数据库内容生成到Excel文件,不用编程,下载后可直接使用
根据不同的省份生成多个execl,每个execl中根据不同的年份生成不同的sheet页
C#快速导出Excel文件,3秒可导10万行数据
SpringBootMybatis+poi+Thymeleaf实现excel文件数据导入到数据库以及从数据库将数据导出成excel
直接可以用的项目。数据量大,导出多个excel然后压缩成zip文件。。。
可手动输入SQL语句查询/导出数据到Excel 可从SQL文件查询/导出数据到Excel 到处结果直接生成Excel文件,支持多条语句同时导出
当有需求,需要把当天的报表文件发送给指定邮箱又不小生成多余的...实现过程是先将数据库里的数据,导出excel文件输出流中(注:不是直接生成文件),发送邮件时附件内容直接从前面excel文件输出流中获取,然后发送邮件