Skip to content

Commit

Permalink
v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Crab2died committed Oct 27, 2017
1 parent 968315d commit 7f0c78a
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 75 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
// 读取数据转换器 Student2ExpelConverter
@ExcelField(title = "是否开除", order = 5, readConverter = Student2ExpelConverter.class)
private boolean expel;
```

## 三. 读取Excel快速实现
Expand Down Expand Up @@ -160,7 +159,7 @@ Student2{id=10000000000004, name='王二', date=Fri Nov 17 00:00:00 CST 2017, cl
@Test
public void testObject2Excel() throws Exception {
String tempPath = "D:\\IdeaSpace\\Excel4J\\src\\test\\resource\\normal_template.xlsx";
String tempPath = "/normal_template.xlsx";
List<Student1> list = new ArrayList<>();
list.add(new Student1("1010001", "盖伦", "六年级三班"));
list.add(new Student1("1010002", "古尔丹", "一年级三班"));
Expand Down Expand Up @@ -223,7 +222,7 @@ Student2{id=10000000000004, name='王二', date=Fri Nov 17 00:00:00 CST 2017, cl
add(new Student1("1010003", "蒙多", "六年级一班"));
}});
ExcelUtils.getInstance().exportObject2Excel("D:\\IdeaSpace\\Excel4J\\src\\test\\resource\\map_template.xlsx",
ExcelUtils.getInstance().exportObject2Excel("/map_template.xlsx",
0, classes, data, Student1.class, false, "D:/C.xlsx");
}
```
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/github/crab2died/ExcelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public static ExcelUtils getInstance() {
/* *) limitLine => 最大读取行数(默认表尾) */
/* *) sheetIndex => Sheet索引(默认0) */

public <T> List<T> readExcel2Objects(String excelPath, Class<T> clazz, int offsetLine, int limitLine, int
sheetIndex) throws Exception {
public <T> List<T> readExcel2Objects(String excelPath, Class<T> clazz, int offsetLine, int limitLine,
int sheetIndex) throws Exception {
Workbook workbook = WorkbookFactory.create(new File(excelPath));
return readExcel2ObjectsHandler(workbook, clazz, offsetLine, limitLine, sheetIndex);
}
Expand Down Expand Up @@ -100,9 +100,8 @@ public <T> List<T> readExcel2Objects(InputStream is, Class<T> clazz)
return readExcel2Objects(is, clazz, 0, Integer.MAX_VALUE, 0);
}

private <T> List<T> readExcel2ObjectsHandler(Workbook workbook, Class<T> clazz,
int offsetLine, int limitLine,
int sheetIndex) throws Exception {
private <T> List<T> readExcel2ObjectsHandler(Workbook workbook, Class<T> clazz, int offsetLine,
int limitLine, int sheetIndex) throws Exception {

Sheet sheet = workbook.getSheetAt(sheetIndex);
Row row = sheet.getRow(offsetLine);
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/github/crab2died/annotation/ExcelField.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,34 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelField {

/*
/**
* 属性的标题名称
* @return 表头名
*/
String title();

/*
/**
* 写数据转换器
*
* @see WriteConvertible
* @return 写入Excel数据转换器
*/
Class<? extends WriteConvertible> writeConverter()
default DefaultConvertible.class;

/*
/**
* 读数据转换器
*
* @see ReadConvertible
* @return 读取Excel数据转换器
*/
Class<? extends ReadConvertible> readConverter()
default DefaultConvertible.class;

/*
/**
* 在excel的顺序
*
* @return 列表顺序
*/
int order() default 9999;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.github.crab2died.converter;

/**
* 默认转换器
* 默认转换器, 实现了{@link WriteConvertible} 与 {@link ReadConvertible}接口
*/
public class DefaultConvertible implements WriteConvertible, ReadConvertible {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@
*/
public interface ReadConvertible {

/**
* 读取Excel列内容转换
*
* @see com.github.crab2died.annotation.ExcelField#readConverter()
* @param object 待转换数据
* @return 转换完成的结果
*/
Object execRead(String object);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@
*/
public interface WriteConvertible {

/**
* 写入Excel列内容转换
*
* @see com.github.crab2died.annotation.ExcelField#writeConverter()
* @param object 待转换数据
* @return 转换完成的结果
*/
Object execWrite(Object object);
}
26 changes: 15 additions & 11 deletions src/main/java/com/github/crab2died/handler/ExcelTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.github.crab2died.handler;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.*;
Expand Down Expand Up @@ -101,21 +102,24 @@ public class ExcelTemplate {
private ExcelTemplate() {
}

public static ExcelTemplate getInstance(String templatePath, int sheetIndex) {
public static ExcelTemplate getInstance(String templatePath, int sheetIndex) throws Exception {
ExcelTemplate template = new ExcelTemplate();
template.sheetIndex = sheetIndex;
try {
template.loadTemplate(templatePath);
} catch (Exception e) {
e.printStackTrace();
}
template.loadTemplate(templatePath);
return template;
}

/*-----------------------------------初始化模板开始-----------------------------------*/

private void loadTemplate(String templatePath) throws Exception {
this.workbook = WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(templatePath));
private void loadTemplate(String templatePath) throws IOException, InvalidFormatException {

try {
// 读取模板文件
this.workbook = WorkbookFactory.create(new File(templatePath));
} catch (IOException | InvalidFormatException e) {
// 读取模板相对文件
this.workbook = WorkbookFactory.create(ExcelTemplate.class.getResourceAsStream(templatePath));
}
this.sheet = this.workbook.getSheetAt(this.sheetIndex);
initModuleConfig();
this.currentRowIndex = this.initRowIndex;
Expand Down Expand Up @@ -336,12 +340,12 @@ private void setCellStyle(Cell cell, String styleKey) {
/**
* 将文件写到相应的路径下
*
* @param filepath 输出文件路径
* @param filePath 输出文件路径
*/
public void write2File(String filepath) {
public void write2File(String filePath) {

try {
try (FileOutputStream fos = new FileOutputStream(filepath)) {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
try {
this.workbook.write(fos);
} catch (IOException e) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/github/crab2died/handler/HandlerConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@
/**
* <p>Excel模板自定义属性,不区分大小写</p>
*/
public class HandlerConstant {
class HandlerConstant {

// 数据插入起始坐标点
static protected final String DATA_INIT_INDEX = "$data_index";
static final String DATA_INIT_INDEX = "$data_index";

// 默认样式
static protected final String DEFAULT_STYLE = "$default_style";
static final String DEFAULT_STYLE = "$default_style";

// 当前标记行样式
static protected final String APPOINT_LINE_STYLE = "$appoint_line_style";
static final String APPOINT_LINE_STYLE = "$appoint_line_style";

// 单数行样式
static protected final String SINGLE_LINE_STYLE = "$single_line_style";
static final String SINGLE_LINE_STYLE = "$single_line_style";

// 双数行样式
static protected final String DOUBLE_LINE_STYLE = "$double_line_style";
static final String DOUBLE_LINE_STYLE = "$double_line_style";

// 序号列坐标点
static protected final String SERIAL_NUMBER = "$serial_number";
static final String SERIAL_NUMBER = "$serial_number";

}
37 changes: 0 additions & 37 deletions src/main/java/com/github/crab2died/utils/MethodType.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/main/java/com/github/crab2died/utils/RegularUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ static public String match(String pattern, String reg) {

String match = null;
try {
List<String> matchs = match(pattern, reg, 0);
if (null != matchs && matchs.size() > 0) {
match = matchs.get(0);
List<String> matches = match(pattern, reg, 0);
if (null != matches && matches.size() > 0) {
match = matches.get(0);
}
} catch (IllegalGroupIndexException e) {
e.printStackTrace();
Expand Down
72 changes: 71 additions & 1 deletion src/main/java/com/github/crab2died/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,32 @@

public class Utils {

/**
* <p>getter与setter方法的枚举</p>
* @author Crab2Died
*/
private enum MethodType {

GET("get"), SET("set");

private String value;

MethodType(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

/**
* <p>根据JAVA对象注解获取Excel表头信息</p>
*
* @param clz 类型
* @return 表头信息
* @throws IllegalAccessException 异常
* @throws InstantiationException 异常
*/
static
public List<ExcelHeader> getHeaderList(Class<?> clz) throws IllegalAccessException,
Expand All @@ -60,6 +81,15 @@ public List<ExcelHeader> getHeaderList(Class<?> clz) throws IllegalAccessExcepti
return headers;
}

/**
* 获取excel列表头
*
* @param titleRow excel行
* @param clz 类型
* @return ExcelHeader集合
* @throws InstantiationException 异常
* @throws IllegalAccessException 异常
*/
static
public Map<Integer, ExcelHeader> getHeaderMap(Row titleRow, Class<?> clz)
throws InstantiationException, IllegalAccessException {
Expand All @@ -78,6 +108,12 @@ public Map<Integer, ExcelHeader> getHeaderMap(Row titleRow, Class<?> clz)
return maps;
}

/**
* 获取单元格内容
*
* @param c 单元格
* @return 单元格内容
*/
static
public String getCellValue(Cell c) {
String o;
Expand Down Expand Up @@ -106,6 +142,13 @@ public String getCellValue(Cell c) {
return o;
}

/**
* 字符串转对象
*
* @param strField 字符串
* @param clazz 待转类型
* @return 转换后数据
*/
static
public Object str2TargetClass(String strField, Class<?> clazz) {
if (null == strField || "".equals(strField))
Expand Down Expand Up @@ -137,6 +180,12 @@ public Object str2TargetClass(String strField, Class<?> clazz) {
return strField;
}

/**
* 科学计数法数据转换
*
* @param bigDecimal 科学计数法
* @return 数据字符串
*/
private static String matchDoneBigDecimal(String bigDecimal) {
// 对科学计数法进行处理
boolean flg = Pattern.matches("^-?\\d+(\\.\\d+)?(E-?\\d+)?$", bigDecimal);
Expand All @@ -147,6 +196,14 @@ private static String matchDoneBigDecimal(String bigDecimal) {
return bigDecimal;
}

/**
* 获取字段的getter或setter方法
*
* @param fieldClass 字段类型
* @param fieldName 字段名
* @param methodType getter或setter
* @return getter或setter方法
*/
private static String getOrSet(Class fieldClass, String fieldName, MethodType methodType) {

if (null == fieldClass || null == fieldName)
Expand Down Expand Up @@ -187,13 +244,26 @@ private static String getOrSet(Class fieldClass, String fieldName, MethodType me
return methodType.getValue() + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
}

/**
* 根据属性名与属性类型获取字段内容
*
* @param bean 对象
* @param fieldName 字段名
* @param fieldClass 字段类型
* @param writeConvertible 写入转换器
* @return 对象指定字段内容
* @throws NoSuchMethodException 异常
* @throws InvocationTargetException 异常
* @throws IllegalAccessException 异常
*/
static
public String getProperty(Object bean, String fieldName, Class fieldClass, WriteConvertible writeConvertible)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

Method method = bean.getClass().getDeclaredMethod(getOrSet(fieldClass, fieldName, MethodType.GET));
Object object = method.invoke(bean);
if (null != writeConvertible && writeConvertible.getClass() != DefaultConvertible.class) {
// 写入转换器
object = writeConvertible.execWrite(object);
}
return object.toString();
Expand Down

0 comments on commit 7f0c78a

Please sign in to comment.