ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

excel数据导入数据库

2020-05-20 15:03:16  阅读:251  来源: 互联网

标签:cellData 数据库 excel return else cell 导入 import null


package com.sinosoft.cms.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.sinosoft.cms.entity.InsuranceSupervision;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sinosoft.mis.dao.BiddingInformationDao;
public class parsingFileTest {
private Workbook wb;
private BiddingInformationDao biddingInformationDao;

/**
* 将cell转换成相应类型
* @param cell
* @return
*/
public static Object getCellFormatValue(Cell cell){
Object cellValue = null;
if(cell!=null){
//判断cell类型
switch(cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC:{
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA:{
//判断cell是否为日期格式
if(DateUtil.isCellDateFormatted(cell)){
//转换为日期格式YYYY-mm-dd
cellValue = cell.getDateCellValue();
}else{
//数字
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
}
case Cell.CELL_TYPE_STRING:{
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}else{
cellValue = "";
}
return cellValue;
}

public String parsingFile(File fileExcel) {
// TODO Auto-generated method stub
try{
InputStream is = new FileInputStream(fileExcel.getPath());
if(fileExcel.getName().endsWith(".xls")){
wb = new HSSFWorkbook(is);
}else if(fileExcel.getName().endsWith(".xlsx")){
wb = new XSSFWorkbook(is);
}
if(wb != null){
Sheet sheet = wb.getSheetAt(0);
int rownum = sheet.getPhysicalNumberOfRows();
Row row = sheet.getRow(1);
//获取最大列数
int colnum = row.getPhysicalNumberOfCells();
//logger.info("上传文件最大列数=="+colnum);
List<Map<String,String>> listMap = new ArrayList<Map<String,String>>();
List<Object> listList = new ArrayList<Object>();
if(colnum!=3){
return "上传文档格式有误!";
}
for (int i = 2; i<rownum; i++) {//循环行
InsuranceSupervision raiInsured = new InsuranceSupervision();
row = sheet.getRow(i);
if(row !=null){
for (int j=0;j<colnum;j++){//循环列
String cellData = (String) getCellFormatValue(row.getCell(j));//列的值
if(j==0){//
if (cellData != null && !"".equals(cellData)) {
raiInsured.setBjh(cellData);
}
}else if(j==1){
if (cellData != null && !"".equals(cellData)) {
raiInsured.setProductName(cellData);
}
}else if(j==2){ 
if (cellData != null && !"".equals(cellData)) {
if(cellData != null && !"".equals(cellData)){
if(isPhone(cellData)){//校验电话号
raiInsured.setApplicantMobile(cellData);
}else{
return"联系电话格式错误!";
}
}else{
return "联系电话不可为空!";
}
}
}
}
}else{
break;
}
listList.add(raiInsured);
}
try{
biddingInformationDao.raiCaiji(listList);
}catch(Exception e){
e.printStackTrace();

return "储存异常";
}
}
return "Y";
}catch(Exception e){
e.printStackTrace();
return"上传文件数据有误";
}
}

/**
* 验证手机号,固定电话号
*/
public boolean isPhone(String str) {
Pattern p1 = null, p2 = null ,p3 = null;
Matcher m = null;
boolean isPhone = false;
p1 = Pattern.compile("^[0][0-9]{2,3}-[0-9]{5,10}$"); // 验证带区号的
p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 验证没有区号的
//校验手机号正则表达式
p3 = Pattern.compile("^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$");
if (str.lastIndexOf("-")>-1) {
m = p1.matcher(str);
isPhone = m.matches();
} else if(str.length() > 6 && str.length() < 9){
m = p2.matcher(str);
isPhone = m.matches();
}else if(str.length() > 10){
m = p3.matcher(str);
isPhone = m.matches();
}
return isPhone;
}

}

 

标签:cellData,数据库,excel,return,else,cell,导入,import,null
来源: https://www.cnblogs.com/duoyan/p/12923638.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有