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

Fix#177 #180

Merged
merged 8 commits into from
May 13, 2021
Merged
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ default void setNullValue(int row, Cell cell, Sheet.Column hc) {
if (hasIntProcessor) {
conversion(row, cell, 0, hc);
} else
cell.setBlank();
cell.blank();
}

/**
Expand All @@ -164,7 +164,7 @@ default void conversion(int row, Cell cell, int n, Sheet.Column hc) {
setCellValue(row, cell, e, hc, clazz);
}
} else {
cell.setBlank();
cell.blank();
}
}
}
78 changes: 66 additions & 12 deletions src/main/java/org/ttzero/excel/entity/ResultSetSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,33 @@ protected void resetBlockData() {
row.index = rows;
Cell[] cells = row.realloc(len);
for (int i = 1; i <= len; i++) {
Column hc = columns[i - 1];
SQLColumn hc = (SQLColumn) columns[i - 1];

// clear cells
Cell cell = cells[i - 1];
cell.clear();

Object e = rs.getObject(i);
Object e;
switch (hc.sqlType) {
case VARCHAR:
case LONGVARCHAR:
case NULL: e = rs.getString(i); break;
case INTEGER:
case TINYINT:
case SMALLINT:
case BIT:
case CHAR: e = rs.getInt(i); break;
case DATE: e = rs.getDate(i); break;
case TIMESTAMP: e = rs.getTimestamp(i); break;
case NUMERIC:
case DECIMAL: e = rs.getBigDecimal(i); break;
case BIGINT: e = rs.getLong(i); break;
case REAL:
case FLOAT:
case DOUBLE: e = rs.getDouble(i); break;
case TIME: e = rs.getTime(i); break;
default: e = rs.getObject(i); break;
}

cellValueAndStyle.reset(rows, cell, e, hc);
}
Expand Down Expand Up @@ -241,24 +261,28 @@ public Column[] getHeaderColumns() {
try {
ResultSetMetaData metaData = rs.getMetaData();
if (hasHeaderColumns()) {
Column[] newColumns = new SQLColumn[columns.length];
for (; i < columns.length; i++) {
Column column = columns[i];
SQLColumn column = SQLColumn.of(columns[i]);
newColumns[i] = column;
if (StringUtil.isEmpty(column.getName())) {
column.setName(metaData.getColumnLabel(i + 1));
}
// FIXME maybe do not reset the types
Class<?> metaClazz = columnTypeToClass(metaData.getColumnType(i + 1));
column.sqlType = metaData.getColumnType(i + 1);
Class<?> metaClazz = columnTypeToClass(column.sqlType);
if (column.clazz != metaClazz) {
what("The specified type " + column.clazz + " is different" +
" from metadata column type " + metaClazz);
column.clazz = metaClazz;
}
}
columns = newColumns;
} else {
int count = metaData.getColumnCount();
columns = new Column[count];
for (; ++i <= count; ) {
columns[i - 1] = new Column(metaData.getColumnLabel(i)
columns[i - 1] = new SQLColumn(metaData.getColumnLabel(i), metaData.getColumnType(i)
, columnTypeToClass(metaData.getColumnType(i)));
}
}
Expand Down Expand Up @@ -296,22 +320,52 @@ protected Class<?> columnTypeToClass(int type) {
case CHAR:
case LONGVARCHAR:
case NULL: clazz = String.class; break;
case INTEGER: clazz = int.class; break;
case INTEGER: clazz = Integer.class; break;
case DATE: clazz = java.sql.Date.class; break;
case TIMESTAMP: clazz = Timestamp.class; break;
case NUMERIC:
case DECIMAL: clazz = BigDecimal.class; break;
case BIT: clazz = boolean.class; break;
case TINYINT: clazz = byte.class; break;
case SMALLINT: clazz = short.class; break;
case BIGINT: clazz = long.class; break;
case REAL: clazz = float.class; break;
case BIT: clazz = Boolean.class; break;
case TINYINT: clazz = Byte.class; break;
case SMALLINT: clazz = Short.class; break;
case BIGINT: clazz = Long.class; break;
case REAL: clazz = Float.class; break;
case FLOAT:
case DOUBLE: clazz = double.class; break;
case DOUBLE: clazz = Double.class; break;
// case CHAR: clazz = char.class; break;
case TIME: clazz = java.sql.Time.class; break;
default: clazz = Object.class;
}
return clazz;
}

private static class SQLColumn extends Column {
int sqlType;

public SQLColumn(String name, int sqlType, Class<?> clazz) {
super(name, clazz);
this.sqlType = sqlType;
}

public static SQLColumn of(Column other) {
SQLColumn self = new SQLColumn(other.name, 0, other.clazz);
self.key = other.key;
self.name = other.name;
self.clazz = other.clazz;
self.share = other.share;
self.type = other.type;
self.processor = other.processor;
self.styleProcessor = other.styleProcessor;
self.cellStyle = other.cellStyle;
self.width = other.width;
self.o = other.o;
self.styles = other.styles;
self.headerComment = other.headerComment;
self.cellComment = other.cellComment;
self.numFmt = other.numFmt;
self.ignoreValue = other.ignoreValue;
self.wrapText = other.wrapText;
return self;
}
}
}
10 changes: 6 additions & 4 deletions src/main/java/org/ttzero/excel/entity/Sheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ public static class Column {
/**
* The string value is shared
*/
public boolean share = true;
public boolean share;
/**
* 0: standard 1:percentage 2:RMB
* @deprecated Do not use.
*/
@Deprecated
public int type;
/**
* The int value conversion
Expand All @@ -261,15 +263,15 @@ public static class Column {
/**
* Specify the cell number format
*/
private NumFmt numFmt;
public NumFmt numFmt;
/**
* Only export column name and ignore value
*/
private boolean ignoreValue;
public boolean ignoreValue;
/**
* Wrap text in a cell
*/
private int wrapText;
public int wrapText;

/**
* Constructor Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void conversion(int row, Cell cell, int n, Sheet.Column hc) {
cell.xf = getStyleIndex(row, hc, n, style);
}
} else {
cell.setBlank();
cell.blank();
cell.xf = getStyleIndex(row, hc, null);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/ttzero/excel/reader/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Cell() { }
public static final char DATETIME = 'i';
public static final char DATE = 'a';
public static final char TIME = 't';
public static final char UNICODE_EMPTY= '\u0000';
public static final char UNALLOCATED = '\0';
public static final char EMPTY_TAG = 'e';
/**
* Value type
Expand Down Expand Up @@ -128,10 +128,14 @@ public void setCv(char c) {
this.cv = c;
}

public void setBlank() {
public void blank() {
this.t = BLANK;
}

public void emptyTag() {
this.t = EMPTY_TAG;
}

public void setLv(long lv) {
this.t = LONG;
this.lv = lv;
Expand All @@ -158,7 +162,7 @@ public void setTv(double t) {
}

public void clear() {
this.t = '\0';
this.t = UNALLOCATED;
this.sv = null;
this.nv = 0;
this.dv = 0.0;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/ttzero/excel/reader/CellType.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public enum CellType {
Null
*/
, BLANK
/*
UNALLOCATED
*/
, UNALLOCATED
}
Loading