java实现Ecel导入功代码

上传人:m**** 文档编号:53414775 上传时间:2022-02-10 格式:DOC 页数:11 大小:72.50KB
返回 下载 相关 举报
java实现Ecel导入功代码_第1页
第1页 / 共11页
java实现Ecel导入功代码_第2页
第2页 / 共11页
java实现Ecel导入功代码_第3页
第3页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
java实现Excel功能代码本源码来源于开源系统MyExam大家可以去开源中国社区和各大下载站去下载myexam本系统开放源代码,完全免费前台代码:vform id =importForm method =posten ctype =multipart/form-data : inputid =downloadBtntype =buttonvalue = /in putid =importBt ntype =butt on value = /js文件:defi ne( fun cti on(require)var $ = require(jquery);var contextPath=$(#contextPath ).val();require( dialog)($);require( uriTemplate )($);var IFA = require( util );require( dateTimePicker )($);require( ./././styles/cim/jquery-ui-1.8.14.custom.css);$( #importBtn).click(function()var url =contextPath+/question/import.do$( #importForm).attr( action,url);$( #importForm).submit(););$( #downloadBtn).click( function ()url = con textPath+/dow nl oad/questi on Template.xlsxlocati on .href = url;););Java代码:*导入试题* author Eric*/SuppressWarnings (unchecked)RequestMapping (value =/question/import.json, method =RequestMethod. POST)public Stri ng create( final ExcelDTO excelDTO,final Bindin gResult result,final ModelMap model,HttpServletRequest request)excelValidator .validate(excelDTO, result);/插入数据的listListvimportQuestio nDTO importQuesti on List =newLin kedListvimportQuestio nDTO();if (result.hasErrors() );true );model.put( importFileTypeErrorl, importFileTypeError else try final Map)parse.get( formatCorrectList); catch (Exception e) model.put( importFileReadErrorMsgl, true);return view(model,importQuestio nView, TopMe nu.EXAMSideMenu. QUESTION_MANAGEMENTrequest); finalMapvStri ng,Object resultMap=insertDB(importQuestion List,model.put(model.put(model.put(lastId,resultMap.get(,resultMap.get(importVoucherSuccessFlaglcou ntin sertLastIdsuccessCou nttrue);););,TopMe nu.EXAMreturn view(model,importQuestio nViewSideMenu. QUESTION_MANAGEMENT/* 插入数据* Descripti on:* * author Eric*/privateMap in sertDB(List question List,HttpServletRequest request) final Date date = new Date();finalIn teger creatorA ndModifier = (In teger)securityHelper .getID(request);for (importQuesti on DTO questi onDTO : questi on List) setDefaultValue(date, creatorA ndModifier, questi on DTO);/return pers on ProfileService.importClubCustomer(clubCustomerList); return n ull ;* Descriptio n:设置其他默认必须数据* * author Eric*/private void setDefaultValue( final Date date,finalIn teger creatorA ndModifier,importQuestio nDTO questio nDTO) questio nDTO.setCreatio nDate(new Date();questio nDTO.setCreator(creatorA ndModifier);questi on DTO.setModificati on Date(new Date();questio nDTO.setModifier(creatorA ndModifier);questio nDTO.setOrigi n(import);/* * Description:解析 excel* * author Eric*/privateMap map =new HashMapvStri ng, Object(3);Workbook workbook =n ull ;try CommonsMultipartFile file=(CommonsMultipartFile) excelDTO.getFiles().get(O);String fileName = file.getFileltem().getName();String extension = fileName.substring(fileName.lastIndexOf(. ) + 1);In putStream is = file.getl nputStream();/ if (xlsx.equals(exte nsion) / XSSFWorkbook workbook2007 = new XSSFWorkbook(is);/ final XSSFSheet sheet = workbook2007.getSheetAt(0);/ final int totalRows = sheet.getLastRowNum();/ if (totalRows 3000) / map.put(dataTooManyErrorFlag, true);/ else / map.putAll(packagePerso nProfile2007(sheet, totalRows);/ map.put(dataTooManyErrorFlag, false);/ / workbook = Workbook. getWorkbook (is);final Sheet sheet = workbook.getSheet(0);final int totalRows = sheet.getRows();if (totalRows 3000) map.put( dataTooManyErrorFlag , true ); else map.putAII(packagePersonProfile(sheet, totalRows); map.put( dataTooManyErrorFlag , false ); catch (BiffException e) throw e; catch (IOException e) throw e; fin allyif (workbook != null ) workbook.close();return map;/* * Description:将excel2003 圭寸装成 list* * author Eric*/private MapStri ng, List packagePers on Profile(final Sheet sheet, finalIn teger totalRows) newfinal ListvimportQuestio nDTO formatCorrectList =Lin kedListvimportQuestio nDTO();newfinalMapvStri ng, List packageResult =HashMapvStri ng, ListvimportQuestio nDTO(2);for ( int i = 1; i totalRows; i+) importQuesti on DTO questi on DTO =new importQuesti on DTO();List optio nDTO=new ArrayList();final String content = sheet.getCell(0, i).getContents().trim();final String analyse = sheet.getCell(1, i).getContents().trim();final String answer = sheet.getCell(2, i).getContents().trim();finalStri ng category = sheet.getCell(3, i).getC onten ts().trim();int num=n um(sheet, i);for (int k = num; k 3; k-) Optio nDTO optio nDTO2=new Optio nDTO();optio nDTO2.setCo nte nt(sheet.getCell(k, i).getCo nten ts().trim();optio nDTO.add(optio nDT02);try questi onDTO = con structclubCustomerDTO(c ontent, an alyse, an swer, category, (ArrayListvOpti on DTO) opti on DTO);formatCorrectList.add(questio nDTO); catch (NumberFormatException e) /log.catch in g(e);packageResult.put( formatCorrectList, formatCorrectList);retur npackageResult;/* vp* Description:封装所需的 DTO* v/p* author Eric*/privateimportQuesti on DTO con structclubCustomerDTO(Str ing content,String an alyse, String an swer, String category,ArrayList optionDTO) finalimportQuestio nDTO importQuestio nDTO =new importQuestio nDTO();importQuestio nDTO.setC onten t(c on te nt);importQuestio nDTO.setA nalysis(a nalyse);importQuestio nDTO.setA nswer(a nswer);importQuesti on DTO.setCategory(category);importQuestio nDTO.setOptio ns(optio nDTO);returnimportQuestio nDTO;finalIn teger i)public int num( final Sheet sheet, int a;for (int j = 0; ; j+) if (StringUtils.isBlank (sheet.getCell(4+j, i).getContents().trim()a=4+j;break ;return a;* Description:将excel2007 圭寸装成 list* * author Eric*/private Map packagePerson Profile2007( final XSSFSheet sheet, finalIn teger totalRows) / 填充的listfinalList forma newLin kedListvQuestio nDTO();finalMapvString,List packageResult = new HashMapvString,List(2);for ( int i = 1; i totalRows; i+) Questio nDTO clubCustomerDTO =new Questio nDTO();retur n packageResult;/*导入试题* author Eric*/RequestMapping (value = /importPaper.do , method = RequestMethod. GET) public String importPaperl ndex(final ModelMap model)return view(model, importPaperView , TopMe nu. EXAMSideMe nu. PAPER_MANAGEMEN;TService 代码:publicMap importQuesti on(finalList question List) /插入新数据,并返回最后插入的perso nProfile 的idfinalIn teger in sertLastId =questi on Dao.in sertQuesti on Batch(questi on List);final MapvStri ng, ObjectresultMap =new HashMapvStri ng, Object();,in sertLastId);resultMap.put(in sertLastIdretur nresultMap;DAOpublicIn teger in sertQuesti on Batch(finalListvimportQuestio nDTO questio nDTOs) In teger lastId = (In teger) getSqlMapClie ntTemplate().execute( new SqlMapClie ntCallback() public Object doI nSqlMapClie nt(finalSqlMapExecutor executor)throws SQLExcepti on executor.startBatch();for(importQuestio nDTO clubCustomerDTOs :questi on DTOs) int id=(I nteger) executor.i nsert( questi on .i nsert clubCustomerDTOs);ListvOptio nDTOoptio nDTOs=clubCustomerDTOs.getOptio ns();for(Optio nDTO optio nDTO:optio nDTOs ) optio nDTO.setQuestio nld(id); executor.i nsert(questi on .i nsertOpti on optio nDTO);executor.executeBatch();fi nal In teger id = (In teger) executor.queryForObject(pers on _profile.selectLastl nsertID); return 5;);return lastId;/根据id删除public void deleteQuestion(int id) ,id);getSqlMapCIie ntTemplate().delete(questio n.deleteByldpublic int in sertQuesti on( Questi onDTO questi on DTO) questi on .i nsertreturn(In teger) getSqlMapClie ntTemplate().i nsert(questio nDTO);大家可以下载源代码,慢慢研究吧
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 活动策划


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!