Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2017-2019, [email protected] All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ttzero.excel.reader;
import org.ttzero.excel.annotation.ExcelColumn;
import org.ttzero.excel.annotation.ExcelColumns;
import org.ttzero.excel.annotation.IgnoreImport;
import org.ttzero.excel.annotation.RowNum;
import org.ttzero.excel.entity.Column;
import org.ttzero.excel.entity.ListSheet;
import org.ttzero.excel.manager.Const;
import org.ttzero.excel.processor.Converter;
import org.ttzero.excel.util.StringUtil;
import java.beans.IntrospectionException;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import static org.ttzero.excel.entity.IWorksheetWriter.isBool;
import static org.ttzero.excel.entity.IWorksheetWriter.isChar;
import static org.ttzero.excel.entity.IWorksheetWriter.isDate;
import static org.ttzero.excel.entity.IWorksheetWriter.isInt;
import static org.ttzero.excel.entity.IWorksheetWriter.isLocalDate;
import static org.ttzero.excel.entity.IWorksheetWriter.isLocalDateTime;
import static org.ttzero.excel.entity.IWorksheetWriter.isLocalTime;
import static org.ttzero.excel.entity.Sheet.toCoordinate;
import static org.ttzero.excel.util.ReflectUtil.listDeclaredFieldsUntilJavaPackage;
import static org.ttzero.excel.util.ReflectUtil.listDeclaredMethods;
import static org.ttzero.excel.util.ReflectUtil.writeMethodsMap;
import static org.ttzero.excel.util.StringUtil.EMPTY;
import static org.ttzero.excel.util.StringUtil.lowFirstKey;
import static org.ttzero.excel.util.StringUtil.toCamelCase;
/**
* 表头行,包含列与对象的映射关系
*
* @author guanquan.wang at 2019-04-17 11:55
*/
public class HeaderRow extends Row {
/**
* Force Import (Match with field name if without {@code ExcelColumn} annotation)
*/
public static final int FORCE_IMPORT = 1;
/**
* Ignore Case (Ignore case matching column names)
*/
public static final int IGNORE_CASE = 1 << 1;
/**
* Camel Case (CAMEL_CASE to camelCase)
*/
public static final int CAMEL_CASE = 1 << 2;
protected String[] names;
protected Class> clazz;
protected Object t;
/* The column name and column position mapping */
protected Map mapping;
/* Storage header column */
protected ListSheet.EntryColumn[] columns;
// Specify total rows of header
protected int headRows;
/**
* Simple properties
*
*
* Bit | Contents
* ------+---------
* 31. 1 | Force Import
* 30. 1 | Ignore Case (Ignore case matching column names)
* 29. 1 | Camel Case
*
*/
protected int option;
public HeaderRow with(Row ... rows) {
return with(null, rows.length, rows);
}
public HeaderRow with(int headRows, Row ... rows) {
return with(null, headRows, rows);
}
public HeaderRow with(List mergeCells, Row ... rows) {
return with(mergeCells, rows.length, rows);
}
public HeaderRow with(List mergeCells, int headRows, Row ... rows) {
this.headRows = headRows;
Row row = rows[rows.length - 1];
if (row == null) return new HeaderRow();
this.names = new String[row.lc];
this.mapping = new HashMap<>();
// Extends from row
this.fc = row.fc;
this.lc = row.lc;
this.index = row.index;
this.cells = new Cell[this.names.length];
for (int i = 0; i < row.fc; i++) {
this.cells[i] = new Cell(i);
}
if (headRows == 1) {
for (int i = row.fc; i < row.lc; i++) {
Cell hCell = row.getCell(i);
this.names[i] = row.getString(hCell);
this.mapping.put(makeKey(this.names[i]), i);
Cell cell = new Cell(hCell.i);
cell.xf = hCell.xf;
cell.setString(this.names[i]);
this.cells[i] = cell;
}
} else {
// Copy on merge cells
mergeCellsIfNull(mergeCells, rows);
StringBuilder buf = new StringBuilder();
for (int i = row.fc; i < row.lc; i++) {
buf.delete(0, buf.length());
for (Row r : rows) {
String tmp = r.getString(i);
if (StringUtil.isNotEmpty(tmp)) {
buf.append(tmp).append(':');
}
}
if (buf.length() > 1) buf.deleteCharAt(buf.length() - 1);
this.names[i] = buf.toString();
this.mapping.put(makeKey(this.names[i]), i);
Cell hCell = rows[rows.length - 1].getCell(i);
Cell cell = new Cell(hCell != null ? hCell.i : i);
cell.xf = hCell != null ? hCell.xf : 0;
cell.setString(this.names[i]);
this.cells[i] = cell;
}
}
return this;
}
public final boolean is(Class> clazz) {
return this.clazz != null && this.clazz == clazz;
}
/**
* mapping
*
* @param clazz the type of binding
* @return the header row
*/
protected HeaderRow setClass(Class> clazz) {
this.clazz = clazz;
// Parse Field
Field[] declaredFields = listDeclaredFieldsUntilJavaPackage(clazz, c -> !ignoreColumn(c));
// Parse Method
Map tmp = new HashMap<>();
try {
tmp.putAll(writeMethodsMap(clazz, Object.class));
} catch (IntrospectionException e) {
LOGGER.warn("Get class {} methods failed.", clazz);
}
List list = new ArrayList<>();
// Map columnMap = new LinkedHashMap<>();
ListSheet.EntryColumn column;
for (int i = 0; i < declaredFields.length; i++) {
Field f = declaredFields[i];
if (!f.isAccessible()) f.setAccessible(true);
String gs = f.getName();
// The setter methods take precedence over property reflection
Method method = tmp.get(gs);
if (method != null) {
column = createColumn(method);
if (column != null) {
ListSheet.EntryColumn tail = column.tail != null ? (ListSheet.EntryColumn) column.tail : column;
tail.method = method;
if (StringUtil.isEmpty(tail.name)) tail.name = method.getName();
if (tail.clazz == null) tail.clazz = method.getParameterTypes()[0];
if (tail.colIndex < 0) tail.colIndex = check(tail.name, gs);
// if ((other = columnMap.get(tail.getName())) == null || other.getMethod() == null) columnMap.put(tail.name, column);
list.add(column);
continue;
}
}
column = createColumn(f);
if (column != null) {
ListSheet.EntryColumn tail = column.tail != null ? (ListSheet.EntryColumn) column.tail : column;
if (StringUtil.isEmpty(tail.name)) tail.name = gs;
if (method != null) {
tail.method = method;
if (tail.clazz == null) tail.clazz = method.getParameterTypes()[0];
} else {
tail.field = f;
if (tail.clazz == null) tail.clazz = declaredFields[i].getType();
}
if (tail.colIndex < 0) tail.colIndex = check(tail.name, gs);
// if ((other = columnMap.get(tail.getName())) == null || other.getMethod() == null) columnMap.put(tail.name, column);
list.add(column);
}
}
// Others
Map otherColumns = attachOtherColumn(clazz);
if (!otherColumns.isEmpty()) {
// 排除已存在的列
for (Column col : list) otherColumns.remove(col.name);
// 添加
for (Map.Entry entry : otherColumns.entrySet()) {
column = createColumn(entry.getValue());
if (column == null) column = new ListSheet.EntryColumn(entry.getKey());
ListSheet.EntryColumn tail = column.tail != null ? (ListSheet.EntryColumn) column.tail : column;
if (StringUtil.isEmpty(tail.name)) tail.name = entry.getKey();
tail.method = entry.getValue();
if (tail.clazz == null) tail.clazz = entry.getValue().getParameterTypes()[0];
if (tail.colIndex < 0) tail.colIndex = getIndex(tail.name);
// if ((other = columnMap.get(tail.getName())) == null || other.getMethod() == null) columnMap.put(tail.name, column);
list.add(column);
}
}
// Merge cells
org.ttzero.excel.entity.Sheet listSheet = new ListSheet