e代劍客——溫柔一刀

          生活就像海洋,只有意志堅強的人,才能到達彼岸

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            76 隨筆 :: 7 文章 :: 215 評論 :: 0 Trackbacks

          隨筆分類(78)

          隨筆檔案(76)

          java源碼

          友情連接

          開發文檔

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          package ?com.zhupan.spring;

          import ?java.io.File;

          import ?javax.servlet.http.HttpServletRequest;
          import ?javax.servlet.http.HttpServletResponse;

          import ?jxl.Sheet;
          import ?jxl.Workbook;

          import ?org.springframework.validation.BindException;
          import ?org.springframework.web.multipart.MultipartFile;
          import ?org.springframework.web.multipart.MultipartHttpServletRequest;
          import ?org.springframework.web.servlet.ModelAndView;
          import ?org.springframework.web.servlet.mvc.SimpleFormController;

          import ?com.ctgusec.model.Student_info;
          import ?com.ctgusec.service.IStudent_infoManage;

          /**
          ?*?
          @author ?zhupan
          ?*?
          ?
          */

          public ? class ?EStudentInsertExcelController? extends ?SimpleFormController? {

          ????
          private ?IStudent_infoManage?studentManage;

          ????@Override
          ????
          protected ?ModelAndView?onSubmit(HttpServletRequest?request,
          ????????????HttpServletResponse?response,?Object?command,?BindException?errors)
          ????????????
          throws ?Exception? {
          ????????Student_info?student_info?
          = ?(Student_info)?command;
          ????????
          try ? {????????????
          ????????????MultipartHttpServletRequest?multipartRequest?
          = ?(MultipartHttpServletRequest)?request;
          ????????????MultipartFile?file?
          = ?multipartRequest.getFile( " Excelfile " );? // ?獲得文件:
          ????????????File?toFile? = ? new ?File( " c:\\學生信息臨時文件.xls " ); // ?產生文件名和空文件
          ????????????file.transferTo(toFile); // ?文件上傳
          ????????????Workbook?book? = ?Workbook.getWorkbook(toFile); // 得到工作薄????????????
          ????????????Sheet?sheet? = ?book.getSheet( 0 ); // ?獲得第一個工作表對象
          ???????????? int ?row? = ?sheet.getRows(); // ?/得到該sheet的行數
          ???????????? int ?column? = ?sheet.getColumns();? // ?得到該sheet的列數????
          ????????????System.out.println( " 數據行數= " + row);
          ????????????System.out.println(
          " 數據列數= " + column);
          ????????????
          for ( int ?i = 1 ;i < row;i ++ )
          ????????????
          {
          ????????????????
          for ( int ?j = 0 ;j < column;j ++ )
          ????????????????
          {
          ????????????????????System.out.println(
          " j= " + j);
          ????????????????????sheet.getCell(j,?i).getContents();
          // ?得到第j列第i行的單元格的類容????????
          ????????????????????student_info.setStudentID(sheet.getCell(j,?i).getContents());????
          ????????????????????student_info.setName(sheet.getCell(
          ++ j,i).getContents());????
          ????????????????????student_info.setSex(sheet.getCell(
          ++ j,i).getContents());????
          ????????????????????student_info.setUnit(sheet.getCell(
          ++ j,i).getContents());????
          ????????????????????student_info.setClass_(sheet.getCell(
          ++ j,i).getContents());????
          ????????????????????student_info.setSpecialty(sheet.getCell(
          ++ j,i).getContents());????
          ????????????????????student_info.setRemark(sheet.getCell(
          ++ j,i).getContents());????????????????????????
          ????????????????}
          ????
          ????????????????
          if ?( this .studentManage.getStudentByStudentID(
          ????????????????????????student_info.getStudentID()).size()?
          != ? 0 )
          ????????????????????
          return ? new ?ModelAndView( " education/e-studentInfoAddError " );
          ????????????????
          this .studentManage.insertStudent_info(student_info);????
          ????????????}
          ????????????????
          ????????????book.close();
          ????????????
          return ? new ?ModelAndView( " education/e-studentInfoAddExcelSuccess " , " row " , new ?Integer(row - 1 ));
          ????????}
          ? catch ?(Exception?e)? {????????????????????
          ????????????e.printStackTrace();
          ????????}

          ????????
          return ? new ?ModelAndView( " education/e-studentInfoAddExcelError " );
          ????}


          ????
          public ? void ?setStudentManage(IStudent_infoManage?studentManage)? {
          ????????
          this .studentManage? = ?studentManage;
          ????}
          ????
          }

          posted on 2006-10-07 15:05 溫柔一刀 閱讀(936) 評論(1)  編輯  收藏 所屬分類: java相關

          評論

          # re: 將Excel文件內容寫入到數據庫 2006-10-27 18:20 junitfans
          這個更直接啊  回復  更多評論
            

          聯系偶 zhupanjava@gmail.com 溫柔一刀
          主站蜘蛛池模板: 三台县| 息烽县| 湖南省| 贵阳市| 集贤县| 邻水| 盐池县| 开化县| 安仁县| 黄平县| 陆河县| 高州市| 紫阳县| 闻喜县| 铜山县| 南涧| 葵青区| 喜德县| 纳雍县| 岳阳县| 清新县| 厦门市| 宜兰市| 防城港市| 巨野县| 乌海市| 乡城县| 绥棱县| 千阳县| 方山县| 梁平县| 丰台区| 杭锦旗| 温州市| 毕节市| 汉源县| 永川市| 启东市| 广安市| 万宁市| 莱州市|