Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加新的注解ExcelIgnore, 来指定不想导出的字段 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
*/target
.DS_Store
.gitattributes
target/*
6 changes: 6 additions & 0 deletions src/main/java/com/xuxueli/poi/excel/ExcelExportUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xuxueli.poi.excel;

import com.xuxueli.poi.excel.annotation.ExcelField;
import com.xuxueli.poi.excel.annotation.ExcelIgnore;
import com.xuxueli.poi.excel.annotation.ExcelSheet;
import com.xuxueli.poi.excel.util.FieldReflectionUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
Expand Down Expand Up @@ -91,6 +92,11 @@ private static void makeSheet(Workbook workbook, List<?> dataList){
if (Modifier.isStatic(field.getModifiers())) {
continue;
}

if (field.getAnnotation(ExcelIgnore.class) != null){
continue;
}

fields.add(field);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/xuxueli/poi/excel/ExcelImportUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.xuxueli.poi.excel;

import com.xuxueli.poi.excel.annotation.ExcelIgnore;
import com.xuxueli.poi.excel.annotation.ExcelSheet;
import com.xuxueli.poi.excel.util.FieldReflectionUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
Expand Down Expand Up @@ -47,6 +48,11 @@ public static List<Object> importExcel(Class<?> sheetClass, Workbook workbook) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}

if (field.getAnnotation(ExcelIgnore.class) != null){
continue;
}

fields.add(field);
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/xuxueli/poi/excel/annotation/ExcelIgnore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xuxueli.poi.excel.annotation;

import java.lang.annotation.*;

/**
* @Author: kba977
* @Date: 2017/12/21 下午6:08
*/

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ExcelIgnore {
}
2 changes: 1 addition & 1 deletion src/test/java/com/xuxueli/poi/excel/test/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void main(String[] args) {
ShopDTO shop = new ShopDTO(true, "商户"+i, (short) i, 1000+i, 10000+i, (float) (1000+i), (double) (10000+i), new Date());
shopDTOList.add(shop);
}
String filePath = "/Users/xuxueli/Downloads/demo-sheet.xls";
String filePath = "/Users/kba977/Downloads/demo-sheet.xls";

/**
* Excel导出:Object 转换为 Excel
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/xuxueli/poi/excel/test/model/ShopDTO.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xuxueli.poi.excel.test.model;

import com.xuxueli.poi.excel.annotation.ExcelField;
import com.xuxueli.poi.excel.annotation.ExcelIgnore;
import com.xuxueli.poi.excel.annotation.ExcelSheet;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
Expand Down Expand Up @@ -28,12 +29,14 @@ public class ShopDTO {
private int shopId;

@ExcelField(name = "浏览人数")
@ExcelIgnore
private long visitNum;

@ExcelField(name = "当月营业额")
@ExcelIgnore
private float turnover;

@ExcelField(name = "历史营业额")
@ExcelIgnore
private double totalTurnover;

@ExcelField(name = "开店时间", dateformat = "yyyy-MM-dd HH:mm:ss")
Expand Down