excel 대량 데이터베이스로 데이터 가져오기

7580 단어 Excel

 
  1. publicList<Choice>GetFromXls(Stringxlsname){
  2. List<Choice>choices=newArrayList<Choice>();
  3. Choicechoice=null;
  4. try{
  5. java.io.Filefile=newjava.io.File(xlsname);
  6. Workbookbook=Workbook.getWorkbook(file);
  7. // sheet,
  8. Sheetsheet=book.getSheet(0);
  9. //
  10. introws=sheet.getRows();
  11. intcolumns=sheet.getColumns();
  12. for(inti=1;i<rows;i++){
  13. booleanhasText=false;
  14. //
  15. for(intj=0;j<columns;j++)
  16. if(sheet.getCell(j,i).getContents().length()!=0){
  17. hasText=true;
  18. break;
  19. }
  20. if(hasText){
  21. try{
  22. choice=newChoice();
  23. choice.setTypeid(Integer.parseInt(sheet.getCell(0,i).getContents()));
  24. choice.setCataid(sheet.getCell(1,i).getContents());
  25. choice.setTitle(sheet.getCell(2,i).getContents());
  26. choice.setA(sheet.getCell(3,i).getContents());
  27. choice.setB(sheet.getCell(4,i).getContents());
  28. choice.setC(sheet.getCell(5,i).getContents());
  29. choice.setD(sheet.getCell(6,i).getContents());
  30. choice.setAnswer(sheet.getCell(7,i).getContents());
  31. choices.add(choice);
  32. }catch(Exceptione){
  33. //TODO:handleexception
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38. book.close();
  39. }catch(Exceptione){
  40. e.printStackTrace();
  41. }
  42. returnchoices;
  43. }
  44. publicintbatchInsert(finalList<Choice>q,Stringxlsname){
  45. //TODOAuto-generatedmethodstub
  46. finalList<Choice>choices=GetFromXls(xlsname);
  47. finalintsize=choices.size();
  48. intresult=0;
  49. //
  50. Stringsql="insertintochoice(title,typeid,cataid,A,B,C,D,Answer)"
  51. +"values(?,?,?,?,?,?,?,?)";
  52. conn=jdbconn.getConn();
  53. try{
  54. ps=conn.prepareStatement(sql);
  55. for(inti=0;i<size;i++){
  56. Choicechoice=choices.get(i);
  57. ps.setString(1,choice.getTitle());
  58. ps.setInt(2,choice.getTypeid());
  59. ps.setString(3,choice.getCataid());
  60. ps.setString(4,choice.getA());
  61. ps.setString(5,choice.getB());
  62. ps.setString(6,choice.getC());
  63. ps.setString(7,choice.getD());
  64. ps.setString(8,choice.getAnswer());
  65. ps.executeUpdate();
  66. result++;
  67. }
  68. }catch(SQLExceptione){
  69. e.printStackTrace();
  70. }finally{
  71. jdbconn.closeDB(conn);
  72. }
  73. returnresult;
  74. }

본고는 "아바타"블로그에서 나온 것이니 반드시 이 출처를 보존하십시오http://shamrock.blog.51cto.com/2079212/702557

좋은 웹페이지 즐겨찾기