easyui+springboot +poi
pom.xml里面引入poi
前端 发送get请求:
$uploadQA.on(“click”, “#btn-downloadDD”, function () {
window.location.href = Util.constants.DOCEDIT_MANAGE_IPO + “地址”;
});
后代control层代码
/**
* 单独的问答知识导入模版下载
*
* @param request
* @param response
* @return
*/
@Override
public Response exportAloneQAKnwlgExcel(HttpServletRequest request, HttpServletResponse response) {
Response aloneQAKnwlgResponse = new Response();
//表头名
String[] title = {"知识标题(必填)", "知识存储路径(必填)","自定义标签", "问题", "扩展问", "答案"};
//sheet名
String sheetName = "独立知识问答模板";
//下载的文件名
String fileName = "独立知识问答模板.xlsx";
//创建XSSFWorkbook
XSSFWorkbook workbook = getXSSFWorkbook(sheetName, title);
OutputStream os = null;
try {
setResponseHeader(response, fileName);
os = response.getOutputStream();
workbook.write(os);
os.flush();
} catch (IOException e) {
logger.error("下载模板失败!");
aloneQAKnwlgResponse.setRspcode(WebUtil.FAIL);
aloneQAKnwlgResponse.setRspdesc("下载模板失败!");
return aloneQAKnwlgResponse;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
logger.error("关闭异常!");
}
}
}
aloneQAKnwlgResponse.setRspcode(WebUtil.SUCCESS);
aloneQAKnwlgResponse.setRspdesc("下载模板成功!");
return aloneQAKnwlgResponse;
}
/**
* 单独的问答知识导入模版下载
* 设置Excel的格式
*
* @param
sheetName
* @param title
* @return
*/
public XSSFWorkbook getXSSFWorkbook(String sheetName, String[] title) {
//创建workbook 2007版本
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个sheet
XSSFSheet sheet = workbook.createSheet(sheetName);
//创建行
XSSFRow row = sheet.createRow(0);
//创建格式
XSSFCellStyle style = workbook.createCellStyle();
//设置边框:
style.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
//设置格式 居中
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
//设置单元格内容自动换行 ===强制换行可以使用\n
style.setWrapText(true);
//设置背景色
style.setFillForegroundColor(new XSSFColor(new Color(204, 204, 204)));// 以给单元格着色
style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); //设置单元格填充样式 SOLID_FOREGROUND纯色使用前景颜色填
XSSFFont font = workbook.createFont();
//设置字体
font.setFontName("黑体");
font.setFontHeightInPoints((short) 12);
// 把字体应用到当前的样式
style.setFont(font);
XSSFCell cell = null;
//设置表头
for (int i = 0; i < title.length; i++) {
//设置列的宽度
sheet.setColumnWidth(i, 5000);
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
return workbook;
}
/**
* 单独的问答知识导入模版下载
* 发送响应流
*
* @param response
* @param fileName
*/
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
logger.error("不支持导出!");
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
}
/**
* 创建头部公共样式与赋值
* @param sheet
* @param title
* @param style
* @param whiteStyle/
*/
private void createHeadRowStyle(XSSFSheet sheet, String[] title, XSSFCellStyle style, XSSFCellStyle whiteStyle,String sheetName) {
//公共属性样式表格创建- ***先创建单元格,在给单元格赋值。这样样式不容易出问题***
//需要title.length行表格
for (int i = 0; i < title.length; i++) {
XSSFRow row = sheet.createRow(i);
//合并单元格 四个参数-开始行,结束行。开始列,结束列
sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 2));
//设置列的宽度
if(i<3) {
sheet.setColumnWidth(i, 5000);
}else if(i==3){
sheet.setColumnWidth(3, 10000);
}
// 需要4列
for (int j = 0; j < 4; j++) {
if(j<3) {
row.createCell(j).setCellStyle(style);
}else{
row.createCell(j).setCellStyle(whiteStyle);
}
}
}
//公共属性样式赋值
for(int i=0;i
导入
思想:前台代码通过ajax发送文件流到后端解析
package com.unicom.kc.manage.doc.service.impl;
import com.unicom.kc.common.sso.domain.UserInfo;
import com.unicom.kc.common.sso.util.TokenUtils;
import com.unicom.kc.common.sso.vo.Response;
import com.unicom.kc.manage.dist.domain.TKcDistrictConfig;
import com.unicom.kc.manage.doc.common.sequence.SequenceUtils;
import com.unicom.kc.manage.doc.dao.*;
import com.unicom.kc.manage.doc.domain.*;
import com.unicom.kc.manage.doc.service.*;
import com.unicom.kc.manage.doc.service.IKcTagRemoteSV;
import com.unicom.kc.manage.doc.service.IKcUploadQAknwlgSV;
import com.unicom.kc.manage.doc.util.Constants;
import com.unicom.kc.manage.doc.util.EsConstants;
import com.unicom.kc.manage.doc.util.StringUtil;
import com.unicom.kc.manage.doc.util.WebUtil;
import com.unicom.kc.manage.doc.web.component.DocIndexComponent;
import com.unicom.kc.manage.doc.web.component.KnwlgDocIndexCompoonent;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class KcUploadQAknwlgSVImpl implements IKcUploadQAknwlgSV {
private static Logger logger = LoggerFactory.getLogger(KcUploadQAknwlgSVImpl.class);
@Autowired
private IKcGetRemoteSV iKcGetRemoteSV; //知识地区//知识渠道
@Autowired
private TKcDocEditMapper tKcDocEditDAO;//知识
@Autowired
private TKcCatalogDocRelMapper tKcCatalogDocRelDAO;//知识路径
@Autowired
private TKcDocGroupsMapper KcDocGroupsDAO;//分组
@Autowired
private TKcDocKeysMapper KcDocKeysDAO;//原子DAO
@Autowired
private SequenceUtils sequenceUtils;
@Autowired
private TKcDocRelationMapper tKcDocRelationMapper;//知识关系表
@Autowired
private TKcDocEditPusMapper KcDocEditPusDAO;//发布表
@Autowired
private TKcRelateEditMapper tKcRelateEditMapper;//自定义标签表
@Autowired
private IKcTagRemoteSV iKcTagRemoteSV;
@Autowired
private IKmKnowledgeCoreSV iKmKnowledgeCoreSV;
@Autowired
private IKcDocIndexSV iKcDocIndexSV;
@Autowired
private IKcGetTmpltRemoteSV iKcGetTmpltRemoteSV;
@Autowired
private DocIndexComponent docIndexComponent;
@Autowired
private IKcDocEditPusSV kcDocEditPusSV;
@Autowired
private KnwlgDocIndexCompoonent knwlgDocIndexCompoonent;
/**
* 批量导入问答知识
*
* @param QAknwlgFile
* @return
*/
@Override
public Response uploadQAknwlgFile(MultipartFile QAknwlgFile, UserInfo userInfo) throws Exception {
Response response = new Response();
Set qaKnwlgNm = new HashSet();
if (QAknwlgFile.isEmpty()) {
response.setRspdesc("问答模板文件为空");
response.setRspcode(WebUtil.EXCEPTION);
return response;
}
// HSSFWorkbook workbook = new HSSFWorkbook(QAknwlgFile.getInputStream()); // 2003版本
XSSFWorkbook workbook = new XSSFWorkbook(QAknwlgFile.getInputStream()); // 2007版本
Sheet sheet = workbook.getSheetAt(0);
sheet.getRow(1).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
String flag = sheet.getRow(1).getCell(0).getStringCellValue();
String sheetName = sheet.getSheetName();//页签名
//列
int totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
String SplitId = sequenceUtils.getSequence("t_kc_doc_edit").toString();
int number = 0;
if ("知识存储路径(必填)".equals(flag)) {
int lastRow = sheet.getLastRowNum();
for (int m = 3; m < totalCells; m++) {
if(m>3){
response.setRspdesc("问答模板只支持单列导入,第一列导入成功");
response.setRspcode(WebUtil.FAIL);
return response;
}
Row row_l = sheet.getRow(1);
if (row_l.getCell(m) == null || StringUtil.isEmpty(row_l.getCell(m).toString())) {
break;
}
String knwlgstr = "";//知识标题
String KnwlgType = null;//知识类别(必填)text
String anthRegnNm = null;//知识发布范围(必填)text
String ChnlNm = null;//知识发布渠道(必填)text
Long CatlId = 0L;//知识存储路径(必填)
String KnwlgRegnType = "";//知识类别(必填)
String AuthRegnNo = "";//知识发布范围(必填)
Long TmpltId = 0L;//知识模板(必填)
String ChnlCodes = "";//知识发布渠道(必填)
Date KnwlgEffTime = null;//知识上线时间(必填)
Date KnwlgInvldTime = null;//知识下线时间
Date BusiEffTime = null;//业务上线时间
Date BusiInvldTime = null;//业务下线时间
List urdfTabsId = new ArrayList<>(); // 自定义页签id
for (int i = 0; i <= 10; i++) {
Row row = sheet.getRow(i);
if (row == null) {
continue;
}
row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
String name = row.getCell(0).getStringCellValue();
if ("".equals(name)) {
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
name = row.getCell(1).getStringCellValue();
}
if ("知识标题(必填)".equals(name)) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
knwlgstr = row.getCell(m).getStringCellValue();
if ("".equals(knwlgstr)) {
throw new Exception("导入失败,知识标题(必填)");
}
} else if ("知识存储路径(必填)".equals(name)) {
//根据知识路径获取ID
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String catlNm = row.getCell(m).getStringCellValue();
if ("".equals(catlNm)) {
throw new Exception("导入失败,知识存储路径(必填)");
}
CatlId = Long.valueOf(iKcGetRemoteSV.getCatlIdByCatlNmJob(catlNm));
} else if ("知识类别(必填)".equals(name)) {
KnwlgType = row.getCell(m).getStringCellValue();
if ("".equals(KnwlgType)) {
throw new Exception("导入失败,知识类别(必填)");
}
List list = iKcGetRemoteSV.getCodeValueByTypeIdAndNameJob("NGKM.KNWLG.REGN.TYPE", KnwlgType);
if (list.size() == 0 || list.get(0).getDataValue() == null) {
throw new Exception("知识类别查询失败");
}
KnwlgRegnType = list.get(0).getDataValue();
/* String StaffType = userInfo.getStaffType();
int StaffType1 = Integer.parseInt(StaffType)-1;
if(!list.get(0).getDataValue().equals(String.valueOf(StaffType1))){
throw new Exception("此用户没有权限导入知识类别为:"+KnwlgType+"的知识");
}
*/
} else if ("知识发布范围(必填)".equals(name)) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
if ("".equals(str)) {
throw new Exception("导入失败,知识发布范围(必填)");
}
anthRegnNm=str;
String[] strArr = str.split(",");
String AuthRegnNos = "";
for (int n = 0; n < strArr.length; n++) {
List list = iKcGetRemoteSV.getByRegnNmJob(strArr[n]);
if (list.size() == 0 || list.get(0).getRegnId() == null) {
throw new Exception("第" + (m + 1) + "列,知识发布范围:" + str + "不存在,请您确认是否正确");
}
AuthRegnNos += list.get(0).getRegnId() + ",";
}
AuthRegnNo = AuthRegnNos.substring(0, AuthRegnNos.length() - 1);
} else if ("知识模板(必填)".equals(name)) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
TKcTmpltInfo tKmTmpltInfo = iKcGetTmpltRemoteSV.selectTmpltByTmpltNmJob("问答模板");
if (tKmTmpltInfo == null) {
throw new Exception("第" + (m + 1) + "列,知识模板:" + str + "不存在,请您确认是否正确");
}
TmpltId = tKmTmpltInfo.getTmpltId();
} else if ("知识发布渠道(必填)".equals(name)) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
if ("".equals(str)) {
throw new Exception("导入失败,知识渠道(必填)");
}
ChnlNm=str;
String[] strArr = str.split(",");
String ChnlCode = "";
for (int n = 0; n < strArr.length; n++) {
List list = iKcGetRemoteSV.getCodeValueByTypeIdAndNameJob("NGKM.TEMPLET.CHNL", strArr[n]);
if (list.size() == 0 || list.get(0).getDataValue() == null) {
throw new Exception("第" + (m + 1) + "列,知识渠道:" + strArr[n] + "不存在,请您确认是否正确");
}
ChnlCode += list.get(0).getDataValue() + ",";
}
ChnlCodes = ChnlCode.substring(0, ChnlCode.length() - 1);
} else if ("知识上线时间(必填)".equals(name)) {
try {
KnwlgEffTime = row.getCell(m).getDateCellValue();
} catch (Exception e) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
KnwlgEffTime = format.parse(str);
}
} else if ("知识下线时间".equals(name)) {
try {
KnwlgInvldTime = row.getCell(m).getDateCellValue();
} catch (Exception e) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
KnwlgInvldTime = format.parse(str);
}
} else if ("业务上线时间".equals(name)) {
try {
BusiEffTime = row.getCell(m).getDateCellValue();
} catch (Exception e) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
BusiEffTime = format.parse(str);
}
//临时注掉
// if (BusiEffTime == null) {
// throw new Exception("导入失败,业务上线时间(必填)");
// }
} else if ("业务下线时间".equals(name)) {
try {
BusiInvldTime = row.getCell(m).getDateCellValue();
} catch (Exception e) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
BusiInvldTime = format.parse(str);
}
} else if ("自定义标签".equals(name)) {
row.getCell(m).setCellType(Cell.CELL_TYPE_STRING);
String str = row.getCell(m).getStringCellValue();
if (StringUtil.isNotEmpty(str)) {
String[] strArr = str.split(",");
for (String s : strArr) {
List tKcCustomtabsInfoList = iKcTagRemoteSV.getTagLibInfosByTagNmJob(s);
if (tKcCustomtabsInfoList == null || tKcCustomtabsInfoList.size() == 0) {
throw new Exception("不存在名称为【" + s + "】的标签");
}
urdfTabsId.add(tKcCustomtabsInfoList.get(0).getUrdfTabsId().toString());
}
}
}
}
TKcTmpltInfo tKmTmpltInfo = iKcGetTmpltRemoteSV.selectTmpltByTmpltNmJob("问答模板");
Long groupId = 0L;
Long knwlgId = 0L;
List tKcRelateEditList = new ArrayList<>();
TKcDocEdit docEdit = null;
TKcCatalogDocRel catalogDocRel = null;
TKcDocGroups docGroups = null;
List keysList = new ArrayList<>();
TKcDocRelation tKcDocRelation = null;
int k=0; //判断问答结构 固定的三条
String strutKnwlgId = "";
//判断是否有关联对应的模板
TKcTmpltInfo tkcTmpltInfo = iKcGetTmpltRemoteSV.selectTmpltByTmpltNmJob(sheetName);
if (tkcTmpltInfo != null && tkcTmpltInfo.getTmpltId() != null) {
//关联结构化知识关联表 docIndexComponent
Map condition = new HashMap<>();
condition.put(EsConstants.DOC_INDEX_FAIELD.KNWLG_NM, knwlgstr);//知识标题
condition.put(EsConstants.DOC_INDEX_FAIELD.REGN_ID, userInfo.getProvnce());//地区
condition.put(EsConstants.DOC_INDEX_FAIELD.KNWLG_TYPE, Constants.NGKM_KNWLG_TYPE.NOKNWLG);//结构化知识
condition.put(EsConstants.DOC_INDEX_FAIELD.KNWLG_STS_CD, "1P,2P");//知识状态
List
导入兼容ie8
HSSFCellStyle cellStyle2 = demoWorkBook.createCellStyle();
HSSFDataFormat format = demoWorkBook.createDataFormat();
cellStyle2.setDataFormat(format.getFormat("@"));
cell.setCellStyle(cellStyle2);
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.6</version>
</dependency>