Excel导入功能
一:前端
<t:dgToolBar title="Excel题库导入" icon="icon-search" onclick="questionImportListImportXls()"></t:dgToolBar>
<script type="text/javascript" charset="utf-8">
function questionImportListImportXls() {
openuploadwin('Excel题库导入', 'xueBaQuestionController.do?upload', "questionImportList");
}
</script>
二:openuploadwin
/**
* 创建上传页面窗口
*
* @param title
* @param addurl
* @param saveurl
*/
function openuploadwin(title, url,name,width, height) {
gridname=name;
$.dialog({
content: 'url:'+url,
cache:false,
button: [
{
name: '开始上传',
callback: function(){
iframe = this.iframe.contentWindow;
iframe.upload();
return false;
},
focus: true
},
{
name: '取消上传',
callback: function(){
iframe = this.iframe.contentWindow;
iframe.cancel();
}
}
]
}).zindex();
}
三:上传页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>Excel题库导入</title>
<t:base type="jquery,easyui,tools"></t:base>
</head>
<body style="overflow-y: hidden" scroll="no">
<t:formvalid formid="formobj" layout="div" dialog="true" beforeSubmit="upload">
<fieldset class="step">
<div class="form"><t:upload name="fiels" buttonText="选择要导入的文件" uploader="xueBaQuestionController.do?importExcel" extend="*.xls;*.xlsx" id="file_upload" formData="documentTitle"></t:upload></div>
<div class="form" id="filediv" style="height: 50px"></div>
</fieldset>
</t:formvalid>
</body>
</html>
四:xueBaQuestionController处理导入题库
@RequestMapping(params = "upload")
public ModelAndView upload(HttpServletRequest req) {
return new ModelAndView("weixin/shyd/happycampus/xueba/questionUpload");
}
@RequestMapping(params = "importExcel", method = RequestMethod.POST)
@ResponseBody
public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
AjaxJson j = new AjaxJson();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
MultipartFile file = entity.getValue();// 获取上传文件对象
ImportParams params = new ImportParams();
params.setTitleRows(0);
params.setSecondTitleRows(1);
params.setNeedSave(false);
try {
List<XueBaQuestionEntity> questionList = GetAllImportQuestion(file.getInputStream());
for (XueBaQuestionEntity question : questionList) {
if(question.getContent()!=null){
xueBaQuestionService.saveQuestion(question);
}
}
j.setMsg("文件导入成功!");
} catch (Exception e) {
j.setMsg("文件导入失败!");
logger.error(ExceptionUtil.getExceptionMessage(e));
}finally{
try {
file.getInputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return j;
}
private List<XueBaQuestionEntity> GetAllImportQuestion(InputStream inputstream) {
POIFSFileSystem fs;
HSSFWorkbook wb;
HSSFSheet sheet;
HSSFRow row;
List<XueBaQuestionEntity> questionList = new ArrayList<XueBaQuestionEntity>();
List<XueBaOptionEntity> optionList;
XueBaQuestionEntity question = null;
try{
fs = new POIFSFileSystem(inputstream);
wb = new HSSFWorkbook(fs);
/** 遍历sheet **/
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
/** 获得当前sheet **/
sheet = wb.getSheetAt(i);
int num = 1;
for (int j = 1; j < sheet.getPhysicalNumberOfRows() ; j++) {
num++;
try{
question = new XueBaQuestionEntity();
optionList = new ArrayList<XueBaOptionEntity>();
/** 获得当前行情 **/
row = sheet.getRow(j);
if(row != null){
String qContent = getCellFormatValue(row.getCell(0)).trim();
String aOption = getCellFormatValue(row.getCell(1)).trim();
//题目或A选项为空就不保存
if(StringUtil.isEmpty(qContent) || StringUtil.isEmpty(aOption)){
logger.info("题库第" + num + "行题库或A选项为空,未保存");
continue;
}
String bOption = getCellFormatValue(row.getCell(2)).trim();
String cOption = getCellFormatValue(row.getCell(3)).trim();
String dOption = getCellFormatValue(row.getCell(4)).trim();
String eOption = getCellFormatValue(row.getCell(5)).trim();
String answer = getCellFormatValue(row.getCell(6)).trim();
System.out.println("qcontent:"+qContent);
/* 赋值问题实体 */
question.setContent(qContent);
if(answer.indexOf(",")>0){
question.setType(1);
}else{
question.setType(0);
}
question.setAnswer(answer);
//赋值选项实体
if (StringUtil.isNotEmpty(aOption)) {
XueBaOptionEntity aOptionEntity = new XueBaOptionEntity();
aOptionEntity = DealOption(aOptionEntity,aOption);
optionList.add(aOptionEntity);
}
if (StringUtil.isNotEmpty(bOption)) {
XueBaOptionEntity bOptionEntity = new XueBaOptionEntity();
bOptionEntity = DealOption(bOptionEntity,bOption);
optionList.add(bOptionEntity);
}
if (StringUtil.isNotEmpty(cOption)) {
XueBaOptionEntity cOptionEntity = new XueBaOptionEntity();
cOptionEntity = DealOption(cOptionEntity,cOption);
optionList.add(cOptionEntity);
}
if (StringUtil.isNotEmpty(dOption)) {
XueBaOptionEntity dOptionEntity = new XueBaOptionEntity();
dOptionEntity = DealOption(dOptionEntity,dOption);
optionList.add(dOptionEntity);
}
if (StringUtil.isNotEmpty(eOption)) {
XueBaOptionEntity eOptionEntity = new XueBaOptionEntity();
eOptionEntity = DealOption(eOptionEntity,eOption);
optionList.add(eOptionEntity);
}
question.setXueBaOptionList(optionList);
questionList.add(question);
}
}catch (Exception e) {
e.printStackTrace();
logger.info("题库第" + num + "行解析异常");
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
return questionList;
}
private XueBaOptionEntity DealOption(XueBaOptionEntity optionEntity,
String option) {
int start = option.indexOf("、");
// System.out.println("option:"+option+" start:"+start);
String optionTitle = option.substring(0, start);
String optionContent = option.substring(start+1);
optionEntity.setTitle(optionTitle);
optionEntity.setContent(optionContent);
return optionEntity;
}
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
// 判断当前Cell的Type
switch (cell.getCellType()) {
// 如果当前Cell的Type为NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
case HSSFCell.CELL_TYPE_FORMULA: {
// 判断当前的cell是否为Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是Date类型则,转化为Data格式
// 方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
// cellvalue = cell.getDateCellValue().toLocaleString();
// 方法2:这样子的data格式是不带带时分秒的:2011-10-12
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellvalue = sdf.format(date);
}
// 如果是纯数字
else {
// 取得当前Cell的数值
cellvalue = String.valueOf(cell.getNumericCellValue());
}
break;
}
// 如果当前Cell的Type为STRIN
case HSSFCell.CELL_TYPE_STRING:
// 取得当前的Cell字符串
cellvalue = cell.getRichStringCellValue().getString();
break;
// 默认的Cell值
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}
Excel导入功能的更多相关文章
- 解析大型.NET ERP系统 设计通用Microsoft Excel导入功能
做企业管理软件很难避免与Microsoft Excel打交道,常常是软件做好了,客户要求说再做一个Excel导入功能.导入Excel数据的功能的难度不大,从Excel列数据栏位的取值,验证值,再导入到 ...
- java利用jxl实现Excel导入功能
本次项目实践基于Spring+SpringMvc+MyBatis框架,简单实现了Excel模板导出.和Excel批量导入的功能.实现过程如下:. 1.maven导入所需jar包 <depende ...
- 后端Springboot前端VUE实现Excel导入功能
功能描述:做的是物联网的项目,Excel导入实现的功能是将Excel中的数据批量的导入AEP系统,再导入我们系统中.目前已经完成该功能,前端还会添加进度条优化.对于导入导出功能,推荐这个Git:htt ...
- Java中Excel导入功能实现、excel导入公共方法_POI -
这是一个思路希望能帮助到大家:如果大家有更好的解决方法希望分享出来 公司导入是这样做的 每个到导入的地方 @Override public List<DataImportMessage> ...
- Excel导入功能(Ajaxfileupload)
前言: 前端采用Easyui+Ajaxfileupload实现 后端采用springmvc框架,其中把解析xml封装成了一个jar包,直接调用即可 准备: 前端需要导入(easyui导入js省略,自行 ...
- php Excel 导入功能
下载excel类地址 https://pan.baidu.com/s/19MqAHUn4RyZ5HEAChyC0jg 密码:mn58 本人用的thinkcmf框架 把类文件放在框架的类文件里面,下面 ...
- Java Controller下兼容xls和xlsx且可识别合并单元格的excel导入功能
1.工具类,读取单元格数据的时候,如果当前单元格是合并单元格,会自动读取合并单元格的值 package com.shjh.core.util; import java.io.IOException; ...
- excel 导入功能
一:示例代码 //InputStream fis = new FileInputStream(tomcaturl+this.awardTask.getFileRoute());//可以通过上述方式获得 ...
- React + Antd开发模式下的Excel导入功能
具体js如下,配合的是antd里面的upload组件,使用的是xlsx插件 npm : npm install xlsx 插件链接: https://github.com/SheetJS/sheet ...
随机推荐
- mac 功能修改。。。。
个人表示 Mac 下的 Spotlight 搜索功能确实是个鸡肋,安装 QuickSilver 才是王道!所以我个人就把 Spotlight 关闭掉了.方法很简单,还是要用到 “终端” 工具. 在 “ ...
- mysql中使用count()统计的特殊之处
如果你的需要是统计总行数时,为什么要使用count(*),而避免使用指定具体的列名? count()函数里面的参数是列名的的时候,那么会计算有值项的次数.也就是,该列没有值的项并不会进入计算范围.这样 ...
- Android XML文件布局各个属性详解
第一常用类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android: ...
- 大文件读取方法(C#)
之前都是用StreamReader.ReadLine方法逐行读取文件,自从.NET4有了File.ReadLines这一利器,就再也不用为大文件发愁了. File.ReadLines在整个文件读取到内 ...
- css笔记01:CSS例子
body { margin:0; padding:0; background:#000 url('images/backgrounds/star.png') no-repeat fixed; font ...
- 大四找实习(web前端),加油
大四很奇妙,课程变少了,事情却繁杂了. 大三暑假去学驾照,在很多人看来太迟了(毕竟身边很多人跑去实习了),包括我自己.学驾照特别费时间,尤其是对即将大四,希望用实习充实自己的我来说.考虑再三,终于决定 ...
- oracle10g 和oracle11g同时安装时PL/SQL连不上解决方案
oracle10g 和oracle11g同时安装的时候,PL/SQL连不上解决办法:找到两者的配置文件改成一致 oracle10g服务端和oracle11g客户端同时安装的时候,PL/SQL连不上解决 ...
- 一些网摘的hpc材料
source from: https://computing.llnl.gov Factors determines a large-scale program's performance 4 ...
- [改善Java代码]数组的真实类型必须是泛型类型的子类型
List接口的toArray方法可以把一个结合转化为数组,但是使用不方便,toArray()方法返回的是一个Object数组,所以需要自行转变. toArray(T[] a)虽然返回的是T类型的数组, ...
- hdu 3093 动态规划
思路:直接引用论文的话. 暂时先不考虑“使剩下的物品都放不下”的条件,那就是求 0-1 背包的所有可行方案. 用 Fi[j]表示前 i 件物品中选若干件总体积为 j 的方案数,初始为 F0[0]=1, ...