前端调用封装好的方法,传入后台
//加载表格
function queryReload(columnsVal) {
//将原表格销毁,再重新init()初始化一次即可
$("#bootstrap-table").bootstrapTable('destroy');
var options = {
url: prefix + "/list",
createUrl: prefix + "/add",
updateUrl: prefix + "/edit/{id}",
removeUrl: prefix + "/remove",
exportUrl: prefix + "/export",
importUrl: prefix + "/importData",
importTemplateUrl: prefix + "/importTemplate",
modalName: "教师发展中心工作量汇总",
tableName: "bas_fzzxgzl",
columns: columnsVal,
showSearch: false,
showRefresh: false,
showToggle: false,
showColumns: false,
};
$.table.init(options);
}
添加导入按钮事件
<a class="btn btn-info" onclick="$.table.importRecordExcel()" shiro:hasPermission="ndjs:fzzxgzl:import" >
<i class="fa fa-upload"></i> 导入
</a>
添加导入区域
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
<form enctype="multipart/form-data" class="mt20 mb10">
<div class="col-xs-offset-1">
<input type="file" id="file" name="file"/>
<div class="mt10 pt5">
<!-- <input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在,更新这条数据。"> 是否更新已经存在的用户数据-->
<!-- -->
<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i
class="fa fa-file-excel-o"></i> 下载模板</a>
</div>
<font color="red" class="pull-left mt10">
提示:仅允许导入“xls”或“xlsx”格式文件!
</font>
</div>
</form>
</script>
</html>
在实体变量上添加@Excel注解,默认为导出导入,也可以单独设置仅导入Type.IMPORT
@Excel(name = "用户序号")
private Long id;
@Excel(name = "部门编号", type = Type.IMPORT)
private Long deptId;
@Excel(name = "用户名称")
private String userName;
/** 导出部门多个对象 */
@Excels({
@Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
@Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
})
private SysDept dept;
/** 导出部门单个对象 */
@Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT)
private SysDept dept;
在Controller添加导入方法
/**
* 模板下载
*
* @param
* @return com.ruoyi.common.core.domain.AjaxResult
* @author
*
*/
@RequiresPermissions("ndjs:fzzxgzl:view")
@GetMapping("/importTemplate")
@ResponseBody
public AjaxResult importTemplate() {
ExcelUtil<BasFzzxgzl> util = new ExcelUtil<BasFzzxgzl>(BasFzzxgzl.class);
return util.importTemplateExcel("教师发展中心工作量数据");
}
/**
* 导入
*
* @param file
* @param updateSupport
* @return com.ruoyi.common.core.domain.AjaxResult
* @author
*
*/
@Log(title = "社会实践工作量", businessType = BusinessType.IMPORT)
@RequiresPermissions("ndjs:fzzxgzl:import")
@PostMapping("/importData")
@ResponseBody
public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
ExcelUtil<BasFzzxgzl> util = new ExcelUtil<BasFzzxgzl>(BasFzzxgzl.class);
List<BasFzzxgzl> fzzxgzlList = util.importExcel(file.getInputStream());
return basFzzxgzlService.importFzzxgzl(fzzxgzlList, updateSupport);
}
创建一个实体类用来记录导入时的错误数据
public class BasImportRecord extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** id */
private Long id;
/** 导入记录表名称 */
@Excel(name = "导入记录表名称")
private String importTableName;
/** 导入记录原内容行号*/
private Long importLineNum;
/** 导入批次序号yyyymmddhhmmssfff */
@Excel(name = "导入批次序号yyyymmddhhmmssfff")
private String importBatchNum;
/** 导入的一行记录内容|分隔多列内容 */
@Excel(name = "导入的一行记录内容|分隔多列内容")
private String importContent;
/** 导入状态 0成功 1失败 */
@Excel(name = "导入状态 0成功 1失败")
private String importState;
/** 导入信息(失败原因) */
@Excel(name = "导入信息(失败原因)")
private String importInfo;
}
添加数据库语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.workload.mapper.BasImportRecordMapper">
<resultMap type="BasImportRecord" id="BasImportRecordResult">
<result property="id" column="id" />
<result property="importTableName" column="import_table_name" />
<result property="importBatchNum" column="import_batch_num" />
<result property="importContent" column="import_content" />
<result property="importState" column="import_state" />
<result property="importInfo" column="import_info" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<result property="importLineNum" column="import_line_num"/>
</resultMap>
<sql id="selectBasImportRecordVo">
select id, import_table_name,import_line_num, import_batch_num, import_content, import_state, import_info, create_by, create_time, update_by, update_time from bas_import_record
</sql>
<select id="selectBasImportRecordList" parameterType="BasImportRecord" resultMap="BasImportRecordResult">
<include refid="selectBasImportRecordVo"/>
<where>
<if test="importTableName != null and importTableName != ''"> and import_table_name = #{importTableName}</if>
<if test="importBatchNum != null and importBatchNum != ''"> and import_batch_num = #{importBatchNum}</if>
<if test="importContent != null and importContent != ''"> and import_content = #{importContent}</if>
<if test="importState != null and importState != ''"> and import_state = #{importState}</if>
<if test="importInfo != null and importInfo != ''"> and import_info = #{importInfo}</if>
</where>
</select>
<select id="selectBasImportRecordById" parameterType="Long" resultMap="BasImportRecordResult">
<include refid="selectBasImportRecordVo"/>
where id = #{id}
</select>
<insert id="insertBasImportRecord" parameterType="BasImportRecord">
insert into bas_import_record
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null ">id,</if>
<if test="importTableName != null and importTableName != ''">import_table_name,</if>
<if test="importBatchNum != null and importBatchNum != ''">import_batch_num,</if>
<if test="importContent != null and importContent != ''">import_content,</if>
<if test="importState != null and importState != ''">import_state,</if>
<if test="importInfo != null and importInfo != ''">import_info,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
<if test="createTime != null ">create_time,</if>
<if test="updateBy != null and updateBy != ''">update_by,</if>
<if test="updateTime != null ">update_time,</if>
<if test="remark != null and remark != ''">remark,</if>
<if test="importLineNum != null">import_line_num,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null ">#{id},</if>
<if test="importTableName != null and importTableName != ''">#{importTableName},</if>
<if test="importBatchNum != null and importBatchNum != ''">#{importBatchNum},</if>
<if test="importContent != null and importContent != ''">#{importContent},</if>
<if test="importState != null and importState != ''">#{importState},</if>
<if test="importInfo != null and importInfo != ''">#{importInfo},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
<if test="createTime != null ">#{createTime},</if>
<if test="updateBy != null and updateBy != ''">#{updateBy},</if>
<if test="updateTime != null ">#{updateTime},</if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="importLineNum != null">#{importLineNum},</if>
</trim>
</insert>
<update id="updateBasImportRecord" parameterType="BasImportRecord">
update bas_import_record
<trim prefix="SET" suffixOverrides=",">
<if test="importTableName != null and importTableName != ''">import_table_name = #{importTableName},</if>
<if test="importBatchNum != null and importBatchNum != ''">import_batch_num = #{importBatchNum},</if>
<if test="importContent != null and importContent != ''">import_content = #{importContent},</if>
<if test="importState != null and importState != ''">import_state = #{importState},</if>
<if test="importInfo != null and importInfo != ''">import_info = #{importInfo},</if>
<if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
<if test="createTime != null ">create_time = #{createTime},</if>
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
<if test="updateTime != null ">update_time = #{updateTime},</if>
<if test="remark != null and remark != ''">remark = #{remark},</if>
<if test="importLineNum != null">import_line_num = #{importLineNum},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteBasImportRecordById" parameterType="Long">
delete from bas_import_record where id = #{id}
</delete>
<delete id="deleteBasImportRecordByIds" parameterType="String">
delete from bas_import_record where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<select id="selectByTableNameCount" resultType="java.util.Map" parameterType="BasImportRecord">
SELECT a.* FROM (
SELECT
date_format(max( r.create_time ), '%Y-%m-%d %H:%i:%s') AS createTime,
count( * ) AS count,
r.import_table_name as importTableName,
r.import_batch_num as importBatchNum,
count( IF ( r.import_state = 0, TRUE, NULL ) ) AS succeedCount,
count( IF ( r.import_state = 1, TRUE, NULL ) ) AS failureCount
FROM
bas_import_record r
WHERE
r.create_time IS NOT NULL
AND r.import_table_name = #{importTableName}
AND r.create_by = #{createBy}
GROUP BY
r.import_batch_num) a ORDER BY a.createTime DESC
</select>
<select id="selectByTableNameAndBatchNum" parameterType="String" resultMap="BasImportRecordResult">
<include refid="selectBasImportRecordVo"/>
<where>
import_state = '1'
and import_table_name = #{tableName}
and import_batch_num = #{batchNum}
</where>
</select>
</mapper>
在servlet层添加自定义方法
public AjaxResult importFzzxgzl(List<BasFzzxgzl> basFzzxgzlList, Boolean isUpdateSupport) {
if (StringUtils.isNull(basFzzxgzlList) || basFzzxgzlList.size() == 0) {
return AjaxResult.error("导入教师发展中心工作量数据不能为空!");
}
try {
basFzzxgzlList.get(0).getJsgh();
}catch (Exception ex){
return AjaxResult.error("导入文件与模板不符!");
}
if (basFzzxgzlList.size() > 500) {
return AjaxResult.error("导入教师发展中心工作量不能超过500条!");
}
int successNum = 0;
int failureNum = 0;
String importBatchBum = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
for (int i = 0; i < basFzzxgzlList.size(); i++) {
BasFzzxgzl fzzxgzl = basFzzxgzlList.get(i);
BasFzzxgzl basFzzxgzl = new BasFzzxgzl();
BasImportRecord basImportRecord = new BasImportRecord();
basImportRecord.setImportTableName("bas_fzzxgzl");
basImportRecord.setImportBatchNum(importBatchBum);
StringBuffer content = new StringBuffer();
String jsgh = "", xm = "", xybh = "", xymc = "", directing = "", teacher = "", other = "", remark = "", ssYear = "";
xybh = StringUtils.isBlank(fzzxgzl.getKkyxbh()) ? " " : fzzxgzl.getKkyxbh();
xymc = StringUtils.isBlank(fzzxgzl.getKkyxmc()) ? " " : fzzxgzl.getKkyxmc();
jsgh = StringUtils.isBlank(fzzxgzl.getJsgh()) ? " " : fzzxgzl.getJsgh();
xm = StringUtils.isBlank(fzzxgzl.getXm()) ? " " : fzzxgzl.getXm();
directing = fzzxgzl.getDirectingLoad() == null ? "0" : fzzxgzl.getDirectingLoad().toString();
teacher = fzzxgzl.getTeachingLoad() == null ? "0" : fzzxgzl.getTeachingLoad().toString();
//wait = fzzxgzl.getWaitLoad() == null ? "0" : fzzxgzl.getWaitLoad().toString();
other = fzzxgzl.getOtherLoad() == null ? "0" : fzzxgzl.getOtherLoad().toString();
remark = StringUtils.isBlank(fzzxgzl.getRemark()) ? " " : fzzxgzl.getRemark();
ssYear = StringUtils.isBlank(fzzxgzl.getSsYear()) ? " " : fzzxgzl.getSsYear();
content.append(jsgh).append("|").append(xm).append("|").append(xybh).append("|").append(xymc).append("|").append(directing).append("|").append(teacher).append("|").append(other).append("|").append(ssYear).append("|").append(remark).append("|");
basImportRecord.setImportContent(content.toString());
Map<String, Object> map = checkImportError(fzzxgzl);
JSONArray array = JSONArray.parseArray(map.get("error").toString());
if (array.size() > 0) {
failureNum++;
//存在异常
basImportRecord.setImportLineNum((long) i + 1);
basImportRecord.setImportState("1");
String error = "";
for (int j = 0; j < array.size(); j++) {
error += array.get(j) + ",";
}
//去掉最后一个,
error = error.substring(0, error.length() - 1);
basImportRecord.setImportInfo(error);
} else {
successNum++;
//不存在异常
basImportRecord.setImportLineNum((long) i + 1);
basImportRecord.setImportState("0");
basFzzxgzl.setJsgh(fzzxgzl.getJsgh());
String name = fzzxgzl.getXm() + "(" + fzzxgzl.getJsgh() + ")";
basFzzxgzl.setXm(name);
basFzzxgzl.setKkyxbh(fzzxgzl.getKkyxbh());
basFzzxgzl.setKkyxmc(fzzxgzl.getKkyxmc());
basFzzxgzl.setDirectingLoad(fzzxgzl.getDirectingLoad());
basFzzxgzl.setTeachingLoad(fzzxgzl.getTeachingLoad());
//basFzzxgzl.setWaitLoad(fzzxgzl.getWaitLoad());
basFzzxgzl.setOtherLoad(fzzxgzl.getOtherLoad());
basFzzxgzl.setSsYear(fzzxgzl.getSsYear());
basFzzxgzl.setRemark(fzzxgzl.getRemark());
// 新增
insertBasFzzxgzl(basFzzxgzl);
}
basImportRecord.setCreateTime(DateUtils.getNowDate());
basImportRecord.setUpdateTime(DateUtils.getNowDate());
basImportRecordMapper.insertBasImportRecord(basImportRecord);
}
if (failureNum > 0) {
return AjaxResult.error("导入成功" + successNum + "条数据,导入失败" + failureNum + "条数据", importBatchBum);
} else {
return AjaxResult.success("导入成功" + successNum + "条数据");
}
}
添加自定义的验证导入数据的方法
public Map<String, Object> checkImportError(BasFzzxgzl fzzxgzl) {
Map<String, Object> map = new HashMap<>();
JSONArray jsonAdd = new JSONArray();
if (StringUtils.isBlank(fzzxgzl.getJsgh())) {
jsonAdd.add("教师工号不能为空");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getJsgh().length() > 20) {
jsonAdd.add("教师工号不能超过20个字符");
map.put("error", jsonAdd);
}
}
if (StringUtils.isBlank(fzzxgzl.getXm())) {
jsonAdd.add("教师姓名不能为空");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getXm().length() > 50) {
jsonAdd.add("教师姓名不能超过50个字符");
map.put("error", jsonAdd);
}
}
if (StringUtils.isNotBlank(fzzxgzl.getJsgh()) && StringUtils.isNotBlank(fzzxgzl.getXm())) {
int jsCount = basYjsgzlMapper.selectCountByJsghAndXm(fzzxgzl.getJsgh(), fzzxgzl.getXm());
if (jsCount == 0) {
jsonAdd.add("教师姓名与教师工号不匹配");
map.put("error", jsonAdd);
} else {
int jyCount = basYjsgzlMapper.selectCountByJsghAndYxbh(fzzxgzl.getJsgh(), fzzxgzl.getKkyxbh());
if (jyCount == 0) {
jsonAdd.add("教师与学院不匹配");
map.put("error", jsonAdd);
}
}
}
if (StringUtils.isBlank(fzzxgzl.getKkyxbh())) {
jsonAdd.add("学院编号不能为空");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getKkyxbh().length() > 20) {
jsonAdd.add("学院编号不能超过20个字符");
map.put("error", jsonAdd);
}
}
if (StringUtils.isBlank(fzzxgzl.getKkyxmc())) {
jsonAdd.add("学院名称不能为空");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getKkyxmc().length() > 100) {
jsonAdd.add("学院名称不能超过100个字符");
map.put("error", jsonAdd);
}
}
if (StringUtils.isNotBlank(fzzxgzl.getKkyxbh()) && StringUtils.isNotBlank(fzzxgzl.getKkyxmc())) {
int xyCount = basYjsgzlMapper.selectCountByYxbhAndYxmc(fzzxgzl.getKkyxbh(), fzzxgzl.getKkyxmc());
if (xyCount == 0) {
jsonAdd.add("学院名称与学院编号不匹配");
map.put("error", jsonAdd);
}
}
if (fzzxgzl.getDirectingLoad() != null) {
if (fzzxgzl.getDirectingLoad().toString().length() > 10) {
jsonAdd.add("导师工作量不能超过10个字符");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getDirectingLoad().toString().indexOf(".") > 8) {
jsonAdd.add("导师工作量整数部分不能超过10位");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getDirectingLoad().toString().indexOf(".") == -1) {
if (fzzxgzl.getDirectingLoad().toString().length() > 8) {
jsonAdd.add("导师工作量整数部分不能超过10位");
map.put("error", jsonAdd);
}
}
}
}
if(fzzxgzl.getDirectingLoad().toString().charAt(0)=='-'){
jsonAdd.add("导师工作量不能为负数");
map.put("error",jsonAdd);
}
}
if (fzzxgzl.getTeachingLoad() != null) {
if (fzzxgzl.getTeachingLoad().toString().length() > 10) {
jsonAdd.add("助教工作量不能超过10个字符");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getTeachingLoad().toString().indexOf(".") > 8) {
jsonAdd.add("助教工作量整数部分不能超过10位");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getTeachingLoad().toString().indexOf(".") == -1) {
if (fzzxgzl.getTeachingLoad().toString().length() > 8) {
jsonAdd.add("助教工作量整数部分不能超过10位");
map.put("error", jsonAdd);
}
}
}
}
if(fzzxgzl.getTeachingLoad().toString().charAt(0)=='-'){
jsonAdd.add("助教工作量不能为负数");
map.put("error",jsonAdd);
}
}
/* if (fzzxgzl.getWaitLoad() != null) {
if (fzzxgzl.getWaitLoad().toString().length() > 10) {
jsonAdd.add("此列名称待定不能超过10个字符");
map.put("error", jsonAdd);
} else {
if (!StringUtils.isNumeric(fzzxgzl.getWaitLoad().toString())) {
jsonAdd.add("此列名称待定必须是数字");
map.put("error", jsonAdd);
}
}
}*/
if (fzzxgzl.getOtherLoad() != null) {
if (fzzxgzl.getOtherLoad().toString().length() > 10) {
jsonAdd.add("其他工作量不能超过10个字符");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getOtherLoad().toString().indexOf(".") > 8) {
jsonAdd.add("其他工作量整数部分不能超过10位");
map.put("error", jsonAdd);
} else {
if (fzzxgzl.getOtherLoad().toString().indexOf(".") == -1) {
if (fzzxgzl.getOtherLoad().toString().length() > 8) {
jsonAdd.add("其他工作量整数部分不能超过10位");
map.put("error", jsonAdd);
}
}
}
}
if(fzzxgzl.getOtherLoad().toString().charAt(0)=='-'){
jsonAdd.add("其他工作量不能为负数");
map.put("error",jsonAdd);
}
}
if (!StringUtils.isBlank(fzzxgzl.getRemark())) {
if (fzzxgzl.getRemark().length() > 50) {
jsonAdd.add("备注不能超过50个字符");
map.put("error", jsonAdd);
}
}
if (!StringUtils.isBlank(fzzxgzl.getSsYear())) {
if (fzzxgzl.getSsYear().length() > 5) {
jsonAdd.add("所属时间不能超过5个字符");
map.put("error", jsonAdd);
} else {
if (!StringUtils.isNumeric(fzzxgzl.getSsYear())) {
jsonAdd.add("所属时间必须是整数数字");
map.put("error", jsonAdd);
}
}
} else {
jsonAdd.add("所属时间不能为空");
map.put("error", jsonAdd);
}
map.put("error", jsonAdd);
return map;
}
导出错误数据的方法
public HSSFWorkbook drawExport(List<BasImportRecord> list) {
// 创建excel文件对象
HSSFWorkbook wb = new HSSFWorkbook();
// 创建sheet
Sheet sheet = wb.createSheet("sheet1");
//表头字体
Font headerFont = wb.createFont();
headerFont.setFontName("微软雅黑");
headerFont.setFontHeightInPoints((short) 18);
headerFont.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
//正文字体
Font contextFont = wb.createFont();
contextFont.setFontName("微软雅黑");
contextFont.setFontHeightInPoints((short) 12);
headerFont.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
//表头样式,左右上下居中
CellStyle headerStyle = wb.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
headerStyle.setLocked(true);
headerStyle.setWrapText(false);// 自动换行
headerStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框
headerStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
headerStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
headerStyle.setBorderRight(BorderStyle.MEDIUM);//右边框
// 单元格样式,左右上下居中 边框
CellStyle commonStyle = wb.createCellStyle();
commonStyle.setFont(contextFont);
commonStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
commonStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
commonStyle.setLocked(true);
commonStyle.setWrapText(false);// 自动换行
commonStyle.setBorderBottom(BorderStyle.MEDIUM); //下边框
commonStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
commonStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
commonStyle.setBorderRight(BorderStyle.MEDIUM);//右边框
// 行号
int rowNum = 0;
Row r3 = sheet.createRow(rowNum++);
r3.setHeight((short) 700);
String[] row = {"教师工号", "教师名称", "开课院系编号", "开课院系名称", "导师工作量", "助教工作量", "其他工作量", "所属年份", "备注", "失败原因"};
for (int i = 0; i < row.length; i++) {
Cell tempCell = r3.createCell(i);
tempCell.setCellValue(row[i]);
tempCell.setCellStyle(commonStyle);
}
//解析导入错误信息
for (BasImportRecord bir : list) {
String[] content = bir.getImportContent().split("\\|");
Row tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 500);
// 循环单元格填入数据
for (int j = 0; j < 10; j++) {
Cell tempCell = tempRow.createCell(j);
tempCell.setCellStyle(commonStyle);
String tempValue;
if (j < 9) {
tempValue = StringUtils.isBlank(content[j]) ? "" : content[j];
} else {
tempValue = StringUtils.isBlank(bir.getImportInfo()) ? "" : bir.getImportInfo();
}
tempCell.setCellValue(tempValue);
}
}
for (int o = 0; o < 10; o++) {
sheet.autoSizeColumn((short) o);
}
return wb;
}
在CommonController这个中的importRecordDownload方法中添加
@GetMapping("importRecord/download")
public void importRecordDownload(String fileName,String tableName,String importBatchBum,Boolean delete, HttpServletResponse response, HttpServletRequest request)
{
List<BasImportRecord> list = basImportRecordMapper.selectByTableNameAndBatchNum(tableName,importBatchBum);
HSSFWorkbook wb = new HSSFWorkbook();
if(tableName.equals("bas_fzzxgzl")){
//教师发展中心工作量
wb = basFzzxgzlService.drawExport(list);
}
//响应到客户端
try {
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, fileName+importBatchBum+"批次导入失败记录表.xls"));
OutputStream outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
log.error("下载文件失败", e);
}
}
注意此方法中的tableName.equals(“bas_fzzxgzl”)这个bas_fzzxgzl必须和前端页面中的 tableName: “bas_fzzxgzl”,
相对应