比如我们遇到一些需要把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表格中的数据获取出来保存到数据库中的更多相关文章

  1. JQuery结合Ajax实现双击Table表格,使Table变成可编辑,并保存到数据库中

    本文属于原创,转载请标明出处! 近期在做项目时,要实现通过双击Table表格的TR,使Table行变成可编辑,来实现修改数据并保存到数据库中的功能,无需多说,直接贴代码吧.希望能得到各位同仁指正. f ...

  2. 把Dev的excel表格用clientdataset保存到数据库中。

    网上很多,如何把图片.word.excel等保存到数据库中.可是自己就是死活出现异常,百思不得其解.原因找到了,为什么没有去弄明白: 在sql server字段类型中,我把存储字段设成binary,结 ...

  3. XAF:如何让用户在运行时个性化界面并将个性化信息保存到数据库中 win/web/entityframework/xpo

    本主题介绍如何启用管理模型差异(XAFML),并将设置存储在数据库中.   名词解释: 1.模型:XAF中把所有应用程序的结构都用模型来定义,比如列表,有哪些列,名称是什么,对应的字段名是什么,业务对 ...

  4. ASP.NET将Session保存到数据库中

    因为ASP.NET中Session的存取机制与ASP相同,都是保存在进行中, 一旦进程崩溃,所有Session信息将会丢失,所以我采取了将Session信息保存到SQL Server中,尽管还有其它的 ...

  5. logback.xml的使用,将日志异步保存到数据库中

    想要把日志异步保存到数据库中,首先需要创建一个数据库,然后创建三张固定的表: https://github.com/xiaorenwu-dashijie/logback.git <?xml ve ...

  6. 把MP3保存到数据库中

    使用JdbcUtils得到连接con java.sql包下的Interface Blob----其实现类SerialBlob Blob是一个可以存储二进制文件的容器. BLOB常常是数据库中用来存储二 ...

  7. Java中将图片保存到数据库中

    在实际的开发中,我们可能需要将图片.影音等文件直接保存到数据库中,然后通过编程方式将数据读出进行使用.例如将读出的图片数据显示出来,将读出的电影文件播放出来. 二进制数据直接保存到文件和从文件中读出非 ...

  8. 利用POI抽取word中的图片并保存在文件中

    利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...

  9. 转: SQL中的where条件,在数据库中提取与应用浅析

    SQL中的where条件,在数据库中提取与应用浅析 http://hedengcheng.com/?p=577 1问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当 ...

随机推荐

  1. 二叉排序树:HDU3791-二叉搜索树(用指针建立二叉排序树)

    二叉搜索树 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descr ...

  2. Educational Codeforces Round 2 Edge coloring of bipartite graph

    题意: 输入一个二分图,用最少的颜色数给它的每条边染色,使得同一个顶点连的边中颜色互不相同. 输出至少需要的颜色数和任意一种染色方案. 分析: 证明不会,只说一下(偷瞄巨巨代码学到的)做法. 假设点的 ...

  3. UVa 1309 DLX Sudoku

    16×16的数独. 看白书学的DLX,有些细节还有待消化,贴个模板先. #include <cstdio> #include <cstring> #include <al ...

  4. HDU 5399 数学 Too Simple

    题意:有m个1~n的映射,而且对于任意的 i 满足 f1(f2(...fm(i))) = i 其中有些映射是知道的,有些是不知道的,问一共有多少种置换的组合. 分析: 首先这些置换一定是1~n的一个置 ...

  5. 启动activity的标准的action常量及对应的字符串

  6. loj2280 「FJOI2017」矩阵填数

    状压 dp.参考there #include <algorithm> #include <iostream> #include <cstring> #include ...

  7. 移动端click时间、touch事件、tap事件详解

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

  8. [python 函数学习篇] 关键字参数

    函数可以通过 关键字参数 的形式来调用,形如 keyword = value .例如,以下的函数: def parrot(voltage, state='a stiff', action='voom' ...

  9. ida动态调试笔记

    ida动态调试笔记 目标文件:阿里安全挑战赛的第二题 点击打开链接 使用环境:ida6.8点击打开链接,adt bundle点击打开链接 首先打开avd安卓模拟器,界面如下: 在dos下运行adb命令 ...

  10. POJ #1025 Department

    模拟题. 这题第一个障碍是现在少见的循环电梯 ('pater-noster' elevator) "The building has `pater-noster' elevator, i.e ...