在pom.xml中引入:

        <!--处理2003 excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!--处理2007 excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>

applicationContext.xml:

    <!--上传组件-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!--1024*1024*5即5M-->
<property name="maxUploadSize" value="5242880"></property>
</bean>

页面:

<div>
<form method="post" action="/Cyberspace/main/informationBatchAdd.do" enctype="multipart/form-data">
<input type="file" id="excelFile" name="file"/>
<button type="submit">批量上传</button>
</form>
</div>

Java后台代码:

@RequestMapping(value = "informationBatchAdd", method = RequestMethod.POST)
@ResponseBody
public void informationBatchAdd(MultipartHttpServletRequest request) { try {
//得到上传的文件
MultipartFile fileFile = request.getFile("file");
//转换成输入流
InputStream in = fileFile.getInputStream();
XSSFWorkbook readWb = new XSSFWorkbook(in);
/*HSSFWorkbook readWb = new HSSFWorkbook(in);*/
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for (int i = 0; i < readWb.getNumberOfSheets(); i++) {
XSSFSheet sheet = readWb.getSheetAt(i);
// 循环行Row
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
XSSFRow hssfRow = sheet.getRow(rowNum);
if (hssfRow != null) {
for (int colNum = 0; colNum < hssfRow.getPhysicalNumberOfCells(); colNum++) {
System.out.println(hssfRow.getCell(colNum));
}
//赋值实例对象 做插入数据库操作
              ...
}
}
} } catch (Exception e) { e.printStackTrace();
} }

基于Spring MVC实现基于form表单上传Excel文件,批量导入数据的更多相关文章

  1. C# FormData 文件太大报错404 Form表单上传大文件,无法进入后台Action,页面提示404.

    web.config中添加如下节点 <system.webServer> <security>      <requestFiltering >        &l ...

  2. django 基于form表单上传文件和基于ajax上传文件

    一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...

  3. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...

  4. vue form表单上传文件

    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js">< ...

  5. 使用form表单上传文件

    在使用form表单上传文件时候,input[type='file']是必然会用的,其中有一些小坑需要避免. 1.form的 enctype="multipart/form-data" ...

  6. JsonResponse类的使用、form表单上传文件补充、CBV和FBV、HTML的模板语法之传值与过滤器

    昨日内容回顾 Django请求生命周期 # 1.浏览器发起请求 到达Django的socket服务端(web服务网关接口) 01 wsgiref 02 uwsgi + nginx 03 WSGI协议 ...

  7. 理解流方式上传和form表单上传

    流方式上传: $post_input = 'php://input'; $save_path = dirname( __FILE__ ); $postdata = file_get_contents( ...

  8. PHP CURL 模拟form表单上传遇到的小坑

    1:引用的时候 $parans ['img']=new \CURLFile($param); 传入的文件 在PHP版本5.5以上记得new CURLFile 不然会上传不成功 /** * http p ...

  9. antd实战:表单上传,文件列表的过滤与限制。

    用表单上传组件最痛苦的地方是: 他的诸多行为与纯上传组件不一样,而表单的文档关于这一块基本上没有提,只能自己试. 比如我想做一个上传前的拦截. beforeUpload: (file, fileLis ...

随机推荐

  1. linux7开机自启动东方通tongweb

    自启动服务: 可以通过把TongWeb设置为系统服务来实现. 具体实现: 以root用户进行操作,在/etc/init.d目录下编写TongWeb的服务脚本tongweb,用来控制TongWeb的启动 ...

  2. 协程+IO切换实现并发

    from gevent import monkey # 以后代码中遇到IO都会自动执行greenlet的switch进行切换 monkey.patch_all() import requests im ...

  3. 我的Android进阶之旅------>RxJava学习资料汇总

    在响应式编程中,应该牢记以下两点: everything is a stream(一切皆流) don't break the chain(不要打断链式结构) 记住,可观测序列就像一条河,它们是流动的. ...

  4. What are the top 10 things that we should be informed about in life

    1.Realize that nobody cares, and if they do, you shouldn't care that they care. Got a new car? Nobod ...

  5. 使用Berkeley Parser进行句法分析

    一.句法分析 1.定义 句法分析判断输入的单词序列(一般为句子)的构成是否合乎给定的语法,并通过构造句法树来确定句子的结构以及各层次句法成分之间的关系,即确定一个句子中的哪些词构成一个短语,哪些词是动 ...

  6. day9 文件的读取

    文件操作 一.打开文件 f = open('歌词.txt','w',encoding='utf-8') # f:文件操作符 文件句柄 文件操作对象 open打开文件是依赖了操作系统提供的途径 操作系统 ...

  7. Team Foundation 中的错误和事件消息

    Visual Studio Team System Team Foundation 中的错误和事件消息 Team Foundation 通过显示错误消息和事件消息来通知您操作成功以及操作失败.一部分错 ...

  8. ruby underscore

    “examScore".underscore : exam_score "ExamScore".underscore: exam_score

  9. sql中1=1和1=0的用处

    where 1=1 where 1=1有什么用?在SQL语言中,写这么一句话就跟没写一样. select * from table1 where 1=1与select * from table1完全没 ...

  10. 20145109 《Java程序设计》第五周学习总结

    20145109 <Java程序设计>第五周学习总结 教材学习内容总结 Chapter 8 Exception Handling try, catch All Exceptions are ...