@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. iOS开发零基础--Swift篇 循环

    循环的介绍 在开发中经常会需要循环 常见的循环有:for/while/do while. 这里我们只介绍for/while,因为for/while最常见 for循环的写法 最常规写法 // 传统写法 ...

  2. Mac OS X 安装Win7双系统

    Mac10安装双系统 为了有一个纯净的开发环境,就在mac电脑中安装windows虚拟机.刚开始使用还很顺利,两个系统的交互很方便,mac用来下载.搜索和写笔记:windows纯开发.时间长了以后关机 ...

  3. nginx.conf各参数的意义

    搬运+翻译至 http://qiita.com/syou007/items/3e2d410bbe65a364b603 /etc/nginx/nginx.conf 记录各个参数的意义 user user ...

  4. .Net Core下如何管理配置文件

    一.前言 根据该issues来看,System.Configuration在.net core中已经不存在了,那么取而代之的是由Microsoft.Extensions.Cnfiguration.XX ...

  5. 关于使用ABP框架搭建的项目升级时需要注意的问题汇总

    ABP理论学习总目录 一步一步使用ABP框架搭建正式项目系列教程 ABP之Module-Zero学习目录 本篇目录 说明 升级方法 问题_01:Log4Net导致编译不成功 2015/12/18更新 ...

  6. jQuery自动加载更多程序

    1.1.1 摘要 现在,我们经常使用的微博.微信或其他应用都有异步加载功能,简而言之,就是我们在刷微博或微信时,移动到界面的顶端或低端后程序通过异步的方式进行加载数据,这种方式加快了数据的加载速度,由 ...

  7. Java枚举类型getClass和getDeclaringClass区别(未完待续)

    Java中的枚举类型有getClass()和getDeclaringClass()两个方法,在通常情况下这两个方法返回的类型一样,在某些场景下会有不同的表现 参照 http://stackoverfl ...

  8. Visual Studio 2015速递(1)——C#6.0新特性怎么用

    系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...

  9. git rm–r folder fatal:pathspec "" did not match any files

    问题描述: 某年某月某日,在查看git库的时候,发现文件的分布和文件夹的名字是极其不合理的,所以移动和重命名了某些文件. 在删除(git rm –r folder)一个空文件夹的时候,出现错误:fat ...

  10. Git Day02,工作区,暂存区,回退,删除文件

    1st,工作区回退:2st,暂存区回退:3rd,删除文件: