把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,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当 ...
随机推荐
- 在Unix系统上,从源文件、目标文件、可执行文件的编译过程
是由“编译器驱动”(compiler driver)完成的: unix> gcc -o hello hello.c 在这里,gcc的编译器驱动程序读取源文件hello.c, #include & ...
- 2018 Multi-University Training Contest 1 Balanced Sequence(贪心)
题意: t组测试数据,每组数据有 n 个只由 '(' 和 ')' 构成的括号串. 要求把这 n 个串排序然后组成一个大的括号串,使得能够匹配的括号数最多. 如()()答案能够匹配的括号数是 4,(() ...
- CodeForce:16C-Monitor
传送门:http://codeforces.com/problemset/problem/16/C Monitor time limit per test0.5 second memory limit ...
- poj 2531 分权问题 dfs算法
题意:一个集合(矩阵) m[i][j]=m[j][i]权值,分成两个集合,使其权值最大.注:在同一个集合中权值只能算一个. 思路:dfs 假设都在集合0 遍历 id 的时候拿到集合1 如果与 id 相 ...
- Flask-用户角色及权限
app/models.py class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=T ...
- webdriver高级应用- 使用Chrome浏览器自动将文件下载到指定路径
#encoding=utf-8 from selenium import webdriver import unittest, time class TestDemo(unittest.TestCas ...
- 大数据学习——akka学习
架构图 重要类介绍 ActorSystem 在Akka中,ActorSystem是一个重量级的结构,他需要分配多个线程,所以在实际应用中,ActorSystem通常是一个单例对象,我们可以使用这个Ac ...
- linux 基础 软件的安装 *****
一软件的安装 原代码与tarball 源代码---->编译------>可执行文件 查看文件类型 file命令 是否是二进制文件 注意如果文件的可执行权限 .c结尾的源文件- ...
- # Linux 命令学习记录
Linux 命令学习记录 取指定文件夹下的任意一个文件,并用vim打开 vi $(ls -l|grep "^-"|head -n 1|awk '{print $9}') 统计给定文 ...
- 【bzoj1690】[Usaco2007 Dec]奶牛的旅行 分数规划+Spfa
题目描述 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城市地图,上面标 ...