Commit 0e46815e by 钟明宏

增加忽略指定字段的功能

parent e16e9b6c
......@@ -80,6 +80,12 @@ public class DefaultExcelImport {
return getData(sheet);
}
/**
* 工作部对象
*
* @param sheet
* @return
*/
private List<T> getData(Sheet sheet) {
List<T> data = new ArrayList<>();
Iterator<Row> iterable = sheet.rowIterator();
......@@ -91,7 +97,8 @@ public class DefaultExcelImport {
excelRowDataModel = new ExcelRowDataModel<>(modelPropertyDescriptor, excelRowTitleModel);
} else {
T entity = excelRowDataModel.getEntity(iterable.next());
data.add(entity);
if (entity != null)
data.add(entity);
}
}
return data;
......
......@@ -10,6 +10,8 @@ import org.apache.poi.ss.usermodel.Row;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.Set;
/**
* 数据行对象
......@@ -22,19 +24,23 @@ public class ExcelRowDataModel<T> {
private ModelPropertyDescriptor<T> modelPropertyDescriptor;
private ExcelRowTitleModel excelRowTitleModel;
/**
* 记录需要过滤的单元格,一个某个表头的是值为空将会 放弃整个单元格
*/
private Set<String> ifFileNUllAndIgnoreRowSets;
public ExcelRowDataModel(ModelPropertyDescriptor<T> modelPropertyDescriptor, ExcelRowTitleModel excelRowTitleModel) {
this.modelPropertyDescriptor = modelPropertyDescriptor;
this.excelRowTitleModel = excelRowTitleModel;
for (String modeTitle : modelPropertyDescriptor.modelTitle()) {
}
this.ifFileNUllAndIgnoreRowSets = modelPropertyDescriptor.getIfFileNUllAndIgnoreRowSets();
}
public T getEntity(Row row) {
try {
T entity = modelPropertyDescriptor.getInstance();
for (int i = 0; i < row.getLastCellNum(); i++) {
boolean isIgnore = false;
int count = 0;
for (int i = 0; i < row.getLastCellNum() && i < excelRowTitleModel.length(); i++) {
Cell cell = row.getCell(i);
String excelTitle = excelRowTitleModel.getTitle(i);
PropertyDescriptor propertyDescriptor = modelPropertyDescriptor.getPropertyDescriptorByExcelTitle(excelTitle);
......@@ -48,6 +54,12 @@ public class ExcelRowDataModel<T> {
} else {
try {
Object value = ValueSelector.select(cell, field);
if (value == null && ifFileNUllAndIgnoreRowSets.size() > 0 && ifFileNUllAndIgnoreRowSets.contains(field.getName())) {
//直接返回一个空值忽略掉整行
return null;
}
//记录空值的个数
if (value != null) count++;
propertyDescriptor.getWriteMethod().invoke(entity, value);
} catch (Exception e) {
throw new ExcelToolsException(e.getMessage(), row.getRowNum(), i + 1, excelTitle, e);
......@@ -57,7 +69,8 @@ public class ExcelRowDataModel<T> {
}
}
return entity;
//如果全部为空将会返回一个null对象
return count != 0 ? entity : null;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
......
......@@ -4,6 +4,8 @@ import cn.yunmaozj.tools.excel.exception.ExcelToolsException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
......@@ -65,6 +67,12 @@ public class ExcelRowTitleModel {
}
public int length() {
if (titles == null) {
titles = new Cell[row.getLastCellNum()];
for (int i = 0; i < titles.length; i++) {
titles[i] = row.getCell(i);
}
}
return titles.length;
}
}
......@@ -36,6 +36,11 @@ public class ModelPropertyDescriptor<T> {
private Map<String, ExcelFieldRow> excelTitleExcelFieldRowMap = new HashMap<>();
/**
* 如果某个资源为空,就忽略到
*/
private Set<String> ifFileNUllAndIgnoreRowSets;
/**
* 这个导入的时候提供的构造方法,或过滤掉没有的字段
*
* @param model
......@@ -72,6 +77,11 @@ public class ModelPropertyDescriptor<T> {
e.printStackTrace();
}
});
ifFileNUllAndIgnoreRowSets = new HashSet<>();
Sheet sheet = model.getAnnotation(Sheet.class);
if (sheet != null) {
ifFileNUllAndIgnoreRowSets.addAll(Arrays.asList(sheet.ifFileNUllAndIgnoreRow()));
}
}
/**
......@@ -189,4 +199,8 @@ public class ModelPropertyDescriptor<T> {
}
return model.getName();
}
public Set<String> getIfFileNUllAndIgnoreRowSets() {
return Collections.unmodifiableSet(ifFileNUllAndIgnoreRowSets);
}
}
......@@ -13,4 +13,11 @@ import java.lang.annotation.*;
public @interface Sheet {
String value();
/**
* 忽略掉空的行,这里需要填假如为空列的字段名
*
* @return
*/
String[] ifFileNUllAndIgnoreRow() default {};
}
......@@ -40,7 +40,12 @@ public class ValueSelector<T> {
return new BigDecimal(cell.getNumericCellValue()).toString();
}
if (cell.getCellTypeEnum() == FORMULA) {
return new StringBuilder().append(cell.getRichStringCellValue()).toString();
if(cell.getCachedFormulaResultTypeEnum() == STRING){
return new StringBuilder().append(cell.getRichStringCellValue()).toString();
}else if(cell.getCellTypeEnum() == NUMERIC){
return new BigDecimal(cell.getNumericCellValue()).toString();
}
}
return null;
}
......@@ -56,6 +61,9 @@ public class ValueSelector<T> {
return new BigDecimal(cell.getNumericCellValue()).intValue();
}
if (cell.getCellTypeEnum() == FORMULA) {
if(cell.getCachedFormulaResultTypeEnum() == NUMERIC){
return new BigDecimal(cell.getNumericCellValue()).intValue();
}
return Integer.parseInt(cell.getRichStringCellValue().getString());
}
return 0;
......
......@@ -18,7 +18,7 @@ import java.util.Date;
@Setter
@ToString
@ExcelSummaryInformation
@Sheet("作品")
@Sheet(value = "作品", ifFileNUllAndIgnoreRow = {"mobile", "opusType", "resource"})
public class OpusModel {
@ExcelFieldRow(title = "标题")
......@@ -27,13 +27,13 @@ public class OpusModel {
@ExcelFieldRow(title = "内容")
private String content;
@ExcelFieldRow(title = "手机号", require = true)
@ExcelFieldRow(title = "手机号")
private String mobile;
@ExcelFieldRow(title = "资源")
private String resource;
@ExcelFieldRow(title = "作品类型", require = true)
@ExcelFieldRow(title = "作品类型")
private String opusType;
@ExcelFieldRow(title = "话题")
......@@ -45,12 +45,6 @@ public class OpusModel {
@ExcelFieldRow(title = "浏览数量")
private Integer browseNumber;
@ExcelFieldRow(title = "opus weight")
private Double opusWeight;
@ExcelFieldRow(title = "sys weight")
private double sysWeight;
@ExcelFieldRow(title = "发布时间", format = "yyyy-MM-dd HH:mm:ss")
private Date createDate;
......@@ -58,8 +52,8 @@ public class OpusModel {
private String location;
@ExcelFieldRow(title = "经度")
private double lat;
private Double lat;
@ExcelFieldRow(title = "维度")
private double lng;
private Double lng;
}
......@@ -4,6 +4,7 @@ import cn.yunmaozj.tools.excel.model.annotations.ExcelFieldRow;
import cn.yunmaozj.tools.excel.model.annotations.Sheet;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Date;
......@@ -14,7 +15,8 @@ import java.util.Date;
*/
@Getter
@Setter
@Sheet("用户")
@ToString
@Sheet(value = "用户", ifFileNUllAndIgnoreRow = {"mobile", "nickname"})
public class UserModel {
@ExcelFieldRow(title = "昵称")
......@@ -29,7 +31,7 @@ public class UserModel {
@ExcelFieldRow(title = "性别", allowableValues = {"男", "女"})
private String gender;
@ExcelFieldRow(title = "生日",format = "yyyy/MM/dd")
@ExcelFieldRow(title = "生日", format = "yyyy/MM/dd")
private Date birthday;
@ExcelFieldRow(title = "个性签名")
......
......@@ -43,9 +43,6 @@ public class DefaultExcelExportTest {
model.setBrowseNumber(0);
model.setLikeNumber(100);
model.setOpusWeight(10.34324324);
model.setSysWeight(89.42343243243);
model.setLocation("成都市");
model.setLat(1.45454354354);
model.setLng(343.434343243);
......@@ -69,8 +66,6 @@ public class DefaultExcelExportTest {
assertEquals(model.getOpusType(), list.get(0).getOpusType());
assertEquals(model.getTopics(), list.get(0).getTopics());
assertEquals(model.getBrowseNumber(), list.get(0).getBrowseNumber());
assertEquals(model.getSysWeight(), list.get(0).getSysWeight(), 1);
assertEquals(model.getOpusWeight(), list.get(0).getOpusWeight());
assertEquals(model.getLocation(), list.get(0).getLocation());
assertEquals(model.getLat(), list.get(0).getLat(), 1);
assertEquals(model.getLng(), list.get(0).getLng(), 1);
......
......@@ -2,6 +2,7 @@ package com.yunmaozj.excel.opus;
import cn.yunmaozj.tools.excel.core.DefaultExcelExport;
import cn.yunmaozj.tools.excel.core.DefaultExcelImport;
import cn.yunmaozj.tools.excel.utils.StringUtils;
import com.yunmaozj.excel.model.OpusModel;
import com.yunmaozj.excel.model.UserModel;
import org.junit.Assert;
......@@ -75,7 +76,7 @@ public class DefaultExcelImportTest {
@Ignore
@Test
public void demo() throws IOException {
public void dispose_user() throws IOException {
DefaultExcelImport defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-陈大为.xlsx");
List<UserModel> list_1 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-孙嘉䜣.xlsx");
......@@ -86,17 +87,21 @@ public class DefaultExcelImportTest {
List<UserModel> list_4 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-郑瀚.xlsx");
List<UserModel> list_5 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
setRealAvatar(list_1,"/avatar/cdw");
setRealAvatar(list_2,"/avatar/sjx");
setRealAvatar(list_3,"/avatar/tsy");
setRealAvatar(list_4,"/avatar/wq");
setRealAvatar(list_5,"/avatar/zh");
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\用户表-翰林.xlsx");
List<UserModel> list_6 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(UserModel.class));
setRealAvatar(list_1, "/avatar/cdw");
setRealAvatar(list_2, "/avatar/sjx");
setRealAvatar(list_3, "/avatar/tsy");
setRealAvatar(list_4, "/avatar/wq");
setRealAvatar(list_5, "/avatar/zh");
setRealAvatar(list_6, "/avatar/hl");
DefaultExcelExport excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-陈大为", list_1));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-孙嘉䜣", list_2));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-唐诗雨", list_3));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-文强", list_4));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-郑瀚", list_5));
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-翰林", list_6));
excelExport.writeFile("E:\\tmp\\avatar\\运营用户表.xlsx");
//反写到每个单独的文件
......@@ -120,18 +125,81 @@ public class DefaultExcelImportTest {
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-郑瀚", list_5));
excelExport.writeFile("E:\\tmp\\avatar\\用户表-郑瀚.xlsx");
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\avatar\\运营用户表.xlsx");
System.out.println(defaultExcelImport.getSheetSize());
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(UserModel.class, "用户表-翰林", list_5));
excelExport.writeFile("E:\\tmp\\avatar\\用户表-翰林.xlsx");
}
@Ignore
@Test
public void dispose_opus() throws IOException {
DefaultExcelImport defaultExcelImport = new DefaultExcelImport("E:\\tmp\\opus\\作品数据-翰林.xlsx");
List<OpusModel> list_1 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\opus\\作品数据-陈大为.xlsx");
List<OpusModel> list_2 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\opus\\作品数据-孙嘉䜣.xlsx");
List<OpusModel> list_3 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\opus\\作品数据-唐诗雨.xlsx");
List<OpusModel> list_4 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\opus\\作品数据-文强.xlsx");
List<OpusModel> list_5 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
defaultExcelImport = new DefaultExcelImport("E:\\tmp\\opus\\作品数据-郑瀚.xlsx");
List<OpusModel> list_6 = defaultExcelImport.getData(0, new DefaultExcelImport.ImportSheet(OpusModel.class));
//合并表格
DefaultExcelExport excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-陈大为", list_2));
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-孙嘉䜣", list_3));
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-唐诗雨", list_4));
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-文强", list_5));
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-郑瀚", list_6));
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-翰林", list_1));
excelExport.writeFile("E:\\tmp\\opus\\运营作品表.xlsx");
//反写到每个单独的文件
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-陈大为", list_2));
excelExport.writeFile("E:\\tmp\\opus\\新-作品数据-陈大为.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-孙嘉䜣", list_3));
excelExport.writeFile("E:\\tmp\\opus\\新-作品数据-孙嘉䜣.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-唐诗雨", list_4));
excelExport.writeFile("E:\\tmp\\opus\\新-作品数据-唐诗雨.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-文强", list_5));
excelExport.writeFile("E:\\tmp\\opus\\新-作品数据-文强.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-郑瀚", list_6));
excelExport.writeFile("E:\\tmp\\opus\\新-作品数据-郑瀚.xlsx");
excelExport = new DefaultExcelExport();
excelExport.writeData(new DefaultExcelExport.ExportSheet(OpusModel.class, "作品数据-翰林", list_1));
excelExport.writeFile("E:\\tmp\\opus\\新-作品数据-翰林.xlsx");
}
public void setRealAvatar(List<UserModel> list, String prefix) {
for (UserModel userModel : list) {
if(userModel != null && userModel.getAvatar() != null){
if (userModel != null && userModel.getAvatar() != null) {
File file = new File(userModel.getAvatar());
userModel.setAvatar(prefix + "/" + file.getName());
}
}
}
public void addResourcePrefix(List<OpusModel> list) {
for (OpusModel opusModel : list) {
String resource = opusModel.getResource();
String[] resources = resource.split(";");
StringBuilder stringBuilder = new StringBuilder();
}
}
}
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 sign in to comment