|
|
|
@ -4,6 +4,7 @@ import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
|
@ -23,17 +24,21 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
public class ExcelOperation {
|
|
|
|
|
|
|
|
|
|
/** 写入excel
|
|
|
|
|
* @param templateFilePath
|
|
|
|
|
* @param workbook 模板
|
|
|
|
|
* @param excelInitRow 从第excelInitRow行 开始填充数据,( Constant.EXCEL_TEMPLATE_INIT_ROW)
|
|
|
|
|
* @param excelInitCell 从第excelInitCell列 开始填充数据,
|
|
|
|
|
* @param excelEndCell 到第excelEndCell列 结束,
|
|
|
|
|
* @param list list中对象的属性顺序和模板的列顺序一一对应
|
|
|
|
|
* @param excelInitRow //从第excelInitRow行 开始填充数据,( Constant.EXCEL_TEMPLATE_INIT_ROW)
|
|
|
|
|
* @return HSSFWorkbook
|
|
|
|
|
* @param filter list中对象的属性过滤(比如"id")
|
|
|
|
|
* @return 带有数据的XSSFWorkbook
|
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
|
*///从第几行 填充数据
|
|
|
|
|
public static XSSFWorkbook writeExcelTemplate(XSSFWorkbook workbook, int excelInitRow, List list) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException {
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
public static XSSFWorkbook writeExcelTemplate(XSSFWorkbook workbook, int excelInitRow, int excelInitCell, int excelEndCell,List list, String... filter) throws FileNotFoundException, IOException, IllegalArgumentException, IllegalAccessException {
|
|
|
|
|
List<String> filters = new ArrayList<String>();
|
|
|
|
|
filters.addAll(Arrays.asList(filter));
|
|
|
|
|
//读取工作表
|
|
|
|
|
XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
|
|
|
|
|
XSSFRow row;
|
|
|
|
@ -46,15 +51,30 @@ public class ExcelOperation {
|
|
|
|
|
Object obj = list.get(i);
|
|
|
|
|
Field[] fields = obj.getClass().getDeclaredFields();
|
|
|
|
|
lengths = fields.length;
|
|
|
|
|
int num = 0;
|
|
|
|
|
for (int j = 0; j < lengths; j++) {
|
|
|
|
|
fields[j].setAccessible(true);
|
|
|
|
|
if (j - num >= lengths) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (j >= excelEndCell) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
num = getWriteIndex(filters, fields, j - num, num);
|
|
|
|
|
if (num == 1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Field tmpField = fields[j - num];
|
|
|
|
|
tmpField.setAccessible(true);
|
|
|
|
|
cell = row.createCell((short) j,HSSFCellStyle.ALIGN_CENTER);
|
|
|
|
|
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
|
|
Object value = fields[j].get(obj);
|
|
|
|
|
Object value = tmpField.get(obj);
|
|
|
|
|
if (null != value) {
|
|
|
|
|
//int型
|
|
|
|
|
if (fields[j].getType().toString().contains("Integer")) {
|
|
|
|
|
cell.setCellValue(String.valueOf(value));
|
|
|
|
|
if (tmpField.getType().toString().contains("Integer") || tmpField.getType().toString().contains("int")) {
|
|
|
|
|
if (filters.contains(tmpField.getName()))
|
|
|
|
|
cell.setCellValue("-");
|
|
|
|
|
else
|
|
|
|
|
cell.setCellValue(String.valueOf(value));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cell.setCellValue(value.toString());
|
|
|
|
@ -123,14 +143,18 @@ public class ExcelOperation {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 读取excel模板
|
|
|
|
|
* @param workbook excel的XSSFWorkbook
|
|
|
|
|
* @param workbook excel的带数据的XSSFWorkbook
|
|
|
|
|
* @param excelInitRow 读取数据的起始行
|
|
|
|
|
* @param excelInitCell 读取数据的起始列
|
|
|
|
|
* @param classz excel的每行数据导出 到的 类
|
|
|
|
|
* @return
|
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
|
* @throws InstantiationException
|
|
|
|
|
* @param filter excel的每行数据导出 到的 类 的属性的过滤
|
|
|
|
|
* @return 类 的集合
|
|
|
|
|
* @throws InstantiationException
|
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
|
*/
|
|
|
|
|
public static List readExcel4Update(XSSFWorkbook workbook, int excelInitRow, Class<?> classz) throws InstantiationException, IllegalAccessException {
|
|
|
|
|
public static List readExcel4Update(XSSFWorkbook workbook, int excelInitRow, int excelInitCell, Class<?> classz, String... filter) throws InstantiationException, IllegalAccessException {
|
|
|
|
|
List<String> filters = new ArrayList<String>();
|
|
|
|
|
filters.addAll(Arrays.asList(filter));
|
|
|
|
|
List list = null;
|
|
|
|
|
//读取工作表
|
|
|
|
|
XSSFSheet sheet = workbook.getSheetAt(0);//getSheet("Sheet1");
|
|
|
|
@ -150,38 +174,48 @@ public class ExcelOperation {
|
|
|
|
|
for (int i = excelInitRow; i < rowNum; i++) {
|
|
|
|
|
//获得当前行
|
|
|
|
|
row = sheet.getRow(i);
|
|
|
|
|
cell = row.getCell(0);
|
|
|
|
|
if ("-".equals(getValue(cell)) || "".equals(getValue(cell))) {
|
|
|
|
|
cell = row.getCell(excelInitCell);
|
|
|
|
|
if ("-".equals(getValue(cell))) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//实例化
|
|
|
|
|
Object newObj = classz.newInstance();
|
|
|
|
|
Field[] fields = newObj.getClass().getDeclaredFields();
|
|
|
|
|
int fieldSize = fields.length;
|
|
|
|
|
boolean isAddObj = false;
|
|
|
|
|
int num = 0;
|
|
|
|
|
//遍历每列的数据
|
|
|
|
|
for (int j = 0; j < objColl; j++) {
|
|
|
|
|
for (int j = excelInitCell; j < objColl; j++) {
|
|
|
|
|
cell = row.getCell(j);
|
|
|
|
|
if (j + num >= fieldSize) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
num = getIndex(filters, fields, j + num, num);
|
|
|
|
|
if (num == -1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Field tmpField = fields[j + num];
|
|
|
|
|
if (null != cell) {
|
|
|
|
|
fields[j].setAccessible(true);
|
|
|
|
|
tmpField.setAccessible(true);
|
|
|
|
|
String cellString = getValue(cell);
|
|
|
|
|
if (!"".equals(cellString)) {
|
|
|
|
|
isAddObj = true;
|
|
|
|
|
//int型
|
|
|
|
|
if (fields[j].getType().toString().contains("Integer") ||fields[j].getType().toString().contains("int")) {
|
|
|
|
|
if (tmpField.getType().toString().contains("Integer") ||tmpField.getType().toString().contains("int")) {
|
|
|
|
|
double dnum = Double.valueOf(cellString);
|
|
|
|
|
int inum = (int) dnum;
|
|
|
|
|
fields[j].set(newObj, inum);
|
|
|
|
|
tmpField.set(newObj, inum);
|
|
|
|
|
}
|
|
|
|
|
//double型
|
|
|
|
|
else if (fields[j].getType().toString().contains("Double") ||fields[j].getType().toString().contains("double")) {
|
|
|
|
|
fields[j].set(newObj, Double.valueOf(cellString));
|
|
|
|
|
else if (tmpField.getType().toString().contains("Double") ||tmpField.getType().toString().contains("double")) {
|
|
|
|
|
tmpField.set(newObj, Double.valueOf(cellString));
|
|
|
|
|
}
|
|
|
|
|
//boolean型
|
|
|
|
|
else if (fields[j].getType().toString().contains("boolean") ||fields[j].getType().toString().contains("Boolean")) {
|
|
|
|
|
fields[j].set(newObj, Boolean.valueOf(cellString));
|
|
|
|
|
else if (tmpField.getType().toString().contains("boolean") ||tmpField.getType().toString().contains("Boolean")) {
|
|
|
|
|
tmpField.set(newObj, Boolean.valueOf(cellString));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fields[j].set(newObj, cellString);
|
|
|
|
|
tmpField.set(newObj, cellString);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -201,6 +235,9 @@ public class ExcelOperation {
|
|
|
|
|
*/
|
|
|
|
|
private static String getValue(XSSFCell cell) {
|
|
|
|
|
String value ="";
|
|
|
|
|
if (null == cell) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
switch (cell.getCellType()) {
|
|
|
|
|
case XSSFCell.CELL_TYPE_NUMERIC:
|
|
|
|
|
value = String.valueOf(cell.getNumericCellValue());
|
|
|
|
@ -216,4 +253,45 @@ public class ExcelOperation {
|
|
|
|
|
}
|
|
|
|
|
return value.trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 过滤掉 filters中含有的 属性
|
|
|
|
|
* @param filters
|
|
|
|
|
* @param fields
|
|
|
|
|
* @param cur
|
|
|
|
|
* @param num
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private static int getIndex(List<String> filters, Field[] fields,int cur, int num) {
|
|
|
|
|
if (null == filters || filters.size() == 0) {
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
if (cur >= fields.length) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (filters.contains(fields[cur].getName())) {
|
|
|
|
|
num++;
|
|
|
|
|
num = getIndex(filters, fields, cur+1, num);
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
/** 过滤掉 filters中含有的 属性
|
|
|
|
|
* @param filters
|
|
|
|
|
* @param fields
|
|
|
|
|
* @param cur
|
|
|
|
|
* @param num
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private static int getWriteIndex(List<String> filters, Field[] fields,int cur, int num) {
|
|
|
|
|
if (null == filters || filters.size() == 0) {
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
if (cur >= fields.length) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (filters.contains(fields[cur].getName())) {
|
|
|
|
|
num--;
|
|
|
|
|
num = getWriteIndex(filters, fields, cur+1, num);
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|