众说周知,java导出excel表格到EXCEL的方式有两种。其一是利用第三方插件JXL实现excel文件的生成,另一种方式则是不需要第三方的插件,直接通过jsp页面的设置和action层的response跳转完成excel文件的生成。综合来讲,采用第三方插件不仅能够满足功能性需求,而且还提供函数、字体、颜色及其他方面的接口,如果直接采用jsp跳转形式,则样式会略显低调,但是其实现形式很容易理解,无需了解更多的技术层面的东西。
那么我下面介绍的就是使用jxl实现文件的导出功能。
首先是从前端传值过来!前端的大多大同小异。我就不介绍了。
function exportExcel(){
var xmdw_input = KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲xmdw_input').va…(’#start_input’).val();
var end_input=$(’#end_input’).val();
var params = “?start_input=”+start_input+
“&end_input=”+end_input+
“&xmdw_input=”+xmdw_input;
window.location.href="/ADXMGL/zhwy/zhwy-xmgl-manage!exportXmtj.action"+params;
}
下面就是后端的代码。
public String exportXmtj() throws Exception {
try {
String xmdw = getParameter(“xmdw_input”);
String start = getParameter(“start_input”);
String end = getParameter(“end_input”);

     PropertyFilter dwPf = new PropertyFilter("xmdw:LIKE_S", xmdw);PropertyFilter startPf = new PropertyFilter("RKSJ:GE_D", start);PropertyFilter endPf = new PropertyFilter("RKSJ:LE_D", end);List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();pfList.add(dwPf);pfList.add(startPf);pfList.add(endPf);//List<PropertyFilter> pfList = initParam();List<String> colList = new ArrayList<String>();//colList.add("guId");int startPage=this.page.getPageNo()*this.page.getPageSize()+1;int endPage=(this.page.getPageNo()-1)*this.page.getPageSize();colList.add("xmdw");colList.add("xmsl");colList.add("cbzj");colList.add("dwzj");colList.add("ml");colList.add("mll");List<Map<String, Object>> mapList = this.zhwyXmglManageService.findMapLists(colList, pfList);exportXmtj(mapList);} catch (Exception e) {e.printStackTrace();}return null;
}
然后是导出的时候,创建一个文件。将需要的东西传到这个表格里面去。
public List<PropertyFilter> initParam() {String status = getParameter("status");String create_start_date = getParameter("create_start_date");String create_end_date = getParameter("create_end_date");String flog = getParameter("flog");UumUser currUser = (UumUser) this.getSessionAttribute(BasicConstant.CURRUSER);List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();if (create_start_date != null) {pfList.add(new PropertyFilter("rksj:GE_D", create_start_date));}if (create_end_date != null) {pfList.add(new PropertyFilter("rksj:LE_D", create_end_date));}return pfList;
}public String exportXmtj(List<Map<String, Object>> mapList) throws Exception {WritableFont font1 = new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);WritableCellFormat cellFormat1 = new WritableCellFormat(font1);//设置背景颜色;//cellFormat1.setBackground(Colour.BLUE_GREY);//设置边框;//cellFormat1.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);//设置自动换行;cellFormat1.setWrap(true);//设置文字居中对齐方式;cellFormat1.setAlignment(Alignment.CENTRE);//设置垂直居中;cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);WritableFont font2 = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);WritableCellFormat cellFormat2 = new WritableCellFormat(font2);cellFormat2.setAlignment(Alignment.CENTRE);cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);cellFormat2.setWrap(true);SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式//String name=df.format(new Date()) + ".xls";String path = this.getRealRoot() + "template\\price-v3" + new Date().getTime() + ".xls";WritableWorkbook book = null;File template = new File(path);book = Workbook.createWorkbook(template);        // 设置表名WritableSheet sheet = book.createSheet("表格", 0);// WritableSheet sheet= new WritableSheet();sheet.setColumnView(0, 20);for (int i = 0; i < 6; i++) {sheet.setColumnView(i, 20);}// 生成表格表头信息Label labe0 = new Label(0, 0, "项目统计", cellFormat1);Label labe1 = new Label(0, 1, "项目单位", cellFormat2);Label labe2 = new Label(1, 1, "项目数量", cellFormat2);Label labe3 = new Label(2, 1, "成本总价", cellFormat2);Label labe4 = new Label(3, 1, "对外总价", cellFormat2);Label labe5 = new Label(4, 1, "毛利", cellFormat2);Label labe6 = new Label(5, 1, "毛利率", cellFormat2);Label labe7 = new Label(0, mapList.size()+2,"-合计-", cellFormat2);//遍历表头信息sheet.mergeCells(0, 0, 5, 0);sheet.addCell(labe0);sheet.addCell(labe1);sheet.addCell(labe2);sheet.addCell(labe3);sheet.addCell(labe4);sheet.addCell(labe5);sheet.addCell(labe6);sheet.addCell(labe7);int i = 2;//求和for (int j=0;j<mapList.size();j++) {labe1 = new Label(0, i, (mapList.get(j).get("xmdw")!= null ?(mapList.get(j).get("xmdw").toString()): "") + "", cellFormat2);labe2 = new Label(5, i, (mapList.get(j).get("mll")!= null ?(mapList.get(j).get("mll").toString().trim()): "") + "", cellFormat2);sheet.addCell(labe1);sheet.addCell(labe2);if(mapList.get(j).get("xmsl")!=null)sheet.addCell(setNumberCell(1, i,mapList.get(j).get("xmsl").toString(), cellFormat2));if(mapList.get(j).get("cbzj")!=null)sheet.addCell(setNumberCell(2, i,mapList.get(j).get("cbzj").toString(), cellFormat2));if(mapList.get(j).get("dwzj")!=null)sheet.addCell(setNumberCell(3, i,mapList.get(j).get("dwzj").toString(), cellFormat2));if(mapList.get(j).get("ml")!=null)sheet.addCell(setNumberCell(4, i,mapList.get(j).get("ml").toString(), cellFormat2));i++;}//计算sheet.addCell(new Formula(1,i,"SUM(b3:b"+(mapList.size()+2)+")",cellFormat2));sheet.addCell(new Formula(2,i,"SUM(C3:C"+(mapList.size()+2)+")",cellFormat2));sheet.addCell(new Formula(3,i,"SUM(d3:d"+(mapList.size()+2)+")",cellFormat2));sheet.addCell(new Formula(4,i,"SUM(e3:e"+(mapList.size()+2)+")",cellFormat2));DisplayFormat displayFormat = NumberFormats.PERCENT_FLOAT;WritableCellFormat wcfF = new WritableCellFormat(displayFormat);wcfF.setAlignment(Alignment.CENTRE);wcfF.setVerticalAlignment(VerticalAlignment.CENTRE);String formula = "e"+(mapList.size()+3)+"/"+"d"+((mapList.size()+3));sheet.addCell(new Formula(5,i,formula, wcfF));//当到达数据的最后一行时,新增一行数据book.write();book.close();// path是指欲下载的文件的路径。File file = new File(path);// 取得文件名。InputStream fis = new BufferedInputStream(new FileInputStream(path));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();// 设置response的HeaderString fileName = "项目统计" + df.format(new Date());fileName = URLEncoder.encode(fileName, "utf-8");this.response.setHeader("Pragma", "public");this.response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");response.addHeader("Content-Length", "" + file.length());response.setContentType("application/octet-stream");OutputStream toClient = new BufferedOutputStream(response.getOutputStream());toClient.write(buffer);toClient.flush();toClient.close();template.delete();return null;
}

最后是接口:
public abstract List<Map<String, Object>> findMapLists(List columnNams, List paramList);
可以参照学习一下。

一个简单的jxl文件上传功能相关推荐

  1. VS2010中使用NeatUpload实现简单的文件上传功能

    neatupload 一个免费的asp.net文件上传组件 项目地址 http://neatupload.codeplex.com/ 首先,从项目网站下载最新版(目前使用的是1.3.26) 在VS20 ...

  2. ie6多文件上传_一个好的“文件上传”功能必须要注意的这些点你都知道吗?

    检查上传文件扩展名白名单,不属于白名单内,不允许上传:[前端和后端都要做好校验] 上传文件的目录必须是http请求无法直接访问到的.如果需要访问的,必须上传到其他(和web服务器不同的)域名下,并设置 ...

  3. php利用ajax文件上传,如何在PHP中利用AjaxForm实现一个文件上传功能

    如何在PHP中利用AjaxForm实现一个文件上传功能 发布时间:2020-12-18 14:52:38 来源:亿速云 阅读:94 作者:Leah 如何在PHP中利用AjaxForm实现一个文件上传功 ...

  4. 亿速云服务器 如何上传文件,使用MultipartFile怎么实现一个文件上传功能

    使用MultipartFile怎么实现一个文件上传功能 发布时间:2021-01-20 16:43:15 来源:亿速云 阅读:139 作者:Leah 使用MultipartFile怎么实现一个文件上传 ...

  5. Android 实现文件上传功能(upload)

    文 件上传在B/S应用中是一种十分常见的功能,那么在Android平台下是否可以实现像B/S那样的文件上传功能呢?答案是肯定的.下面是一个模拟网站程 序上传文件的例子.这里只写出了Android部分的 ...

  6. Spring 文件上传功能

    本篇文章,我们要来做一个Spring的文件上传功能: 1. 创建一个Maven的web工程,然后配置pom.xml文件,增加依赖: 1 2 3 4 5 <dependency>     & ...

  7. jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能

     Ajax file upload plugin是一个功能强大的文件上传jQuery插件,可自定义链接.或其它元素庖代传统的file表单上传结果,可实现Ajax动态提示文件上传 过程,同时支撑多文 ...

  8. jquery.form.js java_使用jQuery.form.js/springmvc框架实现文件上传功能

    使用的技术有jquery.form.js框架, 以及springmvc框架.主要实现异步文件上传的同时封装对象,以及一些注意事项. 功能本身是很简单的,但是涉及到一些传递参数类型的问题.例如:jque ...

  9. SpringMVC文件上传功能MultipartFile参数为空

    最近简单实现了一个springmvc文件上传功能,点了上传按钮一直报空指针,问题解决了mark一下留作以后查阅,如有搜到这篇文章的仅供参考,先上主要代码.springmvc.xml配置如下: < ...

最新文章

  1. Windows如何 cmd 查找文件路径 开机启动 CMD语音播放 CMD切换到管理员!
  2. VScode的基础设置
  3. eclipse文本框输出mysql全部数据_小巧轻便的数据库管理软件HeidiSQL
  4. 配置启动MySQL的Docker容器
  5. 文本文档TXT每行开头结尾加内容批处理代码
  6. 为什么使用linux内核,为什么Linux内核使用它所做的数据结构?
  7. 第十一届中国开源黑客松+中国程序员节重磅来袭,这里将有你不能错过的精彩。...
  8. 转两好文防丢:Debian 版本升级/降级 Linux 应用程序失去输入焦点问题的解决...
  9. java接口开发流程
  10. 计算机网络系统与分布式系统之间的区别
  11. 如何制定有效的项目章程?【含项目章程模板】
  12. matlab自带滤波器,matlab自带滤波器函数小结(图像处理)
  13. 欧洲花费210亿欧元新建大型对撞机,我国要跟进吗?
  14. 搭建MYS-SAM9X5开发环境
  15. SpringBoot18:集成SpringSecurity
  16. 2022年世界最强与最弱的护照:日本和新加坡并列第一,德国和韩国并列第二,美国和英国排名回升 | 美通社头条...
  17. 自动驾驶3-1: 自动驾驶汽车的安全保障 Safety Assurance for Self-Driving Vehicles
  18. C语言课程设计:连锁超市会员消费记录管理系统
  19. SAP 如何在选择画面中显示图片 <转载> cl_gui_docking_container
  20. Buuctf Crpyto writeup --异性相吸

热门文章

  1. 【微软 Azure 认知服务】零基础搭建微软 Azure AI 认知服务实验分享
  2. 用微信小程序加市面上的网络摄像头实现视频会议
  3. 百词斩秋招java,成都百词斩2018web前端秋招笔试题
  4. 管理与维护linux系统(任务五 六 七 八)
  5. 使用终端生成icns,icon文件
  6. Sweet 简洁是美
  7. tcsc工作原理matlab仿真,基于Matlab的TCSC建模与仿真研究.doc
  8. 设计评审CheckList
  9. TortoiseSvn文件夹及文件图标不显示
  10. MP模型、单层感知器、多层感知器的理解