将Excel文件数据导入到数据库表中(Java版)

fuyun 2009-09-17
代码如下,因相对简单,具体说明忽略.
<%@page language="java" contentType="text/html; charset=utf-8" %>
<%@page import="java.io.File"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.util.Vector"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@page import="org.apache.poi.poifs.filesystem.POIFSFileSystem"%>
<%@page import="com.fuyun.hp.common.PubUtil"/>
<%@page import="com.fuyun.hp.hibernate.mapping.RadioType"%>
<%@page import="com.fuyun.hp.hibernate.mapping.RadioTypeDAO"%>
<%@page import="com.fuyun.hp.common.SpringContext"/>
<%@page import="org.springframework.orm.hibernate3.HibernateTransactionManager"/>
<%@page import="org.springframework.transaction.TransactionDefinition"/>
<%@page import="org.springframework.transaction.TransactionStatus"/>
<%@page import="org.springframework.transaction.support.DefaultTransactionDefinition"/>
<%  
  //出于考虑显示格式,实际运行时,请将代码中的全角空格转换为半角空格
  //代码中的PubUtil.nvl(Object)方法,当Object为null时返回空串(也就是类似Oracle中的nvl方法)
  request.setCharacterEncoding("utf-8");
  File tempPath = new File(getServletContext().getRealPath("/")
    + AdsUtil.TEMP_UPLOAD_PATH);
  if (!tempPath.exists())
    return;

  String fileName = "radioTree.xls";
  fileName = tempPath.toString() + "\\" + fileName;
  InputStream inp = new FileInputStream(fileName);
  HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
  HSSFSheet sheet = wb.getSheetAt(0);
  RadioType rt;
  RadioTypeDAO rtDao = RadioTypeDAO.getInstance();
  
  HibernateTransactionManager tManager = SpringContext.getTransactionManager();
  TransactionDefinition td = new DefaultTransactionDefinition();
  TransactionStatus ts = tManager.getTransaction(td);
  
  for (Iterator<HSSFRow> rit = (Iterator<HSSFRow>)sheet.rowIterator(); rit.hasNext(); ) {
    HSSFRow row = rit.next();
    boolean existFlag = true;
    for(short index = 0;index < row.getPhysicalNumberOfCells();index ++){
      HSSFCell cell = row.getCell(index);
      if(index == row.getLastCellNum() && cell == null)
        existFlag = false;
      else if(index < row.getLastCellNum() && cell == null)
        continue;
      else
        break;
    }
    if(existFlag == false){
      break;
    }
    else{
      Vector<String> values = new Vector<String>();
      rt = null;
      try{
        if(row.getRowNum() == 0)//从第2行开始读取
          continue;
        for (int index = 0; index < 5; index ++) {
          HSSFCell cell = row.getCell(new Short(index + ""));
          if(cell == null)
            values.add("");
          else{
            if(cell.getCellType() == cell.CELL_TYPE_NUMERIC){
              values.add(String.format("%.0f", cell.getNumericCellValue()));
            }
            else
              values.add(PubUtil.nvl(cell.getStringCellValue()));
          }
        }
        
        if("".equals(PubUtil.nvl(values.get(0)))){
          rt = new RadioType();
        }
        else{
          rt = rtDao.get(values.get(0));
        }
        rt.setId(values.get(0));//id
        rt.setParentId(PubUtil.nvl(values.get(1)));//上级id
        rt.setName(PubUtil.nvl(values.get(2)));//名称
        rt.setType(PubUtil.nvl(values.get(3)));//类型
        rt.setOrderIndex(Integer.parseInt(PubUtil.nvl(values.get(4))));//排序
        
        if("".equals(PubUtil.nvl(values.get(0)))){
          rtDao.save(rt);
        }
        else{
          rtDao.update(rt);
        }
        out.println(rt);
      }
      catch(Exception e){
        tManager.rollback(ts);
        e.printStackTrace();
        out.print("{success: false, info:'导入失败!'}");
        return;
      }
    }
  }
  tManager.commit(ts);
  out.print("{success: true}");
 %>
jingsql 2012-07-17
你好,你能给我提供完整的将Excel文件数据导入到数据库表中代码吗,包含连库的代码,非常感谢啊,邮箱jingsql@126.com
shaoli1968 2014-12-17
刚学jsp  练习jsp操作excel2007  能否提供代码
shaoli1968 2014-12-17
shaoli1968@126.com
1458595567@qq.com
Global site tag (gtag.js) - Google Analytics