把Execl表格中的数据获取出来保存到数据库中
比如我们遇到一些需要把execl表格中的数据保存到数据库中,一条一条保存效率底下而且容易出错,数据量少还好,一旦遇到数据量大的时候就会累死个人啊,下面我们就来把execl表格中数据保存到对应的数据库中
<div id="deploydiv">
<form id="ff"
action="<%=request.getContextPath()%>/theta/file/fileReadExcel"
method="post" enctype="multipart/form-data">
<table align="center">
<tr>
<td>文件:</td>
<td><input name="file" class="f1 easyui-filebox"/>
<!-- <input name="op" type="hidden" id="op"/></td> -->
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"></input></td>
</tr>
</table>
</form>
</div>
jsp页面写好之后,进入Controller具体实现类
@RequestMapping("/fileReadExcel")
@ResponseBody
public AjaxCommonResultBean getFileReadExcel(@RequestParam MultipartFile file){
AjaxCommonResultBean res = new AjaxCommonResultBean();
boolean result = filereadservice.readExcelFile(file);
if(result){
res.setSuccess(true);
res.setMessage("提交成功");
}else{
res.setSuccess(false);
res.setMessage("提交失败");
}
return res;
}
具体实现类
public boolean readExcelFile(MultipartFile file) {
boolean result =false;
List<fileReadBean> fileList = getExcelInfo(file);
if(fileList != null && !fileList.isEmpty()){
result = true;
}else{
result = false;
}
return result;
}
public List<fileReadBean> getExcelInfo(MultipartFile file) {
String fileName = file.getOriginalFilename();//获取文件名
String ext = fileName.substring(fileName.lastIndexOf("."));
try {
if (!validateExcel(fileName)) {// 验证文件名是否合格
return null;
}
List<fileReadBean> fileList = createExcel(file.getInputStream(),ext);
return fileList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private List<fileReadBean> createExcel(InputStream is,String ext) {
try{
HSSFWorkbook wb = null;
XSSFWorkbook xwb = null;
List<fileReadBean> fileList = null;
if(".xls".equals(ext)){ //HSSF方式获取文件
wb = new HSSFWorkbook(is);
fileList = readExcelValue(wb); // 读取Excel里面客户的信息
}else if(".xlsx".equals(ext)){ //XSSF方式获取文件
xwb = new XSSFWorkbook(is);
fileList = readXExcelValue(xwb);
}
return fileList;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private List<fileReadBean> readXExcelValue(XSSFWorkbook xwb) {
List<fileReadBean> fileList = new ArrayList<fileReadBean>();
for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xwb.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow == null) {
continue;
}
int num=fileReaddao.findSame(getValue(xssfRow.getCell(1)));
// 循环列Cell
// for (int cellNum = 0; cellNum <= xssfRow.getLastCellNum(); cellNum++) {
// XSSFCell xssfCell = xssfRow.getCell(cellNum);
// if (xssfCell == null) {
// continue;
// }
// System.out.print(" " + getValue(xssfCell));
// }
if(rowNum > 3 && num < 1){
fileReadBean fileread = new fileReadBean();
fileread.setId(UUID.randomUUID().toString());
fileread.setTransactionDate(getValue(xssfRow.getCell(0)));
fileread.setTransationId(getValue(xssfRow.getCell(1)));
fileread.setRemark(getValue(xssfRow.getCell(2)));
fileread.setOtherBankId(getValue(xssfRow.getCell(3)));
fileread.setOtherBankName(getValue(xssfRow.getCell(4)));
fileread.setTransfer(getValue(xssfRow.getCell(5)));
fileread.setPayment(getValue(xssfRow.getCell(6)));
fileread.setReceived(getValue(xssfRow.getCell(7)));
fileread.setBalance(getValue(xssfRow.getCell(8)));
fileReaddao.insertFileRead(fileread); //把文件中的数据插入数据库
fileList.add(fileread);
}
}
}
return fileList;
}
private List<fileReadBean> readExcelValue(HSSFWorkbook wb) {
List<fileReadBean> fileList = new ArrayList<fileReadBean>();
for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = wb.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
//查询是否有重复的交易号
int num=fileReaddao.findSame(getValue(hssfRow.getCell(1)));
// 循环列Cell
// for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) {
// HSSFCell hssfCell = hssfRow.getCell(cellNum);
// if (hssfCell == null) {
// continue;
// }
// 循环列Cell
if(rowNum > 3 && num < 1){
fileReadBean fileread = new fileReadBean();
fileread.setId(UUID.randomUUID().toString());
fileread.setTransactionDate(getValue(hssfRow.getCell(0)));
fileread.setTransationId(getValue(hssfRow.getCell(1)));
fileread.setRemark(getValue(hssfRow.getCell(2)));
fileread.setOtherBankId(getValue(hssfRow.getCell(3)));
fileread.setOtherBankName(getValue(hssfRow.getCell(4)));
fileread.setTransfer(getValue(hssfRow.getCell(5)));
fileread.setPayment(getValue(hssfRow.getCell(6)));
fileread.setReceived(getValue(hssfRow.getCell(7)));
fileread.setBalance(getValue(hssfRow.getCell(8)));
fileReaddao.insertFileRead(fileread); //把文件中的数据插入数据库
fileList.add(fileread);
}
/* for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) {
HSSFCell hssfCell = hssfRow.getCell(cellNum);
System.out.print(" " + getValue(hssfCell));
}*/
}
}
return fileList;
}
@SuppressWarnings("static-access")
private String getValue(XSSFCell xssfCell) {
if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfCell.getBooleanCellValue());
} else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(xssfCell.getNumericCellValue());
} else {
return String.valueOf(xssfCell.getStringCellValue());
}
}
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
//验证文件名是否合格
private boolean validateExcel(String fileName) {
if (fileName == null) {
String errorMsg = "文件名不是excel格式";
return false;
}
return true;
}
这样就可以把execl表格中的数据全部保存到数据库中了!如有不当之处请多多指正,一起交流,共同学习!
把Execl表格中的数据获取出来保存到数据库中的更多相关文章
- JQuery结合Ajax实现双击Table表格,使Table变成可编辑,并保存到数据库中
本文属于原创,转载请标明出处! 近期在做项目时,要实现通过双击Table表格的TR,使Table行变成可编辑,来实现修改数据并保存到数据库中的功能,无需多说,直接贴代码吧.希望能得到各位同仁指正. f ...
- 把Dev的excel表格用clientdataset保存到数据库中。
网上很多,如何把图片.word.excel等保存到数据库中.可是自己就是死活出现异常,百思不得其解.原因找到了,为什么没有去弄明白: 在sql server字段类型中,我把存储字段设成binary,结 ...
- XAF:如何让用户在运行时个性化界面并将个性化信息保存到数据库中 win/web/entityframework/xpo
本主题介绍如何启用管理模型差异(XAFML),并将设置存储在数据库中. 名词解释: 1.模型:XAF中把所有应用程序的结构都用模型来定义,比如列表,有哪些列,名称是什么,对应的字段名是什么,业务对 ...
- ASP.NET将Session保存到数据库中
因为ASP.NET中Session的存取机制与ASP相同,都是保存在进行中, 一旦进程崩溃,所有Session信息将会丢失,所以我采取了将Session信息保存到SQL Server中,尽管还有其它的 ...
- logback.xml的使用,将日志异步保存到数据库中
想要把日志异步保存到数据库中,首先需要创建一个数据库,然后创建三张固定的表: https://github.com/xiaorenwu-dashijie/logback.git <?xml ve ...
- 把MP3保存到数据库中
使用JdbcUtils得到连接con java.sql包下的Interface Blob----其实现类SerialBlob Blob是一个可以存储二进制文件的容器. BLOB常常是数据库中用来存储二 ...
- Java中将图片保存到数据库中
在实际的开发中,我们可能需要将图片.影音等文件直接保存到数据库中,然后通过编程方式将数据读出进行使用.例如将读出的图片数据显示出来,将读出的电影文件播放出来. 二进制数据直接保存到文件和从文件中读出非 ...
- 利用POI抽取word中的图片并保存在文件中
利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...
- 转: SQL中的where条件,在数据库中提取与应用浅析
SQL中的where条件,在数据库中提取与应用浅析 http://hedengcheng.com/?p=577 1问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当 ...
随机推荐
- python3与python2的编码问题
在讲这个问题之前,我们先说说unicode的工作原理.unicode包含了跟全球所有国家编码的映射关系,就是不管你用哪个国家的编码,unicode都能找到它在unicode中的编码.那么无论你用什么编 ...
- JAVA基础篇—异常
五种常见异常 1.NullPointerException 空指针 2.ClassNotFoundException 指定类不存在 3.ArithmeticException运算异常 4.ArrayI ...
- MySQL迁移至MariaDB
为什么要用MariaDB来代替MySQL MariaDB是MySQL社区开发的分支,也是一个增强型的替代品.它由MySQL前开发者们带头组织的基金会开发,使用起来和MySQL完全一样.自从Oracle ...
- KVO And KVC
http://www.cocoachina.com/industry/20140224/7866.html
- 大数据学习——spark学习
计算圆周率 [root@mini1 bin]# ./run-example SparkPi [root@mini1 bin]# ./run-example SparkPi [root@mini1 bi ...
- Leetcode 437.路径总和III
路径总和III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). ...
- C语言总结(3)
1.字符输入函数getchar 输入一个字符 char ch; ch=getchai(); 字符输出函数putchar 输出一个字符 putchar(输出参数): 2.调用scanf和printf输入 ...
- sqlserver数据库的权限设置
1.先用Windows账户登陆,然后在安全性中添加用户--SQL server 身份验证,用户名,密码2.用户映射--勾选对应的数据库--数据库角色成员身份--db_owner public
- 快速samba配置
apt-get install samba smbpasswd -a user 如果需要写权限 [homes] read only = no
- ACM程序设计选修课——1058: Lucky Sequence(思考)
1058: Lucky Sequence Time Limit: 10 Sec Memory Limit: 64 MB Submit: 52 Solved: 6 [Submit][Status][ ...