java 如何实现excel中数据导入到数据库?
急求!!
拜托了
急求!!
拜托了
解决方案 »
- MyEclipse8.5 配置Ext 提示代码详细配置 求解!(不要给我从百度Copy来的解答,谢谢)
- spring+hibernate 延迟加载问题的完美解决方法
- JMS小问题(JBOSS)
- [100分]请教Tomcat服务器问题,高手请进~
- 用JS如何限制上传文件格式
- hibernate3.0: The dialect was not set. Set the property hibernate.dialect怎么解决啊?
- 请教大侠们,急!!!!!
- 载入图标,图标却是花的情况
- ejb一问
- spring+hibernate配置双数据源的问题
- 高分求助,jboss下,Jsp页面调用EJB中的方法出错,总是报编译不了
- spring2.5.6+hibernate3.3.1启动tomcat错误
{
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();
}
}
}写得不是很好,多指教