设为首页 收藏本站
查看: 289|回复: 0

[经验分享] how to transfer excel data to mysql using poi

[复制链接]

尚未签到

发表于 2016-10-19 07:39:20 | 显示全部楼层 |阅读模式
  在我们日常的开发中经常需要把excel文件的数据导入数据库中,近期做项目需要实现这样的需求,现把实现过程分享给大家:
    到官方网站下载poi包最新版本。放入项目lib目录下。经研究发现poi的接口不知道为什么没有做的尽善尽美:
   1、在表格为空时会报错
   2、为日期型时获取到的日期不准确
   3、没有整型值的获取方法
   遂进行二次封装,以下是项目中部分源代码的摘录。
   封装类如下:
 

package com.toto.service;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 
/**
* <p>封装poi常用操作</p>
* @author zxc
*
*/
public class ExcelService {
/**
* <p>单元格格式为日期时,获取指定形式的日期格式</p>
* @param cell
* @return The value
*/
public String getExcelDateTime(HSSFCell cell) {
if (isBlank(cell))
return null;
double d =cell.getNumericCellValue();
Datedate = HSSFDateUtil.getJavaDate(d);
SimpleDateFormatsFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Strings_datetime = sFormat.format(date);
return s_datetime;
}
 
/**
* <p>空判断</p>
* @param cell
* @return The value
*/
public boolean isBlank(HSSFCellcell) {
if (null == cell)
return true;
int cellType =cell.getCellType();
if (HSSFCell.CELL_TYPE_BLANK == cellType) {
return true;
}
return false;
}
 
/**
* <p>取得整型格式</p>
* @param cell
* @return The value
*/
public String getExcelInt(HSSFCell cell) {
if (isBlank(cell))
return null;
return (int)cell.getNumericCellValue() + "";
}
/**
* <p>取得长整型格式</p>
* @param cell
* @return The value
*/
public String getExcelLong(HSSFCell cell) {
if (isBlank(cell))
return null;
return (long)cell.getNumericCellValue() + "";
}
/**
* <p>取得长整型格式</p>
* @param cell
* @return The value
*/
public String getExcelDouble(HSSFCell cell) {
if (isBlank(cell))
return null;
returncell.getNumericCellValue() + "";
}
/**
* <p>取得字符串形式
* @param cell
* @return The value
*/
public String getExcelString(HSSFCell cell) {
if (isBlank(cell))
return null;
returncell.getStringCellValue();
}
}
上传文件成功后立刻调用importGameBack 方法,Bo中的导入方法如下:

/**
* 导excel进mysql
* @param pathWithName:物理目录
* @return error info if fail
* @throws FileNotFoundException
* @throws IOException
*/
public String importGameBack(StringpathWithName) throws FileNotFoundException, IOException {
  DAO dao = factory.getDaoFactory().getDao();
  ExcelService es = new ExcelService();
  StringBuffer sb = new StringBuffer();
  HSSFWorkbook workbook = new HSSFWorkbook(newFileInputStream(pathWithName));
  HSSFSheet sheet = workbook.getSheetAt(0);
  Iterator iter = sheet.iterator();
if(iter.hasNext())
  iter.next();
for(;iter.hasNext();){
  GameForm gameForm = new GameForm();
  setDefaultVal4Import(gameForm);
  HSSFRow row = (HSSFRow) iter.next();
if(es.isBlank(row.getCell(0))){
break;
  }
  String result = setVal4Import(gameForm,row);
if(result.length() > 0){
  sb.append(result);
break;
  }
if( ! dao.insert("game.insert.only", gameForm)){
  sb.append("Error:excel row:").append(row.getRowNum()+ 1);
break;
  }
  }
return sb.toString();
}
 
/**
* <p>设置表记录默认值</p>
* @param gameForm
*/
public void setDefaultVal4Import(GameForm gameForm){
UserBOubo = factory.getBoFactory().getUserBO();
Stringnow = ubo.getNow();
gameForm.setAdd_time(now);
gameForm.setSort_time(now);
}
/**
* <p>根据excel设置表记录值</p>
* @param gameForm
* @param row
* @return
*/
public String setVal4Import(GameForm gameForm,HSSFRow row){
StringBuffersb = new StringBuffer();
ExcelServicees = new ExcelService();
try{
Stringis_event = es.getExcelInt(row.getCell(0));
if(null == is_event){
sb.append("is_event is null");
return sb.toString();
}
gameForm.setIs_event(is_event);
Stringrace_time = es.getExcelDateTime(row.getCell(1));
if(null == race_time){
sb.append("race_time is null");
return sb.toString();
}
gameForm.setRace_time(race_time);
Stringgame_type = es.getExcelInt(row.getCell(2));
if(null == game_type){
sb.append("game_type is null");
return sb.toString();
}
gameForm.setGame_type(game_type);
Stringleague_id = es.getExcelInt(row.getCell(3));
if(null == league_id){
sb.append("league_id is null");
return sb.toString();
}
gameForm.setLeague_id(league_id);
Stringmaster_team = es.getExcelString(row.getCell(4));
if(null == master_team){
sb.append("master_team is null");
return sb.toString();
}
gameForm.setMaster_team(master_team);
Stringguest_team = es.getExcelString(row.getCell(5));
if(null == guest_team){
sb.append("guest_team is null");
return sb.toString();
}
gameForm.setGuest_team(guest_team);
Stringmaster_val = es.getExcelDouble(row.getCell(7));
if(null == master_val){
sb.append("master_val is null");
return sb.toString();
}
gameForm.setMaster_val(master_val);
Stringguest_val = es.getExcelDouble(row.getCell(9));
if(null == guest_val){
sb.append("guest_val is null");
return sb.toString();
}
gameForm.setGuest_val(guest_val);
gameForm.setRef_val(es.getExcelDouble(row.getCell(6)));
gameForm.setPeace_val(es.getExcelDouble(row.getCell(8)));
}catch(Exception e){
sb.append("row:").append(row.getRowNum());
}
return "";
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-288111-1-1.html 上篇帖子: 【转】MySQL数据库简单实用的优化方法 下篇帖子: MySQL源码学习:InnoDB的ib_logfile写入策略
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表