前端框架:Bootstrap,后端框架:SpringMVC、Spring

  • 下载、上传的页面如下:
  • 模版内容如下:
  • 下载、上传的前端代码如下:
<div class="modal" id="batchUpdateDigestModal" role="dialog" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button><h4 class="modal-title">文件批量补充摘要信息</h4></div><div class="modal-body text-center"><form action="##" class="form-horizontal form-row-sepe" id="batchUpdateDigestForm"><div class="form-group" style="margin-left: 10px;"><a href="javascript:exportTemplateData();" id="exportTemplate" class="btn btn-sm green"><i class="fa"></i>模板下载 </a> <span>(请下载模版,按照模版格式上传)</span></div><hr><div class="form-group margin-top-10"><div class="col-md-6"><input type="file" name="multipartFile" id="file_id" style="margin-left: 120px;"></div><div class="col-md-2"><button type="button" onclick="batchUpdateDigest();" class="btn btn-sm blue"style="height: 30px;">批量补充摘要</button></div></div></form></div><div class="modal-footer"><button type="button" class="btn default" data-dismiss="modal">关闭</button></div></div></div>
</div>

js代码:

//下载模版
function exportTemplateData() {var form = document.getElementById("batchUpdateDigestForm");form.action = "${basePath}/bankFlowJournal/downloadTemplate.do";form.submit();form.action = "##";}//上传摘要文件,批量补充摘要
function batchUpdateDigest() {var file_id = $("#file_id").val();if (file_id == null || file_id == "") {bootbox.alert("上传文件不能为空!");return;}$.startMaskLayer(); //缓冲图层打开$("#batchUpdateDigestForm").ajaxSubmit({type: "post",dataType: "json",clearForm: true,resetForm: true,url: "${basePath}/bankFlowJournal/batchUpdateDigest.do",success: function (data) {$.endMaskLayer();//缓冲图层关闭$("#batchUpdateDigestModal").modal("hide");bootbox.alert(data.resultMsg); //显示返回信息$("#go_search").click(); //刷新页面}});}
  • 下载文件的JAVA代码:
@RequestMapping("/downloadTemplate")public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) {try {String fileName = "批量补充摘要模板.xlsx";OutputStream out = response.getOutputStream();// 取得输出流response.reset();// 清空输出流//组装模版数据List<List<String>> data = new ArrayList<List<String>>();List<String> list = new ArrayList<>();list.add("xxxxxxxxxxxx");list.add("2018-01-01或2018/1/1");list.add("101001|xxx");data.add(list);//生成ExcelString headers[] = {"流水号", "交易日期", "摘要"};ExportExcel<Object> eeu = new ExportExcel<Object>();HSSFWorkbook workbook = new HSSFWorkbook();eeu.exportExcel(workbook, 0, "摘要模板", headers, data, out);//输出到responseresponse.setContentType("application/vnd.ms-excel;charset=UTF-8");response.setHeader("Content-Disposition","attachment;filename="+ new String((fileName).getBytes(), "iso-8859-1"));OutputStream output  = response.getOutputStream();workbook.write(output);output.flush();output.close();} catch (Exception e) {logger.error("【下载模板出错", e);throw new SettleException(RetCode.FAILURE, "下载模板出错");}}
  • 上传文件的JAVA代码:
@RequestMapping("/batchUpdateDigest")@ResponseBodypublic Object batchUpdateDigest(HttpServletRequest request, MultipartFile multipartFile) {User user = (User) request.getSession().getAttribute(Cons.S_USER);logger.info("【账户流水日记账-批量更新摘要信息】操作人={}", user.getLoginName());SettleResponse response = new SettleResponse(RetCode.SUCCESS);try {String filename = multipartFile.getOriginalFilename();logger.info("begin to parse file...");if (null == filename || "".equals(filename)) {throw new SettleException(RetCode.FAILURE, "上传文件不能为空!");}if (!filename.toUpperCase().endsWith(".XLS")) {throw new SettleException(RetCode.FAILURE, "不支持此文件类型,请重新上传!");}Workbook wb = Workbook.getWorkbook(multipartFile.getInputStream());Sheet sheet = wb.getSheet(0);    //获得第一个表单int lastRow = sheet.getRows();BankDigestUpdateReq digestUpdateReq;DateCell dCell=null;for (int i = 1; i < lastRow; i++) {digestUpdateReq = new BankDigestUpdateReq();Cell[] cells = sheet.getRow(i);//防止有编辑过的空行被扫描if(null == cells || cells.length < 1 || CellType.EMPTY.equals(cells[0].getType())){continue;}//非空检验if (null == cells[0]) {throw new SettleException(RetCode.FAILURE, "流水号不能为空,请检查文件!");}if (null == cells[1] || StringUtils.isBlank(cells[1].getContents())) {throw new SettleException(RetCode.FAILURE, "交易日期不能为空,请检查文件!");}if (null == cells[2]) {throw new SettleException(RetCode.FAILURE, "摘要不能为空,请检查文件!");}//如果是文本格式的日期,即:yyyy-MM-ddif(CellType.LABEL.equals(cells[1].getType())){digestUpdateReq.setTradeDate(DateUtil.SDF1().parse(cells[1].getContents()));}//如果是日期格式的日期,即:yyyy/MM/ddelse if(CellType.DATE.equals(cells[1].getType())){dCell = (DateCell) cells[1];digestUpdateReq.setTradeDate(dCell.getDate());}digestUpdateReq.setTradeNo(cells[0].getContents());digestUpdateReq.setDigest(cells[2].getContents());digestUpdateReq.setOperator(user.getLoginName());BaseRes res = journalUpdateFacade.updateDigestInfo(digestUpdateReq);if (!RetCode.SUCCESS.getCode().equals(res.getRetCode())) {throw new SettleException(RetCode.FAILURE, res.getRetInfo());}}} catch (SettleException se) {logger.info("解析摘要补充文件业务异常,异常原因:{}", se.getMessage());response.setResult(RetCode.FAILURE, se.getMessage());} catch (Exception ex) {logger.error("读取excel出错", ex);response.setResult(RetCode.FAILURE, "读取excel出错!");}return response;}
  • 涉及到的pom配置:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.5.9.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.1.7.RELEASE</version>
</dependency>
<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version>
</dependency>
<dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version>
</dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.0.1</version>
</dependency>
<dependency><groupId>net.sourceforge.jexcelapi</groupId><artifactId>jxl</artifactId><version>2.6.12</version>
</dependency>

SpringMVC下载上传Excel文件相关推荐

  1. Jquery+SpringMVC实现上传Excel文件,并批量导入

    1.前端代码 function alertUploadFile(){ //创建表单var formData = new FormData();var file = $("#upload&qu ...

  2. Django框架(上传Excel文件并读取)

    博主今天整理下Django框架中上传Excel文件并读取 博主是要在管理平台中新增用例的维护功能,想着通过上传Excel文件来展示用例,下面是项目的路径图: 首先先建数据库模型 model.py 可以 ...

  3. php上传查询excel到mysql_PHP上传Excel文件导入数据到MySQL数据库示例

    PHP上传Excel文件导入数据到MySQL数据库示例2020-06-20 00:34:11 最近在做Excel文件导入数据到数据库.网站如果想支持批量插入数据,可以制作一个上传Excel文件,导入里 ...

  4. php 上传excel到mysql_PHP上传Excel文件导入数据到MySQL数据库示例

    最近在做Excel文件导入数据到数据库.网站如果想支持批量插入数据,可以制作一个上传Excel文件,导入里面的数据内容到MySQL数据库的小程序. 要用到的工具: ThinkPHP:轻量级国产PHP开 ...

  5. Web项目,网页上传excel文件并解析实战示例

    最近写了一个基于poi解析excel文件的工具类,所以想在web项目中测试一下,就做了这个简单的项目.本项目主要使用了 SpringMVC+RESTful+Maven的风格.适合有一定基础的人员. 源 ...

  6. java下载上传远程文件

    利用的是SMB协议从远程服务器下载上传文件 可以在本地做一个共享文件夹放点东西来测试下 这是用到的jar包[jcifs-1.2.3.jar],不能设置0积分有点可惜.(https://download ...

  7. 服务器上传excel文件并读取数据,asp.net上传Excel文件并读取数据的实现方法

    前言 本文主要给大家介绍了关于asp.net上传Excel文件并读取数据的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 实现如下: 前台代码:使用服务端控件实现上传 服务端 ...

  8. vue之 上传 excel文件

    目录 vue之 上传 excel文件 父组件 UploadExcel.vue 效果 vue之 上传 excel文件 父组件 <upload-excel-component :on-success ...

  9. 帆软报表决策系统 上传excel文件

    这个属于二次开发的,比如我要在决策系统中开发一个功能,上传excel文件或者其他文件, 首先前端使用fineUI: BI.File1 = BI.inherit(BI.File,{_defaultCon ...

最新文章

  1. 高糊视频秒变4K!Facebook发布低分辨率视频实时渲染算法,网友:是好东西,但是玩不起...
  2. Exception in thread main java.io.IOException: (null) entry in command string: null chmod 0700 E:\t
  3. 如何将一个数据库中的一个表复制到另一个数据库中去
  4. Windows平台下 找回已丢失的MySql root 用户密码
  5. Mule,目前综合状态最良好的开源ESB方案引文
  6. Vue 中使用watch监听$route 无效问题
  7. c++将小写转换为大写函数_必须掌握的基础函数组合应用技巧,提高效率,准时下班...
  8. 【XAMPP启动mysql报错】Port 3306 in use by ““C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqld“……
  9. RIP简易配置第二篇
  10. 理解JESD204B链路参数 Understanding JESD204B Link Parameters
  11. OPenGL 学习笔记之 Assimp 知识
  12. python学习态度_python基础一 day29 学习方法(课前谈心)
  13. Web前端-HTML
  14. hapi_带有节点和Hapi后端的Angular文件上传
  15. 中文数字与阿拉伯数字转换(Python)
  16. 过程与结果——独立思考——认真详谈
  17. 离焦、球差、像散等成像结果
  18. 队列的应用--火车车厢重排列
  19. 洛谷3628 APIO2010特别行动队(斜率优化)
  20. mysql中删除数据库语句

热门文章

  1. 让 Lua 访问数据库
  2. 点云IO篇之las文件读写
  3. 盘点机需要连接网络吗
  4. 冰封王座人工只能_《冰封王座》强大人工智能AI补丁 AMAI2.20
  5. 超强干货!7个腾讯最常用的用户研究方法
  6. Visual Studio 2013运行C/C++程序
  7. Excel表格批量将文本转换为超链接 批量文本转链接 一键转URL
  8. AWS IAM certification
  9. 利用地址转经纬度获取坐标
  10. css3 太极动画,纯css实现太极阴阳鱼动画