一个简单的excel文件上传到数据库方法
生活随笔
收集整理的這篇文章主要介紹了
一个简单的excel文件上传到数据库方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
因為以前項目中有用到過Excel導入,所以整理了一下,這是一個導入Excel數(shù)據(jù)到數(shù)據(jù)庫的方法
注意:需要導入poi jar包
代碼清單
/**
* Excel 導入
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward inpexcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UsForm uf = (UsForm)form;
String url = uf.getUrl();//獲取Excel文件路徑
int input = 0;
String name = null;
String age = null;
String sex = null;
String likes = null;
InputStream inputstream;
try {
inputstream = new FileInputStream(url);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(inputstream);
HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);//第一個工作表
HSSFRow hssfrow = hssfsheet.getRow(0);//第一行
//遍歷該表格中所有的工作表,i表示工作表的數(shù)量 getNumberOfSheets表示工作表的總數(shù)
for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++) {
hssfsheet = hssfworkbook.getSheetAt(i);
//遍歷該行所有的行,j表示行數(shù) getPhysicalNumberOfRows行的總數(shù)
for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
hssfrow = hssfsheet.getRow(j);
//判斷是否還存在需要導入的數(shù)據(jù)
if (hssfrow == null) {
System.out.println("這里已沒有數(shù)據(jù),在第"+i+"列,第"+j+"行");
break;
}
/**將EXCEL中的第 j 行,第一列的值插入到name*/
if (hssfrow.getCell((short) 0) == null) {
name = "";
} else if (hssfrow.getCell((short) 0).getCellType() == 0) {
name = new Double(hssfrow.getCell((short) 0).getNumericCellValue()).toString();
}
//如果EXCEL表格中的數(shù)據(jù)類型為字符串型
else {
name = hssfrow.getCell((short) 0).getStringCellValue().trim();
}
/**將EXCEL中的第 j 行,第二列的值插入到sex*/
if(hssfrow.getCell((short) 1) == null){
sex = "";
} else if(hssfrow.getCell((short) 1).getCellType() == 0) {
sex = new Double(hssfrow.getCell((short) 1).getNumericCellValue()).toString();
}
//如果EXCEL表格中的數(shù)據(jù)類型為字符串型
else {
sex = hssfrow.getCell((short) 1).getStringCellValue().trim();
}
/**將EXCEL中的第 j 行,第三列的值插入到age*/
if(hssfrow.getCell((short) 2) == null){
age = "";
} else if(hssfrow.getCell((short) 2).getCellType() == 0) {
age = new Double(hssfrow.getCell((short) 2).getNumericCellValue()).toString();
}
//如果EXCEL表格中的數(shù)據(jù)類型為字符串型
else {
age = hssfrow.getCell((short) 2).getStringCellValue().trim();
}
/**將EXCEL中的第 j 行,第三列的值插入到likes*/
if(hssfrow.getCell((short) 3) == null){
likes = "";
} else if(hssfrow.getCell((short) 3).getCellType() == 0) {
likes = new Double(hssfrow.getCell((short) 3).getNumericCellValue()).toString();
}
//如果EXCEL表格中的數(shù)據(jù)類型為字符串型
else {
likes = hssfrow.getCell((short) 3).getStringCellValue().trim();
}
name = name.trim();
sex = sex.toUpperCase();//將取到的值賦給uf對象存儲
uf.getUf().setName(name);
uf.getUf().setAge(age);
uf.getUf().setSex(sex);
uf.getUf().setLikes(likes);
Session session = HibernateSessionFactory.getSession();//hibernate 存儲
Transaction tx = session.beginTransaction();
session.save(uf.getUf());
tx.commit();
session.close();
//導入成功加1
input++;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mapping.findForward("suc");
}
總結
以上是生活随笔為你收集整理的一个简单的excel文件上传到数据库方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 外部排序-第11章-《数据结构题集》习题
- 下一篇: 印尼火山爆发(火山爆发)