excel数据导入数据库

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 static void main(String[] args) throws Exception {

 

String abspathString = "D:\\33.xls";

//String abspathString=ServletActionContext.getServletContext().getRealPath("template/33.xls");
// ClassPathResource classPathResource = new ClassPathResource("template/33.xls");
// InputStream inputStream =classPathResource.getInputStream();
// System.out.println("");
// String widz =inputStream;
File fileExcel = new File(abspathString);

parsingFile(fileExcel);

}


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;
}

}

 

posted on 2020-05-20 14:56  多言  阅读(193)  评论(0编辑  收藏  举报

导航