@RequestMapping(params = "method=import", method = RequestMethod.POST)
@ResponseBody
public String importConfig(HttpServletRequest request, HttpServletResponse response) throws Exception{
response.setCharacterEncoding("GBK");
String result = new String();;
try{
//获取请求中的文件
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
MultipartFile contentFile = multipartHttpServletRequest.getFile(IMPORT_FILE_NAME); //IMPORT_FILE_NAME为前端中input的name
//读取请求中的excel
Workbook workbook = Workbook.getWorkbook(contentFile.getInputStream());
Sheet sheet = workbook.getSheet(0);
int rows=sheet.getRows();
int columns=sheet.getColumns();
//将excel中的内容读为ConfigInfo对象
List<ConfigInfo> listConfigInfo = new ArrayList<ConfigInfo>();
for(int i=1; i<rows; i++){
ConfigInfo configInfo = new ConfigInfo();
for(int j=0; j<columns; j++){
Cell cell = sheet.getCell(j, i);
String cellContent = cell.getContents();
switch (j){
case 0 : configInfo.setDataId(cellContent);
case 1 : configInfo.setGroup(cellContent);
case 2 : configInfo.setContent(cellContent);
}
}
listConfigInfo.add(configInfo);
}
//写入数据库
int importFailedNum = 0;
for(ConfigInfo configInfo:listConfigInfo){
ConfigInfo configInfoExist = configService.findConfigInfo(configInfo.getDataId(), configInfo.getGroup());
//写入数据库之前判断是否存在该条配置,并记录
if (null != configInfoExist){
importFailedNum++;
         }else{
try{
configService.addConfigInfo(configInfo.getDataId(), configInfo.getGroup(),configInfo.getContent());
}catch(Exception operationDatabaseException){
log.error("导入配置出错"+operationDatabaseException);
return result;
}
}
}
result = String.valueOf(importFailedNum);
return result;
}catch(Exception exception){
log.error("导入配置出错"+exception);
return result;
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>导入配置信息</title>
<script src="../../../js/jquery.js" type="text/javascript"></script>
<script src="../../../js/ajaxfileupload.js" type="text/javascript"></script>
<style type="text/css">
body td {
color: #333;
font-family: Arial, Helvetica, sans-serif;
font-size: 10pt;
}
</style>
<script type="text/javascript">
$(function () {
$(":button").click(function () {
if ($("#contentFile").val().length > 0) {
ajaxFileUpload();
}
else {
alert("请选择文件");
}
})
})
function ajaxFileUpload() {
$.ajaxFileUpload
(
{
url: '/diamond-server/admin.do?method=import', //用于文件上传的服务器端请求地址
secureuri: false, //一般设置为false
fileElementId: 'contentFile', //文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: 'JSON', //返回值类型 一般设置为json
type:'post',
success: function (data, status) //服务器成功响应处理函数
{
//判断返回的值
if (data == 0)
alert("导入配置成功");
else
alert("错误!第"+data+"条配置导入失败,请仔细对比要导入的配置,不能与已存在的配置重复");
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert(data.msg);
}
}
window.location.href = "list.jsp";
},
error: function (data, status, e)//服务器响应失败处理函数
{
alert(e);
window.location.href = "list.jsp";
}
}
)
return false;
}
</script>
</head>
<body>
<center><h1><strong>导入配置信息</strong></h1></center>
<p align='center'>
<input type="file" id="contentFile" name="contentFile" />
<input type="button" value="上传" />
</p>
</body>
</html>

在MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;这句代码执行强转出错。发现我的配置文件里少了对multipart解析器的配置:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize" value="104857600"/> <!-- 文件最大的大小 -->
  <property name="maxInMemorySize" value="4096"/>
</bean>

同时需要依赖commons-fileupload-1.2.jar和commons-io-1.3.1。

