jxl读取excel实现导入excel写入数据库
@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写入数据库的更多相关文章
- 十万级百万级数据量的Excel文件导入并写入数据库
一.需求分析 最近接到一个需求,导入十万级,甚至可能百万数据量的记录了车辆黑名单的Excel文件,借此机会分析下编码过程; 首先将这个需求拆解,发现有三个比较复杂的问题: 问题一:Excel文件导入后 ...
- NodeJs之EXCEL文件导入导出MongoDB数据库数据
NodeJs之EXCEL文件导入导出MongoDB数据库数据 一,介绍与需求 1.1,介绍 (1),node-xlsx : 基于Node.js解析excel文件数据及生成excel文件. (2),ex ...
- 如何使用NPOI 导出到excel和导入excel到数据库
近期一直在做如何将数据库的数据导出到excel和导入excel到数据库. 首先进入官网进行下载NPOI插件(http://npoi.codeplex.com/). 我用的NPOI1.2.5稳定版. 使 ...
- JAVA处理Excel表格数据并写入数据库
package com.hncj.test; import java.io.FileInputStream; import java.sql.Connection; import java.sql.D ...
- 读取FTP上的excel文件,并写入数据库
今天遇到一些问题,需要从ftp上读取一些excel文件,并需要将excel中的数据写入到数据库,这样就可以通过管理页面查看这些数据. 我将相关工作分为三步,1.从ftp上读取相关文件,并将excel文 ...
- [转] 从数据库中读取图片并导入Excel文件,C#方式
原文地址, 作者 Lvyou1980 直接源码吧. using System; using System.IO; using System.Data; using System.Drawing; us ...
- c#WebApi使用form表单提交excel,实现批量写入数据库
思路:用户点击下载模板按钮,获取到excel模板,然后向里面填写数据保存.from表单提交的时候选择保存好的excel,实现数据的批量导入过程 先把模板放在服务器的项目目录下面:如 模板我一般放在:F ...
- PHP Excel文件导入数据到数据库
1.php部分(本例thinkphp5.1): 下载PHPExcel了扩展http://phpexcel.codeplex.com/ <?phpnamespace app\admin\contr ...
- 批量将制定文件夹下的全部Excel文件导入微软SQL数据库
以下代码将c:\cs\文件夹下的全部Excle中数据导入到SQL数据库 declare @query vARCHAR(1000) declare @max1 int declare @count1 i ...
随机推荐
- [Leetcode][JAVA] Path Sum I && II
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- 【转】COM技术内幕(笔记)
COM技术内幕(笔记) COM--到底是什么?--COM标准的要点介绍,它被设计用来解决什么问题?基本元素的定义--COM术语以及这些术语的含义.使用和处理COM对象--如何创建.使用和销毁COM对象 ...
- 8.4.3 Glide
1). 导入库 dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support: ...
- 如何去掉底部的织梦版权信息powered by dedecms
由于织梦DEDECMS程序6月份的漏洞,很多织梦网站都被黑了,所以大家都在抓紧时间更新系统补丁.但是这次的DEDECMS V5.7版本更新后,在前台网页底部会出现织梦版权信息 “powered by ...
- blog搬迁
因为一些个人原因,2年后继续写blog,但是blog搬到github上!具体的地址为: http://www.94geek.com 内容以linux的c开发,分布式存储和分布式计算,还有架构为主.
- 解决ie8(及其以下)不支持getElementsByClassName的问题
这篇技术笔记与大家分享的是:解决IE8(及以下)不支持getElementsByClassName的方法.如果有说错的地方,麻烦留言告诉我,我及时更正,一来是更新一下我的认知,二来也是不要误导的他人, ...
- Perl的多进程框架(watcher-worker)
关于perl的多进程,大家可能马上会想到Parallel::ForkManager这个模块.但是今天我们试着自己动手写一个类似的框架:) 该多进程开发模型从开源服务器框架Lighttpd发展而来,核心 ...
- Silverlight5中横向显示ListBox
备忘 <ListBox x:Name="Cpbrow" HorizontalAlignment="Left" Height="153" ...
- CSS 布局入门
概述 Web 兴起之后,关于CSS的介绍和学习资料已经铺天盖地. 本文不涉及具体的CSS语法之类的,而是希望从初学者的角度,让没有接触或很少接触CSS的人能快速的了解 CSS 到底是什么以及如何使用. ...
- HTML5学习生涯1--touchmove中遇到的问题
在使用html5做在手机上显示轮播图片的效果时突然遇到touchmove事件在touchstart事件之后只触发了一次touchmove之后和touchend一起触发了一次,咦,这是怎么回事?怎么不和 ...