比如我们遇到一些需要把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. sql执行过长,如何入手优化

    一条sql执行过长的时间,你如何优化,从哪些方面 1.查看sql是否涉及多表的联表或者子查询,如果有,看是否能进行业务拆分,相关字段冗余或者合并成临时表(业务和算法的优化)2.涉及链表的查询,是否能进 ...

  2. hierarchy viewer不能获取userbuild手机版本的UI布局

    步骤很详细:http://maider.blog.sohu.com/255485243.html 其中的第7步命令需要更改为: java -jar smali-2.0.3.jar ./out -o c ...

  3. HT1621控制的段式液晶驱动程序

    MCU是STM8S207 /*LED 字模结构*/ typedef struct { char mChar; u8 mModal; }LED_MODAL_DEFINE; typedef struct ...

  4. MySQL 的MAC终端一些指令总结

    开启MySQL服务 sudo /usr/local/mysql/support-files/mysql.server start 关闭MySQL服务 udo /usr/local/mysql/supp ...

  5. 强大的with语句

    上下文管理器对象存在的目的是管理 with 语句,就像迭代器的存在是为了管理 for 语句一样. with 语句的目的是简化 try/finally 模式.这种模式用于保证一段代码运行完毕后执行某项操 ...

  6. day04_09 while循环03

    练习题: 3.如何输入一个如下的直角三角形,用户指定输出行数:(如果上下反转,右如何实现?) ********** 以下是自己的思路,没有按照上课老师的思路,反正经过不断的测试改进得出的算法 num ...

  7. JS使用onerror进行默认图像显示,可代替alt

    JS代码 //图像加载出错时的处理 function errorImg(img) { img.src = "默认图片.jpg"; img.onerror = null; } HTM ...

  8. Clarke and five-pointed star

    Clarke is a patient with multiple personality disorder. One day, Clarke turned into a learner of geo ...

  9. TOJ4537: n阶行列式

    4537: n阶行列式  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 28       ...

  10. 台州学院we are without brain 训练 计算几何

    A - View Angle Flatland has recently introduced a new type of an eye check for the driver's licence. ...