jxl读取excel实现导入excel写入数据库的更多相关文章

  1. 十万级百万级数据量的Excel文件导入并写入数据库

    一.需求分析 最近接到一个需求,导入十万级,甚至可能百万数据量的记录了车辆黑名单的Excel文件,借此机会分析下编码过程; 首先将这个需求拆解,发现有三个比较复杂的问题: 问题一:Excel文件导入后 ...

  2. NodeJs之EXCEL文件导入导出MongoDB数据库数据

    NodeJs之EXCEL文件导入导出MongoDB数据库数据 一,介绍与需求 1.1,介绍 (1),node-xlsx : 基于Node.js解析excel文件数据及生成excel文件. (2),ex ...

  3. 如何使用NPOI 导出到excel和导入excel到数据库

    近期一直在做如何将数据库的数据导出到excel和导入excel到数据库. 首先进入官网进行下载NPOI插件(http://npoi.codeplex.com/). 我用的NPOI1.2.5稳定版. 使 ...

  4. JAVA处理Excel表格数据并写入数据库

    package com.hncj.test; import java.io.FileInputStream; import java.sql.Connection; import java.sql.D ...

  5. 读取FTP上的excel文件,并写入数据库

    今天遇到一些问题,需要从ftp上读取一些excel文件,并需要将excel中的数据写入到数据库,这样就可以通过管理页面查看这些数据. 我将相关工作分为三步,1.从ftp上读取相关文件,并将excel文 ...

  6. [转] 从数据库中读取图片并导入Excel文件,C#方式

    原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...

  7. c#WebApi使用form表单提交excel,实现批量写入数据库

    思路:用户点击下载模板按钮,获取到excel模板,然后向里面填写数据保存.from表单提交的时候选择保存好的excel,实现数据的批量导入过程 先把模板放在服务器的项目目录下面:如 模板我一般放在:F ...

  8. PHP Excel文件导入数据到数据库

    1.php部分(本例thinkphp5.1): 下载PHPExcel了扩展http://phpexcel.codeplex.com/ <?phpnamespace app\admin\contr ...

  9. 批量将制定文件夹下的全部Excel文件导入微软SQL数据库

    以下代码将c:\cs\文件夹下的全部Excle中数据导入到SQL数据库 declare @query vARCHAR(1000) declare @max1 int declare @count1 i ...

随机推荐

  1. Freemarker 内置函数 数字、字符串、日期格式化用法介绍

    在用FreeMarker过程中,感觉FreeMarker的字符串,日期,集合等处理能力还是很强大的,上网搜了一些资料,整理如下,以便能帮助大家更熟练的应用Freemarker完成项目开发. 一.Seq ...

  2. 各廠商ERP系統架構圖連結 (ERP流程圖)(轉)

    各廠商ERP系統架構圖連結 (ERP流程圖)   資料來源 Google圖片搜尋ERP整理而來 資通電腦 ArgoERP 資通電腦 Oracle ERP 鼎新電腦 Workflow ERP鼎新電腦 S ...

  3. (转)Linux服务器调优

    Linux内核参数 http://space.itpub.net/17283404/viewspace-694350 net.ipv4.tcp_syncookies = 1 表示开启SYN Cooki ...

  4. Spring对Quartz的封装实现简单需注意事项

    前段时间在项目中一直使用正常的Quartz突然出现了任务漏跑的情况,由于我以前看过Quartz的内部实现,凭借记忆我觉得是由于Quartz的线程池的使用出现问题导致了故障的发生.为了搞清问题的真相,我 ...

  5. Hibernate自动创建表

    只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 <property name="hibernate.hbm2ddl.auto">update& ...

  6. Lock-Free 编程

    文章索引 Lock-Free 编程是什么? Lock-Free 编程技术 读改写原子操作(Atomic Read-Modify-Write Operations) Compare-And-Swap 循 ...

  7. Ubuntu环境搭建系列—WPS/LAMP/Python篇

    由于篇幅不宜太长,所以就将此文分成了两部分,在第一部分中我们讲解了google-chrome(谷歌浏览器)的安装,jdk的环境配置,android开发的环境搭建.那么在第二部分中,位们将讲解的是金山W ...

  8. querySelector和querySelectorAll

    jQuery被开发者如此的青睐和它强大的选择器有很大关系,比起笨重的document.getElementById.document.getElementByName… ,查找元素很方便,其实W3C中 ...

  9. zk系列-zookeeper的使用

    zk支持java/c访问,java常用的有apache-zkclient.社区版的i0tec-zkclient.github.adyliu,apache-zkclient是zk自身提供的接口,i0te ...

  10. IOS Animation-CABasicAnimation、CAKeyframeAnimation详解&区别&联系

    1.先看看网上流传的他们的继承图: 从上面可以看出CABasicAnimation与CAKeyframeAnimation都继承于CAPropertyAnimation.而CAPropertyAnim ...