Commit 10007c56 by lanmw

增加 ExcelFileCtrl

parent bb9badd9
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
...@@ -113,16 +114,6 @@ ...@@ -113,16 +114,6 @@
<artifactId>spring-boot-starter-amqp</artifactId> <artifactId>spring-boot-starter-amqp</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId> <groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId> <artifactId>antlr4-runtime</artifactId>
<version>4.7</version> <version>4.7</version>
...@@ -133,11 +124,6 @@ ...@@ -133,11 +124,6 @@
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<dependency> <dependency>
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
...@@ -151,14 +137,23 @@ ...@@ -151,14 +137,23 @@
<version>5.1.6</version> <version>5.1.6</version>
</dependency> </dependency>
<!-- 引入easypoi -->
<dependency> <dependency>
<groupId>com.kakawait</groupId> <groupId>cn.afterturn</groupId>
<artifactId>cas-security-spring-boot-starter</artifactId> <artifactId>easypoi-base</artifactId>
<version>1.0.0-beta-1</version> <version>3.0.3</version>
</dependency> </dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.0.3</version>
</dependency>
<!-- 引入easypoi -->
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
......
package com.keymobile.tagmanager.api;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.keymobile.tagmanager.model.Tag;
import com.keymobile.tagmanager.service.TagFileService;
import com.keymobile.tagmanager.util.UserInfoUtils;
import io.swagger.annotations.Api;
@Api(value = "文件", tags = "文件")
@RestController
public class TagFileCtrl {
@Autowired
private TagFileService tagExportService;
@GetMapping("/exportExcel")
public int export(HttpServletResponse response) {
String userName = UserInfoUtils.getUserName();
return tagExportService.exportExcel(userName, response);
}
@PostMapping("/importExcel")
public int importExcel(MultipartFile file) throws Exception {
String userName = UserInfoUtils.getUserName();
return tagExportService.importExcel(userName, file, Tag.class);
}
}
...@@ -8,6 +8,10 @@ import org.springframework.data.mongodb.core.mapping.Document; ...@@ -8,6 +8,10 @@ import org.springframework.data.mongodb.core.mapping.Document;
import com.keymobile.tagmanager.util.Constants; import com.keymobile.tagmanager.util.Constants;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
@ExcelTarget("20")
@Document(collection="Tag") @Document(collection="Tag")
public class Tag implements Serializable{ public class Tag implements Serializable{
...@@ -18,15 +22,22 @@ public class Tag implements Serializable{ ...@@ -18,15 +22,22 @@ public class Tag implements Serializable{
@Id @Id
private String id; private String id;
@Excel(name = "名称", orderNum = "0", width=30)
private String name; private String name;
@Excel(name = "英文名", orderNum = "1", width=30)
private String nameEn; private String nameEn;
@Excel(name = "路径", orderNum = "2", width=30)
private String path; private String path;
@Excel(name = "描述", orderNum = "3", width=30)
private String desc; private String desc;
private String imgId; private String imgId;
@Excel(name = "排序权重", orderNum = "4", width=30)
private Integer order; private Integer order;
private Integer level; private Integer level;
@Excel(name = "标签类型", replace = { "系统标签_0", "自定义标签_1" }, orderNum = "5", width=30)
private String tagType; //0, 系统标签, 1 ,自定义标签 private String tagType; //0, 系统标签, 1 ,自定义标签
private String creator; private String creator;
private String createDate;
private String isOpen = Constants.TAG_CLOSE_STATUS; //0 不公开, 1, 公开 private String isOpen = Constants.TAG_CLOSE_STATUS; //0 不公开, 1, 公开
private Integer domain; private Integer domain;
private String dimensionType = Constants.TAG_DIMENSION_FALSE;//0 非维度标签, 1 维度标签 private String dimensionType = Constants.TAG_DIMENSION_FALSE;//0 非维度标签, 1 维度标签
...@@ -50,6 +61,14 @@ public class Tag implements Serializable{ ...@@ -50,6 +61,14 @@ public class Tag implements Serializable{
this.imgId = imgId; this.imgId = imgId;
} }
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getCreateDate() {
return createDate;
}
public Tag(String parentPath, String name, String id) { public Tag(String parentPath, String name, String id) {
this(parentPath, name, "", ""); this(parentPath, name, "", "");
this.id = id; this.id = id;
...@@ -159,4 +178,12 @@ public class Tag implements Serializable{ ...@@ -159,4 +178,12 @@ public class Tag implements Serializable{
return dimensionType; return dimensionType;
} }
@Override
public String toString() {
return "Tag [id=" + id + ", name=" + name + ", nameEn=" + nameEn + ", path=" + path + ", desc=" + desc
+ ", imgId=" + imgId + ", order=" + order + ", level=" + level + ", tagType=" + tagType + ", creator="
+ creator + ", createDate=" + createDate + ", isOpen=" + isOpen + ", domain=" + domain
+ ", dimensionType=" + dimensionType + "]";
}
} }
package com.keymobile.tagmanager.service;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.keymobile.tagmanager.model.Tag;
import com.keymobile.tagmanager.util.Constants;
import com.keymobile.tagmanager.util.DateUtils;
import com.keymobile.tagmanager.util.ExcelUtils;
@Service
public class TagFileService {
@Autowired
private MongoOperations mongoOperations;
public int exportExcel(String userName, HttpServletResponse response) {
List<Tag> toExportTags = mongoOperations.find(Query.query(Criteria.where("creator").is(userName)
.and("dimensionType").is(Constants.TAG_DIMENSION_TRUE)), Tag.class);
// 导出操作
ExcelUtils.exportExcel(toExportTags, null , "sheet1", Tag.class, "标签.xlsx", response);
return toExportTags.size();
}
public int importExcel(String userName, MultipartFile file, Class<Tag> class1) {
List<Tag> tags = ExcelUtils.importExcel(file, 0, 1, Tag.class);
int importCount = 0;
for (Tag t :tags) {
if (checkIsValid(t)) {
t.setLevel(t.getPath().split(",").length);
t.setCreator(userName);
t.setDimensionType(Constants.TAG_DIMENSION_TRUE);
t.setCreateDate(DateUtils.formatDate(new Date(), "yyyy-mm-dd"));
mongoOperations.save(t);
importCount ++;
}
}
return importCount;
}
private boolean checkIsValid(Tag t) {
t = mongoOperations.findOne(Query.query(Criteria.where("path")
.is(t.getPath()).and("dimensionType").is(Constants.TAG_DIMENSION_TRUE)), Tag.class);
return t == null;
}
}
package com.keymobile.tagmanager.util;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;
import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
public class ExcelExportStylerCustomImpl extends AbstractExcelExportStyler implements IExcelExportStyler {
public ExcelExportStylerCustomImpl(Workbook workbook) {
super.createStyles(workbook);
}
@Override
public CellStyle getHeaderStyle(short headerColor) {
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
titleStyle.setWrapText(true);
return titleStyle;
}
@Override
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(STRING_FORMAT);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setDataFormat(STRING_FORMAT);
if (isWarp) {
style.setWrapText(true);
}
return style;
}
@Override
public CellStyle getTitleStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
Font headFont = workbook.createFont();
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headFont.setFontName("宋体");
headFont.setFontHeightInPoints((short) 11);
titleStyle.setFont(headFont);
titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return titleStyle;
}
}
package com.keymobile.tagmanager.util;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
public class ExcelUtils {
private ExcelUtils () {};
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
boolean isCreateHeader, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
exportParams.setStyle(ExcelExportStylerCustomImpl.class);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
defaultExport(list, fileName, response);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null)
;
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
// throw new NormalException(e.getMessage());
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);
if (workbook != null)
;
downLoadExcel(fileName, response, workbook);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
// throw new NormalException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
// throw new NormalException(e.getMessage());
}
return list;
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
Class<T> pojoClass) {
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (NoSuchElementException e) {
// throw new NormalException("excel文件不能为空");
} catch (Exception e) {
// throw new NormalException(e.getMessage());
System.out.println(e.getMessage());
}
return list;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment