- public List<Choice> GetFromXls(String xlsname){
- List<Choice> choices = new ArrayList<Choice>();
- Choice choice=null;
- try {
- java.io.File file=new java.io.File(xlsname);
- Workbook book = Workbook.getWorkbook(file);
- // 得到第一個sheet,默認有三個
- Sheet sheet = book.getSheet(0);
- // 一共有多少行多少列數據
- int rows = sheet.getRows();
- int columns = sheet.getColumns();
- for (int i = 1; i <rows; i++) {
- boolean hasText = false;
- // 過濾掉沒有文本內容的行
- for (int j = 0; j < columns; j++)
- if (sheet.getCell(j, i).getContents().length()!=0) {
- hasText = true;
- break;
- }
- if (hasText) {
- try {
- choice = new Choice();
- 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 (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- }
- book.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return choices;
- }
- public int batchInsert(final List<Choice> q,String xlsname) {
- // TODO Auto-generated method stub
- final List<Choice> choices = GetFromXls(xlsname);
- final int size = choices.size();
- int result=0;
- //此處應當獲取題型進行插入數據庫
- String sql = "insert into choice(title,typeid,cataid,A,B,C,D,Answer) "
- + "values(?,?,?,?,?,?,?,?)";
- conn=jdbconn.getConn();
- try{
- ps=conn.prepareStatement(sql);
- for(int i=0;i<size;i++){
- Choice choice = 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 (SQLException e) {
- e.printStackTrace();
- } finally {
- jdbconn.closeDB(conn);
- }
- return result;
- }