- publicList<Choice>GetFromXls(Stringxlsname){
- List<Choice>choices=newArrayList<Choice>();
- Choicechoice=null;
- try{
- java.io.Filefile=newjava.io.File(xlsname);
- Workbookbook=Workbook.getWorkbook(file);
- //获得第一个sheet,默认有三个
- Sheetsheet=book.getSheet(0);
- //一共有多少行多少列数据
- introws=sheet.getRows();
- intcolumns=sheet.getColumns();
- for(inti=1;i<rows;i++){
- booleanhasText=false;
- //过滤掉没有文本内容的行
- for(intj=0;j<columns;j++)
- if(sheet.getCell(j,i).getContents().length()!=0){
- hasText=true;
- break;
- }
- if(hasText){
- try{
- choice=newChoice();
- choice.setTypeid(Integer.parseInt(sheet.getCell(0,i).getContents()));
- choice.setCataid(sheet.getCell(1,i).getContents());
- choice.setTitle(sheet.getCell(2,i).getContents());
- choice.setA(sheet.getCell(3,i).getContents());
- choice.setB(sheet.getCell(4,i).getContents());
- choice.setC(sheet.getCell(5,i).getContents());
- choice.setD(sheet.getCell(6,i).getContents());
- choice.setAnswer(sheet.getCell(7,i).getContents());
- choices.add(choice);
- }catch(Exceptione){
- //TODO:handleexception
- e.printStackTrace();
- }
- }
- }
- book.close();
- }catch(Exceptione){
- e.printStackTrace();
- }
- returnchoices;
- }
- publicintbatchInsert(finalList<Choice>q,Stringxlsname){
- //TODOAuto-generatedmethodstub
- finalList<Choice>choices=GetFromXls(xlsname);
- finalintsize=choices.size();
- intresult=0;
- //此处应当获取题型进行插入数据库
- Stringsql="insertintochoice(title,typeid,cataid,A,B,C,D,Answer)"
- +"values(?,?,?,?,?,?,?,?)";
- conn=jdbconn.getConn();
- try{
- ps=conn.prepareStatement(sql);
- for(inti=0;i<size;i++){
- Choicechoice=choices.get(i);
- ps.setString(1,choice.getTitle());
- ps.setInt(2,choice.getTypeid());
- ps.setString(3,choice.getCataid());
- ps.setString(4,choice.getA());
- ps.setString(5,choice.getB());
- ps.setString(6,choice.getC());
- ps.setString(7,choice.getD());
- ps.setString(8,choice.getAnswer());
- ps.executeUpdate();
- result++;
- }
- }catch(SQLExceptione){
- e.printStackTrace();
- }finally{
- jdbconn.closeDB(conn);
- }
- returnresult;
- }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702557