java 如何实现excel中数据导入到数据库?
急求!!
拜托了

解决方案 »

  1.   

    用poipublic void importCollege(final int year, final String filePath)
    {
    Session session = getSession();
    HSSFSheet sheet = null;
    try
    {
    sheet = Execel.getSheet(filePath);
    }
    catch (IOException e)
    {
    try
    {
    throw e;
    }
    catch (IOException e1)
    {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    }
    int rows = sheet.getPhysicalNumberOfRows();

    for (int i = 0; i < rows; i++)
    {
    HSSFRow row =sheet.getRow(i);
    HSSFCell cell0 = row.getCell(0);
    HSSFCell cell1 = row.getCell(1);
    College college = new College();
    college.setForYear(year);
    college.setName(Execel.getCellStr(cell0));
    college.setAddress(Execel.getCellStr(cell1));
    session.save(college);
    if (i%100==0)
    {
    session.flush();
    session.clear();
    }
    }
    session.close();
    }
    public class Execel
    {
    public static HSSFSheet getSheet(String filePath) throws IOException
    {
    HSSFWorkbook workbook = null;
     try
    {
    workbook = new HSSFWorkbook(new FileInputStream(
     filePath));
    }
    catch (FileNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw e;
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    throw e;

     HSSFSheet sheet = workbook.getSheetAt(0);
     return sheet;
    }
    public static String getCellStr(HSSFCell cell)
    {
    if (cell.getCellType()==0)
    {
    return (int)cell.getNumericCellValue()+"";
    }
    else {
    return cell.getStringCellValue();
    }
    }
    }写得不是很好,多指教