From 71cf6ce6d5794fdd2a1a8c8b03b26207ca5e471b Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 16:54:08 +0800 Subject: [PATCH 1/8] Add Coll type `UNALLOCATED` --- src/main/java/org/ttzero/excel/reader/Cell.java | 10 +++++++--- src/main/java/org/ttzero/excel/reader/CellType.java | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/ttzero/excel/reader/Cell.java b/src/main/java/org/ttzero/excel/reader/Cell.java index e0156283..6e7952a6 100644 --- a/src/main/java/org/ttzero/excel/reader/Cell.java +++ b/src/main/java/org/ttzero/excel/reader/Cell.java @@ -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 @@ -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; @@ -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; diff --git a/src/main/java/org/ttzero/excel/reader/CellType.java b/src/main/java/org/ttzero/excel/reader/CellType.java index c0f0d5f3..488b4387 100644 --- a/src/main/java/org/ttzero/excel/reader/CellType.java +++ b/src/main/java/org/ttzero/excel/reader/CellType.java @@ -48,4 +48,8 @@ public enum CellType { Null */ , BLANK + /* + UNALLOCATED + */ + , UNALLOCATED } From 30700549ddbca80d029c127a7c172559d5a06127 Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 16:59:13 +0800 Subject: [PATCH 2/8] Add blank & empty_tag func --- .../excel/entity/ICellValueAndStyle.java | 4 +- .../excel/entity/e7/XMLCellValueAndStyle.java | 2 +- .../java/org/ttzero/excel/reader/Row.java | 56 ++++++++++++++++++- .../java/org/ttzero/excel/reader/XMLRow.java | 12 ++-- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java b/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java index 1958ce6d..77124233 100644 --- a/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java +++ b/src/main/java/org/ttzero/excel/entity/ICellValueAndStyle.java @@ -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(); } /** @@ -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(); } } } diff --git a/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java b/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java index a6299899..16a5b9bf 100644 --- a/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java +++ b/src/main/java/org/ttzero/excel/entity/e7/XMLCellValueAndStyle.java @@ -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); } } diff --git a/src/main/java/org/ttzero/excel/reader/Row.java b/src/main/java/org/ttzero/excel/reader/Row.java index dccc4778..f1e55e49 100644 --- a/src/main/java/org/ttzero/excel/reader/Row.java +++ b/src/main/java/org/ttzero/excel/reader/Row.java @@ -43,7 +43,7 @@ import static org.ttzero.excel.reader.Cell.NUMERIC; import static org.ttzero.excel.reader.Cell.SST; import static org.ttzero.excel.reader.Cell.TIME; -import static org.ttzero.excel.reader.Cell.UNICODE_EMPTY; +import static org.ttzero.excel.reader.Cell.UNALLOCATED; import static org.ttzero.excel.util.DateUtil.toDate; import static org.ttzero.excel.util.DateUtil.toLocalDate; import static org.ttzero.excel.util.DateUtil.toLocalDateTime; @@ -257,6 +257,10 @@ protected byte getByte(Cell c) { case DOUBLE: b |= (int) c.dv; break; + case BLANK: + case EMPTY_TAG: + case UNALLOCATED: + break; default: throw new UncheckedTypeException("Can't convert cell value to byte"); } return b; @@ -315,6 +319,10 @@ protected char getChar(Cell c) { case DOUBLE: cc |= (int) c.dv; break; + case BLANK: + case EMPTY_TAG: + case UNALLOCATED: + break; default: throw new UncheckedTypeException("Can't convert cell value to char"); } return cc; @@ -435,6 +443,11 @@ protected int getInt(Cell c) { case BOOL: n = c.bv ? 1 : 0; break; + case BLANK: + case EMPTY_TAG: + case UNALLOCATED: + n = 0; + break; default: throw new UncheckedTypeException("Can't convert cell value to int"); } @@ -543,6 +556,10 @@ protected String getString(Cell c) { case BLANK: s = EMPTY; break; + case EMPTY_TAG: + case UNALLOCATED: + s = null; + break; case LONG: s = String.valueOf(c.lv); break; @@ -682,7 +699,8 @@ protected BigDecimal getDecimal(Cell c) { case INLINESTR: bd = new BigDecimal(c.sv); break; - case UNICODE_EMPTY: + case UNALLOCATED: + case BLANK: case EMPTY_TAG: bd = null; break; @@ -736,6 +754,11 @@ protected Date getDate(Cell c) { case INLINESTR: date = toDate(c.sv); break; + case UNALLOCATED: + case BLANK: + case EMPTY_TAG: + date = null; + break; default: throw new UncheckedTypeException("Can't convert cell value to java.util.Date"); } return date; @@ -786,6 +809,11 @@ protected Timestamp getTimestamp(Cell c) { case INLINESTR: ts = toTimestamp(c.sv); break; + case UNALLOCATED: + case BLANK: + case EMPTY_TAG: + ts = null; + break; default: throw new UncheckedTypeException("Can't convert cell value to java.sql.Timestamp"); } return ts; @@ -827,6 +855,11 @@ protected java.sql.Time getTime(Cell c) { } // @Mark:=>There is no missing `break`, this is normal logic here case INLINESTR: t = toTime(c.sv); break; + case UNALLOCATED: + case BLANK: + case EMPTY_TAG: + t = null; + break; default: throw new UncheckedTypeException("Can't convert cell value to java.sql.Time"); } @@ -878,6 +911,11 @@ protected LocalDateTime getLocalDateTime(Cell c) { case INLINESTR: ldt = toTimestamp(c.sv).toLocalDateTime(); break; + case UNALLOCATED: + case BLANK: + case EMPTY_TAG: + ldt = null; + break; default: throw new UncheckedTypeException("Can't convert cell value to java.time.LocalDateTime"); } return ldt; @@ -928,6 +966,11 @@ protected LocalDate getLocalDate(Cell c) { case INLINESTR: ld = toTimestamp(c.sv).toLocalDateTime().toLocalDate(); break; + case UNALLOCATED: + case BLANK: + case EMPTY_TAG: + ld = null; + break; default: throw new UncheckedTypeException("Can't convert cell value to java.sql.Timestamp"); } return ld; @@ -983,6 +1026,11 @@ protected LocalTime getLocalTime(Cell c) { lt = toTimestamp(c.sv).toLocalDateTime().toLocalTime(); } break; + case UNALLOCATED: + case BLANK: + case EMPTY_TAG: + lt = null; + break; default: throw new UncheckedTypeException("Can't convert cell value to java.sql.Timestamp"); } return lt; @@ -1083,9 +1131,13 @@ protected CellType getCellType(Cell c) { case TIME: type = CellType.DATE; break; + case EMPTY_TAG: case BLANK: type = CellType.BLANK; break; + case UNALLOCATED: + type = CellType.UNALLOCATED; + break; default: type = CellType.STRING; } diff --git a/src/main/java/org/ttzero/excel/reader/XMLRow.java b/src/main/java/org/ttzero/excel/reader/XMLRow.java index 5a6c778e..a9e51f3f 100644 --- a/src/main/java/org/ttzero/excel/reader/XMLRow.java +++ b/src/main/java/org/ttzero/excel/reader/XMLRow.java @@ -19,12 +19,10 @@ import org.ttzero.excel.entity.style.Styles; import static org.ttzero.excel.reader.Cell.BOOL; -import static org.ttzero.excel.reader.Cell.EMPTY_TAG; import static org.ttzero.excel.reader.Cell.NUMERIC; import static org.ttzero.excel.reader.Cell.FUNCTION; import static org.ttzero.excel.reader.Cell.SST; import static org.ttzero.excel.reader.Cell.INLINESTR; -import static org.ttzero.excel.reader.Cell.BLANK; import static org.ttzero.excel.reader.SharedStrings.toInt; import static org.ttzero.excel.reader.SharedStrings.unescape; import static org.ttzero.excel.util.StringUtil.swap; @@ -75,7 +73,7 @@ public int getRowNumber() { /////////////////////////////////////////////////////// XMLRow with(char[] cb, int from, int size) { -// LOGGER.info(new String(cb, from, size)); +// LOGGER.debug(new String(cb, from, size)); this.cb = cb; this.from = from; this.to = from + size; @@ -87,7 +85,7 @@ XMLRow with(char[] cb, int from, int size) { /* empty row*/ XMLRow empty(char[] cb, int from, int size) { -// LOGGER.info(new String(cb, from, size)); +// LOGGER.debug(new String(cb, from, size)); this.cb = cb; this.from = from; this.to = from + size; @@ -352,7 +350,7 @@ void parseCellValue(Cell cell) { if (a < cursor) { cell.setSv(unescape(buf, cb, a, cursor)); } else { // null value - cell.setT(BLANK); // Reset type to BLANK if null value + cell.blank(); // Reset type to BLANK if null value } break; case SST: // shared string lazy get @@ -371,7 +369,7 @@ void parseCellValue(Cell cell) { if (a < cursor) { cell.setSv(unescape(buf, cb, a, cursor)); } else { // null value - cell.setT(BLANK); // Reset type to BLANK if null value + cell.blank(); // Reset type to BLANK if null value } break; default: @@ -391,7 +389,7 @@ void parseCellValue(Cell cell) { } } // Maybe the cell should be merged - else cell.setT(EMPTY_TAG); + else cell.emptyTag(); } // end of cell From d89f995b0803257749b0e4113f1a742472cb5d26 Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 16:59:35 +0800 Subject: [PATCH 3/8] Jdbc username & password properties --- src/test/resources/test.properties | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties index cf28ea3b..b378e287 100644 --- a/src/test/resources/test.properties +++ b/src/test/resources/test.properties @@ -1,3 +1,5 @@ #sqlite3 dataSource.driverClassName = org.sqlite.JDBC -dataSource.url = jdbc:sqlite:target/eec-test.db \ No newline at end of file +dataSource.url = jdbc:sqlite:target/eec-test.db +dataSource.username = +dataSource.password = \ No newline at end of file From d74c6c4a774cdc6a7e2054246634daf34ecb1c28 Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 17:34:46 +0800 Subject: [PATCH 4/8] Use box type instead of basic types by default --- .../org/ttzero/excel/reader/HeaderRow.java | 96 +++++++-- .../java/org/ttzero/excel/reader/Row.java | 185 ++++++++++-------- 2 files changed, 179 insertions(+), 102 deletions(-) diff --git a/src/main/java/org/ttzero/excel/reader/HeaderRow.java b/src/main/java/org/ttzero/excel/reader/HeaderRow.java index abaf8a63..236b51f8 100644 --- a/src/main/java/org/ttzero/excel/reader/HeaderRow.java +++ b/src/main/java/org/ttzero/excel/reader/HeaderRow.java @@ -363,10 +363,10 @@ private void fieldPut(int i, Row row, Object t) throws IllegalAccessException { if (fieldClazz[i] == String.class) { fields[i].set(t, row.getString(c)); } - else if (fieldClazz[i] == Integer.class || fieldClazz[i] == int.class) { + else if (fieldClazz[i] == Integer.class) { fields[i].set(t, row.getInt(c)); } - else if (fieldClazz[i] == Long.class || fieldClazz[i] == long.class) { + else if (fieldClazz[i] == Long.class) { fields[i].set(t, row.getLong(c)); } else if (fieldClazz[i] == java.util.Date.class || fieldClazz[i] == java.sql.Date.class) { @@ -375,18 +375,38 @@ else if (fieldClazz[i] == java.util.Date.class || fieldClazz[i] == java.sql.Date else if (fieldClazz[i] == java.sql.Timestamp.class) { fields[i].set(t, row.getTimestamp(c)); } - else if (fieldClazz[i] == Double.class || fieldClazz[i] == double.class) { + else if (fieldClazz[i] == Double.class) { fields[i].set(t, row.getDouble(c)); } - else if (fieldClazz[i] == Float.class || fieldClazz[i] == float.class) { + else if (fieldClazz[i] == Float.class) { fields[i].set(t, row.getFloat(c)); } - else if (fieldClazz[i] == Boolean.class || fieldClazz[i] == boolean.class) { + else if (fieldClazz[i] == Boolean.class) { fields[i].set(t, row.getBoolean(c)); } else if (fieldClazz[i] == BigDecimal.class) { fields[i].set(t, row.getDecimal(c)); } + else if (fieldClazz[i] == int.class) { + Integer v; + fields[i].set(t, (v = row.getInt(c)) != null ? v : 0); + } + else if (fieldClazz[i] == long.class) { + Long v; + fields[i].set(t, (v = row.getLong(c)) != null ? v : 0L); + } + else if (fieldClazz[i] == double.class) { + Double v; + fields[i].set(t, (v = row.getDouble(c)) != null ? v : 0.0D); + } + else if (fieldClazz[i] == float.class) { + Float v; + fields[i].set(t, (v = row.getFloat(c)) != null ? v : 0.0F); + } + else if (fieldClazz[i] == boolean.class) { + Boolean v; + fields[i].set(t, (v = row.getBoolean(c)) != null ? v : false); + } else if (fieldClazz[i] == java.sql.Time.class) { fields[i].set(t, row.getTime(c)); } @@ -399,15 +419,27 @@ else if (fieldClazz[i] == LocalDate.class) { else if (fieldClazz[i] == LocalTime.class) { fields[i].set(t, row.getLocalTime(c)); } - else if (fieldClazz[i] == Character.class || fieldClazz[i] == char.class) { + else if (fieldClazz[i] == Character.class) { fields[i].set(t, row.getChar(c)); } - else if (fieldClazz[i] == Byte.class || fieldClazz[i] == byte.class) { + else if (fieldClazz[i] == Byte.class) { fields[i].set(t, row.getByte(c)); } - else if (fieldClazz[i] == Short.class || fieldClazz[i] == short.class) { + else if (fieldClazz[i] == Short.class) { fields[i].set(t, row.getShort(c)); } + else if (fieldClazz[i] == char.class) { + Character v; + fields[i].set(t, (v = row.getChar(c)) != null ? v : '\0'); + } + else if (fieldClazz[i] == byte.class) { + Byte v; + fields[i].set(t, (v = row.getByte(c)) != null ? v : 0); + } + else if (fieldClazz[i] == short.class) { + Short v; + fields[i].set(t, (v = row.getShort(c)) != null ? v : 0); + } } private void methodPut(int i, Row row, Object t) throws IllegalAccessException, InvocationTargetException { @@ -415,10 +447,10 @@ private void methodPut(int i, Row row, Object t) throws IllegalAccessException, if (fieldClazz[i] == String.class) { methods[i].invoke(t, row.getString(c)); } - else if (fieldClazz[i] == Integer.class || fieldClazz[i] == int.class) { + else if (fieldClazz[i] == Integer.class) { methods[i].invoke(t, row.getInt(c)); } - else if (fieldClazz[i] == Long.class || fieldClazz[i] == long.class) { + else if (fieldClazz[i] == Long.class) { methods[i].invoke(t, row.getLong(c)); } else if (fieldClazz[i] == java.util.Date.class || fieldClazz[i] == java.sql.Date.class) { @@ -427,18 +459,38 @@ else if (fieldClazz[i] == java.util.Date.class || fieldClazz[i] == java.sql.Date else if (fieldClazz[i] == java.sql.Timestamp.class) { methods[i].invoke(t, row.getTimestamp(c)); } - else if (fieldClazz[i] == Double.class || fieldClazz[i] == double.class) { + else if (fieldClazz[i] == Double.class) { methods[i].invoke(t, row.getDouble(c)); } - else if (fieldClazz[i] == Float.class || fieldClazz[i] == float.class) { + else if (fieldClazz[i] == Float.class) { methods[i].invoke(t, row.getFloat(c)); } - else if (fieldClazz[i] == Boolean.class || fieldClazz[i] == boolean.class) { + else if (fieldClazz[i] == Boolean.class) { methods[i].invoke(t, row.getBoolean(c)); } else if (fieldClazz[i] == BigDecimal.class) { methods[i].invoke(t, row.getDecimal(c)); } + else if (fieldClazz[i] == int.class) { + Integer v; + methods[i].invoke(t, (v = row.getInt(c)) != null ? v : 0); + } + else if (fieldClazz[i] == long.class) { + Long v; + methods[i].invoke(t, (v = row.getLong(c)) != null ? v : 0); + } + else if (fieldClazz[i] == double.class) { + Double v; + methods[i].invoke(t, (v = row.getDouble(c)) != null ? v : 0.0D); + } + else if (fieldClazz[i] == float.class) { + Float v; + methods[i].invoke(t, (v = row.getFloat(c)) != null ? v : 0.0F); + } + else if (fieldClazz[i] == boolean.class) { + Boolean v; + methods[i].invoke(t, (v = row.getBoolean(c)) != null ? v : false); + } else if (fieldClazz[i] == java.sql.Time.class) { methods[i].invoke(t, row.getTime(c)); } @@ -451,14 +503,26 @@ else if (fieldClazz[i] == LocalDate.class) { else if (fieldClazz[i] == LocalTime.class) { methods[i].invoke(t, row.getLocalTime(c)); } - else if (fieldClazz[i] == Character.class || fieldClazz[i] == char.class) { + else if (fieldClazz[i] == Character.class) { methods[i].invoke(t, row.getChar(c)); } - else if (fieldClazz[i] == Byte.class || fieldClazz[i] == byte.class) { + else if (fieldClazz[i] == Byte.class) { methods[i].invoke(t, row.getByte(c)); } - else if (fieldClazz[i] == Short.class || fieldClazz[i] == short.class) { + else if (fieldClazz[i] == Short.class) { methods[i].invoke(t, row.getShort(c)); } + else if (fieldClazz[i] == char.class) { + Character v; + methods[i].invoke(t, (v = row.getChar(c)) != null ? v : '\0'); + } + else if (fieldClazz[i] == byte.class) { + Byte v; + methods[i].invoke(t, (v = row.getByte(c)) != null ? v : 0); + } + else if (fieldClazz[i] == short.class) { + Short v; + methods[i].invoke(t, (v = row.getShort(c)) != null ? v : 0); + } } } diff --git a/src/main/java/org/ttzero/excel/reader/Row.java b/src/main/java/org/ttzero/excel/reader/Row.java index f1e55e49..ffdd8f94 100644 --- a/src/main/java/org/ttzero/excel/reader/Row.java +++ b/src/main/java/org/ttzero/excel/reader/Row.java @@ -163,34 +163,34 @@ Row setHr(HeaderRow hr) { } /** - * Get boolean value by column index + * Get {@code Boolean} value by column index * * @param columnIndex the cell index - * @return boolean + * @return {@code Boolean} */ - public boolean getBoolean(int columnIndex) { + public Boolean getBoolean(int columnIndex) { Cell c = getCell(columnIndex); return getBoolean(c); } /** - * Get boolean value by column name + * Get {@code Boolean} value by column name * * @param columnName the cell name - * @return boolean + * @return {@code Boolean} */ - public boolean getBoolean(String columnName) { + public Boolean getBoolean(String columnName) { Cell c = getCell(columnName); return getBoolean(c); } /** - * Get boolean value + * Get {@code Boolean} value * * @param c the {@link Cell} - * @return boolean + * @return {@code Boolean} */ - protected boolean getBoolean(Cell c) { + protected Boolean getBoolean(Cell c) { boolean v; switch (c.t) { case BOOL: @@ -208,41 +208,44 @@ protected boolean getBoolean(Cell c) { case INLINESTR: v = isNotEmpty(c.sv); break; - + case BLANK: + case EMPTY_TAG: + case UNALLOCATED: + return null; default: v = false; } return v; } /** - * Get byte value by column index + * Get {@code Byte} value by column index * * @param columnIndex the cell index - * @return byte + * @return {@code Byte} */ - public byte getByte(int columnIndex) { + public Byte getByte(int columnIndex) { Cell c = getCell(columnIndex); return getByte(c); } /** - * Get byte value by column name + * Get {@code Byte} value by column name * * @param columnName the cell name - * @return byte + * @return {@code Byte} */ - public byte getByte(String columnName) { + public Byte getByte(String columnName) { Cell c = getCell(columnName); return getByte(c); } /** - * Get byte value + * Get {@code Byte} value * * @param c the {@link Cell} - * @return byte + * @return {@code Byte} */ - protected byte getByte(Cell c) { + protected Byte getByte(Cell c) { byte b = 0; switch (c.t) { case NUMERIC: @@ -260,48 +263,48 @@ protected byte getByte(Cell c) { case BLANK: case EMPTY_TAG: case UNALLOCATED: - break; - default: throw new UncheckedTypeException("Can't convert cell value to byte"); + return null; + default: throw new UncheckedTypeException("Can't convert cell value to Byte"); } return b; } /** - * Get char value by column index + * Get {@code Character} value by column index * * @param columnIndex the cell index - * @return char + * @return {@code Character} */ - public char getChar(int columnIndex) { + public Character getChar(int columnIndex) { Cell c = getCell(columnIndex); return getChar(c); } /** - * Get char value by column name + * Get {@code Character} value by column name * * @param columnName the cell name - * @return char + * @return {@code Character} */ - public char getChar(String columnName) { + public Character getChar(String columnName) { Cell c = getCell(columnName); return getChar(c); } /** - * Get char value + * Get {@code Character} value * * @param c the {@link Cell} - * @return char + * @return {@code Character} */ - protected char getChar(Cell c) { + protected Character getChar(Cell c) { char cc = 0; switch (c.t) { case SST: if (c.sv == null) { c.setSv(sst.get(c.nv)); } - // @Mark:=>There is no missing `break`, this is normal logic here + // @Mark:=>There is no missing `break`, this is normal logic here case INLINESTR: if (isNotEmpty(c.sv)) { cc |= c.sv.charAt(0); @@ -322,41 +325,41 @@ protected char getChar(Cell c) { case BLANK: case EMPTY_TAG: case UNALLOCATED: - break; - default: throw new UncheckedTypeException("Can't convert cell value to char"); + return null; + default: throw new UncheckedTypeException("Can't convert cell value to Character"); } return cc; } /** - * Get short value by column index + * Get {@code Short} value by column index * * @param columnIndex the cell index - * @return short + * @return {@code Short} */ - public short getShort(int columnIndex) { + public Short getShort(int columnIndex) { Cell c = getCell(columnIndex); return getShort(c); } /** - * Get short value by column name + * Get {@code Short} value by column name * * @param columnName the cell name - * @return short + * @return {@code Short} */ - public short getShort(String columnName) { + public Short getShort(String columnName) { Cell c = getCell(columnName); return getShort(c); } /** - * Get short value + * Get {@code Short} value * * @param c the {@link Cell} - * @return short + * @return {@code Short} */ - protected short getShort(Cell c) { + protected Short getShort(Cell c) { short s = 0; switch (c.t) { case NUMERIC: @@ -372,7 +375,7 @@ protected short getShort(Cell c) { if (c.sv == null) { c.setSv(sst.get(c.nv)); } - // @Mark:=>There is no missing `break`, this is normal logic here + // @Mark:=>There is no missing `break`, this is normal logic here case INLINESTR: if (c.sv.indexOf('E') >= 0 || c.sv.indexOf('e') >= 0) { s = (short) Double.parseDouble(c.sv); @@ -383,40 +386,44 @@ protected short getShort(Cell c) { case BOOL: s |= c.bv ? 1 : 0; break; + case BLANK: + case EMPTY_TAG: + case UNALLOCATED: + return null; default: throw new UncheckedTypeException("Can't convert cell value to short"); } return s; } /** - * Get int value by column index + * Get {@code Integer} value by column index * * @param columnIndex the cell index - * @return int + * @return {@code Integer} */ - public int getInt(int columnIndex) { + public Integer getInt(int columnIndex) { Cell c = getCell(columnIndex); return getInt(c); } /** - * Get int value by column name + * Get {@code Integer} value by column name * * @param columnName the cell name - * @return int + * @return {@code Integer} */ - public int getInt(String columnName) { + public Integer getInt(String columnName) { Cell c = getCell(columnName); return getInt(c); } /** - * Get int value + * Get {@code Integer} value * * @param c the {@link Cell} - * @return int + * @return {@code Integer} */ - protected int getInt(Cell c) { + protected Integer getInt(Cell c) { int n; switch (c.t) { case NUMERIC: @@ -446,43 +453,42 @@ protected int getInt(Cell c) { case BLANK: case EMPTY_TAG: case UNALLOCATED: - n = 0; - break; + return null; - default: throw new UncheckedTypeException("Can't convert cell value to int"); + default: throw new UncheckedTypeException("Can't convert cell value to Integer"); } return n; } /** - * Get long value by column index + * Get {@code Long} value by column index * * @param columnIndex the cell index - * @return long + * @return {@code Long} */ - public long getLong(int columnIndex) { + public Long getLong(int columnIndex) { Cell c = getCell(columnIndex); return getLong(c); } /** - * Get long value by column name + * Get {@code Long} value by column name * * @param columnName the cell name - * @return long + * @return {@code Long} */ - public long getLong(String columnName) { + public Long getLong(String columnName) { Cell c = getCell(columnName); return getLong(c); } /** - * Get long value + * Get {@code Long} value * * @param c the {@link Cell} - * @return long + * @return {@code Long} */ - protected long getLong(Cell c) { + protected Long getLong(Cell c) { long l; switch (c.t) { case LONG: @@ -498,7 +504,7 @@ protected long getLong(Cell c) { if (c.sv == null) { c.setSv(sst.get(c.nv)); } - // @Mark:=>There is no missing `break`, this is normal logic here + // @Mark:=>There is no missing `break`, this is normal logic here case INLINESTR: if (c.sv.indexOf('E') >= 0 || c.sv.indexOf('e') >= 0) { l = (long) Double.parseDouble(c.sv); @@ -509,6 +515,10 @@ protected long getLong(Cell c) { case BOOL: l = c.bv ? 1L : 0L; break; + case BLANK: + case EMPTY_TAG: + case UNALLOCATED: + return null; default: throw new UncheckedTypeException("Can't convert cell value to long"); } return l; @@ -554,8 +564,6 @@ protected String getString(Cell c) { s = c.sv; break; case BLANK: - s = EMPTY; - break; case EMPTY_TAG: case UNALLOCATED: s = null; @@ -578,54 +586,56 @@ protected String getString(Cell c) { } /** - * Get float value by column index + * Get {@code Float} value by column index * * @param columnIndex the cell index - * @return float + * @return {@code Float} */ - public float getFloat(int columnIndex) { - return (float) getDouble(columnIndex); + public Float getFloat(int columnIndex) { + Double d = getDouble(columnIndex); + return d != null ? Float.valueOf(d.toString()) : null; } /** - * Get float value by column index + * Get {@code Float} value by column index * * @param columnName the cell index - * @return float + * @return {@code Float} */ - public float getFloat(String columnName) { - return (float) getDouble(columnName); + public Float getFloat(String columnName) { + Double d = getDouble(columnName); + return d != null ? Float.valueOf(d.toString()) : null; } /** - * Get double value by column index + * Get {@code Double} value by column index * * @param columnIndex the cell index - * @return double + * @return {@code Double} */ - public double getDouble(int columnIndex) { + public Double getDouble(int columnIndex) { Cell c = getCell(columnIndex); return getDouble(c); } /** - * Get double value by column name + * Get {@code Double} value by column name * * @param columnName the cell name - * @return double + * @return {@code Double} */ - public double getDouble(String columnName) { + public Double getDouble(String columnName) { Cell c = getCell(columnName); return getDouble(c); } /** - * Get double value + * Get {@code Double} value * * @param c the {@link Cell} - * @return double + * @return {@code Double} */ - protected double getDouble(Cell c) { + protected Double getDouble(Cell c) { double d; switch (c.t) { case DOUBLE: @@ -641,11 +651,14 @@ protected double getDouble(Cell c) { if (c.sv == null) { c.setSv(sst.get(c.nv)); } - // @Mark:=>There is no missing `break`, this is normal logic here + // @Mark:=>There is no missing `break`, this is normal logic here case INLINESTR: d = Double.parseDouble(c.sv); break; - + case BLANK: + case EMPTY_TAG: + case UNALLOCATED: + return null; default: throw new UncheckedTypeException("Can't convert cell value to double"); } return d; From 593e5b61fa427a10fac383d3606f2bbf1f136a2a Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 17:37:55 +0800 Subject: [PATCH 5/8] Upgrade ResultSetSheet --- .../ttzero/excel/entity/ResultSetSheet.java | 78 ++++++++++++++++--- .../java/org/ttzero/excel/entity/Sheet.java | 10 ++- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java b/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java index 834b2de6..0f97255b 100644 --- a/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java +++ b/src/main/java/org/ttzero/excel/entity/ResultSetSheet.java @@ -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); } @@ -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))); } } @@ -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; + } + } } diff --git a/src/main/java/org/ttzero/excel/entity/Sheet.java b/src/main/java/org/ttzero/excel/entity/Sheet.java index eb7465be..719ad98e 100644 --- a/src/main/java/org/ttzero/excel/entity/Sheet.java +++ b/src/main/java/org/ttzero/excel/entity/Sheet.java @@ -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 @@ -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 From 2c4be40d719dd15a1915583535bd51a2d87feed8 Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 17:53:10 +0800 Subject: [PATCH 6/8] Test case --- .../excel/entity/ListObjectSheetTest.java | 4 - .../excel/entity/MultiWorksheetTest.java | 4 +- .../excel/entity/ResultSetPagingTest.java | 9 +- .../excel/entity/ResultSetSheetTest.java | 131 ++++------ .../excel/entity/StatementPagingTest.java | 11 +- .../excel/entity/StatementSheetTest.java | 155 ++++++------ .../org/ttzero/excel/entity/TemplateTest.java | 4 +- .../excel/entity/csv/ResultSetSheetTest.java | 74 +----- .../excel/entity/csv/StatementSheetTest.java | 76 ++---- .../excel/reader/ExcelReaderSharedTest.java | 4 +- .../ttzero/excel/reader/ExcelReaderTest.java | 230 +++++++++++------- .../reader/IndexSharedStringTableTest.java | 12 +- .../excel/reader/MultiStyleInCellTest.java | 4 +- src/test/resources/all type.xlsx | Bin 7810 -> 10561 bytes 14 files changed, 302 insertions(+), 416 deletions(-) diff --git a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java index f0789eb1..41144f19 100644 --- a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java +++ b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java @@ -410,8 +410,6 @@ public int getScore() { ExtStudent student = opt.get(); assert student.getId() == 9527; assert student.getScore() == 0; // The setter column name is 'score' - } catch (IOException e) { - e.printStackTrace(); } } @@ -427,8 +425,6 @@ public int getScore() { ExtStudent student = opt.get(); assert student.getId() == 0; assert student.getScore() == 0; - } catch (IOException e) { - e.printStackTrace(); } } diff --git a/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java b/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java index d3963e50..4fdacbeb 100644 --- a/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java +++ b/src/test/java/org/ttzero/excel/entity/MultiWorksheetTest.java @@ -47,7 +47,7 @@ public void testMultiWorksheet() throws IOException { } @Test - public void testMultiDataSource() { + public void testMultiDataSource() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student order by age limit 10"); @@ -87,8 +87,6 @@ public void testMultiDataSource() { // Customize .addSheet(new CustomizeDataSourceSheet("Customize")) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } } diff --git a/src/test/java/org/ttzero/excel/entity/ResultSetPagingTest.java b/src/test/java/org/ttzero/excel/entity/ResultSetPagingTest.java index 7a9dcd3a..df8e8838 100644 --- a/src/test/java/org/ttzero/excel/entity/ResultSetPagingTest.java +++ b/src/test/java/org/ttzero/excel/entity/ResultSetPagingTest.java @@ -23,15 +23,16 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; /** * @author guanquan.wang at 2019-04-29 15:16 */ public class ResultSetPagingTest extends SQLWorkbookTest { @Test - public void testPaging() { + public void testPaging() throws SQLException, IOException { try (Connection con = getConnection()) { - PreparedStatement ps = con.prepareStatement("select id, name, age from student"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student"); ResultSet rs = ps.executeQuery(); new Workbook("result set paging", author) .watch(Print::println) @@ -40,12 +41,12 @@ public void testPaging() { , new Sheet.Column("学号", int.class) , new Sheet.Column("性名", String.class) , new Sheet.Column("年龄", int.class) + , new Sheet.Column("创建时间", Timestamp.class) + , new Sheet.Column("更新", Timestamp.class) ) .setWorkbookWriter(new ReLimitXMLWorkbookWriter()) .writeTo(defaultTestPath); ps.close(); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } } diff --git a/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java b/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java index ac6e2d5a..39999334 100644 --- a/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java +++ b/src/test/java/org/ttzero/excel/entity/ResultSetSheetTest.java @@ -24,15 +24,16 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; /** * @author guanquan.wang at 2019-04-28 21:50 */ public class ResultSetSheetTest extends SQLWorkbookTest { - @Test public void testWrite() { + @Test public void testWrite() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("result set", author) @@ -41,65 +42,55 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .addSheet(rs , new Sheet.Column("学号", int.class) , new Sheet.Column("性名", String.class) - , new Sheet.Column("年龄", int.class) + , new Sheet.Column("年龄", Integer.class) + , new Sheet.Column("创建时间", Timestamp.class) + , new Sheet.Column("更新", Timestamp.class) ) .writeTo(defaultTestPath); - } catch (SQLException|IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor1() { + @Test public void testConstructor1() throws IOException { try { new Workbook("test ResultSet sheet Constructor1", author) - .watch(Print::println) - .addSheet(new ResultSetSheet()) - .writeTo(defaultTestPath); - } catch (IOException e) { - e.printStackTrace(); + .watch(Print::println) + .addSheet(new ResultSetSheet()) + .writeTo(defaultTestPath); } catch (ExcelWriteException e) { assert true; } } - @Test public void testConstructor2() { + @Test public void testConstructor2() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor2", author) .watch(Print::println) .addSheet(new ResultSetSheet().setRs(rs)) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor3() { + @Test public void testConstructor3() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor3", author) .watch(Print::println) .addSheet(new ResultSetSheet("Student").setRs(rs)) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor4() { + @Test public void testConstructor4() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor4", author) @@ -107,20 +98,18 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .addSheet(new ResultSetSheet("Student" , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) - , new Sheet.Column("AGE", int.class)) - .setRs(rs)) + , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) + ).setRs(rs)) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor5() { + @Test public void testConstructor5() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor5", author) @@ -128,54 +117,44 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .addSheet(new ResultSetSheet("Student", WaterMark.of(author) , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) - , new Sheet.Column("AGE", int.class)) - .setRs(rs)) + , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) + ).setRs(rs)) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor6() { + @Test public void testConstructor6() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor6", author) .watch(Print::println) .addSheet(new ResultSetSheet(rs)) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor7() { + @Test public void testConstructor7() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor7", author) .watch(Print::println) .addSheet(new ResultSetSheet("Student", rs)) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor8() { + @Test public void testConstructor8() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor8", author) @@ -184,19 +163,17 @@ public class ResultSetSheetTest extends SQLWorkbookTest { , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) )) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor9() { + @Test public void testConstructor9() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor9", author) @@ -205,19 +182,17 @@ public class ResultSetSheetTest extends SQLWorkbookTest { , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) )) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor10() { + @Test public void testConstructor10() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor10", author) @@ -226,19 +201,17 @@ public class ResultSetSheetTest extends SQLWorkbookTest { , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) )) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor11() { + @Test public void testConstructor11() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet sheet Constructor11", author) @@ -247,19 +220,17 @@ public class ResultSetSheetTest extends SQLWorkbookTest { , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", int.class) + , new Sheet.Column("AGE", int.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) )) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testDiffTypeFromMetadata() { + @Test public void testDiffTypeFromMetadata() throws SQLException, IOException { try ( Connection con = getConnection(); - PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); + PreparedStatement ps = con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"); ResultSet rs = ps.executeQuery() ) { new Workbook("test ResultSet different type from metadata", author) @@ -268,12 +239,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { , new Sheet.Column("ID", String.class) // Integer in database , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", String.class) // // Integer in database + , new Sheet.Column("AGE", int.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) )) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } diff --git a/src/test/java/org/ttzero/excel/entity/StatementPagingTest.java b/src/test/java/org/ttzero/excel/entity/StatementPagingTest.java index 65d6c30a..20b8954d 100644 --- a/src/test/java/org/ttzero/excel/entity/StatementPagingTest.java +++ b/src/test/java/org/ttzero/excel/entity/StatementPagingTest.java @@ -27,20 +27,13 @@ * @author guanquan.wang at 2019-04-28 22:47 */ public class StatementPagingTest extends SQLWorkbookTest { - @Test public void testPaging() { + @Test public void testPaging() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("statement paging", author) .watch(Print::println) - .setConnection(con) - .addSheet("select id, name, age from student" - , new Sheet.Column("学号", int.class) - , new Sheet.Column("性名", String.class) - , new Sheet.Column("年龄", int.class) - ) + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student")) .setWorkbookWriter(new ReLimitXMLWorkbookWriter()) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } } diff --git a/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java b/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java index b3dcea6a..6ceaef30 100644 --- a/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java +++ b/src/test/java/org/ttzero/excel/entity/StatementSheetTest.java @@ -26,268 +26,255 @@ import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; +import java.sql.Timestamp; /** * @author guanquan.wang at 2019-04-28 22:47 */ public class StatementSheetTest extends SQLWorkbookTest { - @Test public void testWrite() { + @Test public void testWrite() throws SQLException, IOException { testWrite(false); } - @Test public void testStyleProcessor() { + @Test public void testStyleProcessor() throws SQLException, IOException { testStyleProcessor(false); } - @Test public void testIntConversion() { + @Test public void testIntConversion() throws SQLException, IOException { testIntConversion(false); } // ---- AUTO SIZE - @Test public void testWriteAutoSize() { + @Test public void testWriteAutoSize() throws SQLException, IOException { testWrite(true); } - @Test public void testStyleProcessorAutoSize() { + @Test public void testStyleProcessorAutoSize() throws SQLException, IOException { testStyleProcessor(true); } - @Test public void testIntConversionAutoSize() { + @Test public void testIntConversionAutoSize() throws SQLException, IOException { testIntConversion(true); } - private void testWrite(boolean autoSize) { + private void testWrite(boolean autoSize) throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("statement", author) .setAutoSize(autoSize) .setConnection(con) - .addSheet("select id, name, age from student order by age" + .addSheet("select id, name, age, create_date, update_date from student order by age" , new Sheet.Column("学号", int.class) , new Sheet.Column("性名", String.class) , new Sheet.Column("年龄", int.class) + , new Sheet.Column("创建时间", Timestamp.class) + , new Sheet.Column("更新", Timestamp.class) ) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - private void testStyleProcessor(boolean autoSize) { + private void testStyleProcessor(boolean autoSize) throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("statement style processor", author) .setAutoSize(autoSize) .setConnection(con) - .addSheet("select id, name, age from student" + .addSheet("select id, name, age, create_date, update_date from student" , new Sheet.Column("学号", int.class) , new Sheet.Column("性名", String.class) , new Sheet.Column("年龄", int.class) .setStyleProcessor((o, style, sst) -> { - int n = (int) o; - if (n < 10) { + Integer n = (Integer) o; + if (n == null || n < 10) { style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.orange)); } return style; }) + , new Sheet.Column("创建时间", Timestamp.class) + , new Sheet.Column("更新", Timestamp.class) ) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - private void testIntConversion(boolean autoSize) { + private void testIntConversion(boolean autoSize) throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test int conversion statement", author) .setConnection(con) .setAutoSize(autoSize) .watch(Print::println) - .addSheet("select id, name, age from student" + .addSheet("select id, name, age, create_date, update_date from student" , new Sheet.Column("学号", int.class) , new Sheet.Column("姓名", String.class) , new Sheet.Column("年龄", int.class, n -> n > 14 ? "高龄" : n) .setStyleProcessor((o, style, sst) -> { - int n = (int) o; - if (n > 14) { + Integer n = (Integer) o; + if (n == null || n > 14) { style = Styles.clearFill(style) | sst.addFill(new Fill(PatternType.solid, Color.orange)); } return style; }) + , new Sheet.Column("创建时间", Timestamp.class) + , new Sheet.Column("更新", Timestamp.class) ) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor1() { + @Test public void testConstructor1() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor1", author) .watch(Print::println) - .addSheet(new StatementSheet(con, "select id, name, age from student limit 10")) + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student limit 10")) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor2() { + @Test public void testConstructor2() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor2", author) .watch(Print::println) - .addSheet(new StatementSheet("Student", con, "select id, name, age from student limit 10")) + .addSheet(new StatementSheet("Student", con, "select id, name, age, create_date, update_date from student limit 10")) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor3() { + @Test public void testConstructor3() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor3", author) .watch(Print::println) - .addSheet(new StatementSheet(con, "select id, name, age from student where id between ? and ?", ps -> { + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student where id between ? and ?", ps -> { ps.setInt(1, 10); ps.setInt(2, 20); })) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor4() { + @Test public void testConstructor4() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor4", author) .watch(Print::println) - .addSheet(new StatementSheet("Student", con, "select id, name, age from student where id between ? and ?", ps -> { + .addSheet(new StatementSheet("Student", con, "select id, name, age, create_date, update_date from student where id between ? and ?", ps -> { ps.setInt(1, 10); ps.setInt(2, 20); })) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor5() { + @Test public void testConstructor5() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor5", author) .watch(Print::println) - .addSheet(new StatementSheet(con, "select id, name, age from student limit 10" + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student limit 10" , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) )) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor6() { + @Test public void testConstructor6() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor6", author) .watch(Print::println) - .addSheet(new StatementSheet("Student", con, "select id, name, age from student limit 10" + .addSheet(new StatementSheet("Student", con, "select id, name, age, create_date, update_date from student limit 10" , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) )) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor7() { + @Test public void testConstructor7() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor7", author) .watch(Print::println) - .addSheet(new StatementSheet(con, "select id, name, age from student where id between ? and ?" + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student where id between ? and ?" , ps -> { ps.setInt(1, 10); ps.setInt(2, 20); } , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) - , new Sheet.Column("AGE", int.class))) + , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) + )) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor8() { + @Test public void testConstructor8() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor8", author) .watch(Print::println) - .addSheet(new StatementSheet("Student", con, "select id, name, age from student where id between ? and ?" + .addSheet(new StatementSheet("Student", con, "select id, name, age, create_date, update_date from student where id between ? and ?" , ps -> { ps.setInt(1, 10); ps.setInt(2, 20); } , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) - , new Sheet.Column("AGE", int.class))) + , new Sheet.Column("AGE", int.class) + , new Sheet.Column("CREATE_DATE", Timestamp.class) + , new Sheet.Column("UPDATE_DATE", Timestamp.class) + )) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor9() { + @Test public void testConstructor9() throws IOException { try { new Workbook("test statement sheet Constructor9", author) .watch(Print::println) .addSheet(new StatementSheet()) .writeTo(defaultTestPath); - } catch (IOException e) { - e.printStackTrace(); } catch (ExcelWriteException e) { assert true; } } - @Test public void testConstructor10() { + @Test public void testConstructor10() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor10", author) .watch(Print::println) .addSheet(new StatementSheet() - .setPs(con.prepareStatement("select id, name, age from student limit 10"))) + .setPs(con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"))) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor11() { + @Test public void testConstructor11() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor11", author) .watch(Print::println) .addSheet(new StatementSheet("Student") - .setPs(con.prepareStatement("select id, name, age from student limit 10"))) + .setPs(con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"))) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor12() { + @Test public void testConstructor12() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor12", author) .watch(Print::println) .addSheet(new StatementSheet("Student", WaterMark.of(author)) - .setPs(con.prepareStatement("select id, name, age from student limit 10"))) + .setPs(con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"))) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor13() { + @Test public void testConstructor13() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor13", author) .watch(Print::println) @@ -295,51 +282,45 @@ private void testIntConversion(boolean autoSize) { , new Sheet.Column("ID", int.class) , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", int.class)) - .setPs(con.prepareStatement("select id, name, age from student limit 10"))) + .setPs(con.prepareStatement("select id, name, age, create_date, update_date from student limit 10"))) .writeTo(defaultTestPath); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testCancelOddStyle() { + @Test public void testCancelOddStyle() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet cancel odd", author) .watch(Print::println) - .addSheet(new StatementSheet(con, "select id, name, age from student limit 10") + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student limit 10") .setWaterMark(WaterMark.of("TEST")) .cancelOddStyle() ) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testDiffTypeFromMetadata() { + @Test public void testDiffTypeFromMetadata() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test Statement different type from metadata", author) .watch(Print::println) - .addSheet(new StatementSheet(con, "select id, name, age from student limit 10" + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student limit 10" , new Sheet.Column("ID", String.class) // Integer in database , new Sheet.Column("NAME", String.class) , new Sheet.Column("AGE", String.class) // Integer in database + , new Sheet.Column("CREATE_DATE", String.class) // Timestamp in database + , new Sheet.Column("UPDATE_DATE", String.class) // Timestamp in database )) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testFixWidth() { + @Test public void testFixWidth() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement fix width", author) .watch(Print::println) - .addSheet(new StatementSheet(con, "select id, name, age from student limit 10").fixSize(10)) + .addSheet(new StatementSheet(con, "select id, name, age, create_date, update_date from student limit 10").fixSize(10)) .writeTo(defaultTestPath); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } } diff --git a/src/test/java/org/ttzero/excel/entity/TemplateTest.java b/src/test/java/org/ttzero/excel/entity/TemplateTest.java index f9d6e834..33ab0503 100644 --- a/src/test/java/org/ttzero/excel/entity/TemplateTest.java +++ b/src/test/java/org/ttzero/excel/entity/TemplateTest.java @@ -31,7 +31,7 @@ */ public class TemplateTest extends WorkbookTest { - @Test public void testTemplate() { + @Test public void testTemplate() throws IOException { try (InputStream fis = Files.newInputStream(testResourceRoot().resolve("template.xlsx"))) { // Map data Map map = new HashMap<>(); @@ -49,8 +49,6 @@ public class TemplateTest extends WorkbookTest { new Workbook("模板导出", author) .withTemplate(fis, map) .writeTo(defaultTestPath); - } catch (IOException | ExcelWriteException e) { - e.printStackTrace(); } } } diff --git a/src/test/java/org/ttzero/excel/entity/csv/ResultSetSheetTest.java b/src/test/java/org/ttzero/excel/entity/csv/ResultSetSheetTest.java index ec979721..defea808 100644 --- a/src/test/java/org/ttzero/excel/entity/csv/ResultSetSheetTest.java +++ b/src/test/java/org/ttzero/excel/entity/csv/ResultSetSheetTest.java @@ -34,7 +34,7 @@ * @author guanquan.wang at 2019-04-28 21:50 */ public class ResultSetSheetTest extends SQLWorkbookTest { - @Test public void testWrite() { + @Test public void testWrite() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -50,26 +50,22 @@ public class ResultSetSheetTest extends SQLWorkbookTest { ) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException|IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor1() { + @Test public void testConstructor1() throws IOException { try { new Workbook("test ResultSet sheet Constructor1") .watch(Print::println) .addSheet(new ResultSetSheet()) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (IOException e) { - e.printStackTrace(); } catch (ExcelWriteException e) { assert true; } } - @Test public void testConstructor2() { + @Test public void testConstructor2() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -80,14 +76,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .addSheet(new ResultSetSheet().setRs(rs)) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor3() { + @Test public void testConstructor3() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -98,14 +90,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .addSheet(new ResultSetSheet("Student").setRs(rs)) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor4() { + @Test public void testConstructor4() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -120,14 +108,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .setRs(rs)) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor5() { + @Test public void testConstructor5() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -142,14 +126,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .setRs(rs)) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor6() { + @Test public void testConstructor6() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -160,14 +140,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .addSheet(new ResultSetSheet(rs)) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor7() { + @Test public void testConstructor7() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -178,14 +154,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { .addSheet(new ResultSetSheet("Student", rs)) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor8() { + @Test public void testConstructor8() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -200,14 +172,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor9() { + @Test public void testConstructor9() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -222,14 +190,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor10() { + @Test public void testConstructor10() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -244,14 +208,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testConstructor11() { + @Test public void testConstructor11() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -266,14 +226,10 @@ public class ResultSetSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } - @Test public void testDiffTypeFromMetadata() { + @Test public void testDiffTypeFromMetadata() throws SQLException, IOException { try ( Connection con = getConnection(); PreparedStatement ps = con.prepareStatement("select id, name, age from student limit 10"); @@ -288,10 +244,6 @@ public class ResultSetSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); - } catch (ExcelWriteException e) { - assert true; } } diff --git a/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java b/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java index 83751f8d..3806410c 100644 --- a/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java +++ b/src/test/java/org/ttzero/excel/entity/csv/StatementSheetTest.java @@ -36,7 +36,7 @@ * @author guanquan.wang at 2019-04-28 22:47 */ public class StatementSheetTest extends SQLWorkbookTest { - @Test public void testWrite() { + @Test public void testWrite() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("statement") .watch(Print::println) @@ -48,12 +48,10 @@ public class StatementSheetTest extends SQLWorkbookTest { ) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testStyleProcessor() { + @Test public void testStyleProcessor() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("statement style processor") .watch(Print::println) @@ -73,12 +71,10 @@ public class StatementSheetTest extends SQLWorkbookTest { ) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testIntConversion() { + @Test public void testIntConversion() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test int conversion statement") .setConnection(con) @@ -98,36 +94,30 @@ public class StatementSheetTest extends SQLWorkbookTest { ) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor1() { + @Test public void testConstructor1() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor1") .watch(Print::println) .addSheet(new StatementSheet(con, "select id, name, age from student limit 10")) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor2() { + @Test public void testConstructor2() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor2") .watch(Print::println) .addSheet(new StatementSheet("Student", con, "select id, name, age from student limit 10")) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor3() { + @Test public void testConstructor3() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor3") .watch(Print::println) @@ -137,12 +127,10 @@ public class StatementSheetTest extends SQLWorkbookTest { })) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor4() { + @Test public void testConstructor4() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor4") .watch(Print::println) @@ -152,12 +140,10 @@ public class StatementSheetTest extends SQLWorkbookTest { })) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor5() { + @Test public void testConstructor5() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor5") .watch(Print::println) @@ -168,12 +154,10 @@ public class StatementSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor6() { + @Test public void testConstructor6() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor6") .watch(Print::println) @@ -184,12 +168,10 @@ public class StatementSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor7() { + @Test public void testConstructor7() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor7") .watch(Print::println) @@ -203,12 +185,10 @@ public class StatementSheetTest extends SQLWorkbookTest { , new Sheet.Column("AGE", int.class))) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor8() { + @Test public void testConstructor8() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor8") .watch(Print::println) @@ -222,26 +202,22 @@ public class StatementSheetTest extends SQLWorkbookTest { , new Sheet.Column("AGE", int.class))) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor9() { + @Test public void testConstructor9() throws IOException { try { new Workbook("test statement sheet Constructor9") .watch(Print::println) .addSheet(new StatementSheet()) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (IOException e) { - e.printStackTrace(); } catch (ExcelWriteException e) { assert true; } } - @Test public void testConstructor10() { + @Test public void testConstructor10() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor10") .watch(Print::println) @@ -249,12 +225,10 @@ public class StatementSheetTest extends SQLWorkbookTest { .setPs(con.prepareStatement("select id, name, age from student limit 10"))) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor11() { + @Test public void testConstructor11() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor11") .watch(Print::println) @@ -262,12 +236,10 @@ public class StatementSheetTest extends SQLWorkbookTest { .setPs(con.prepareStatement("select id, name, age from student limit 10"))) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor12() { + @Test public void testConstructor12() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor12") .watch(Print::println) @@ -275,12 +247,10 @@ public class StatementSheetTest extends SQLWorkbookTest { .setPs(con.prepareStatement("select id, name, age from student limit 10"))) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testConstructor13() { + @Test public void testConstructor13() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet Constructor13") .watch(Print::println) @@ -291,13 +261,11 @@ public class StatementSheetTest extends SQLWorkbookTest { .setPs(con.prepareStatement("select id, name, age from student limit 10"))) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException | IOException e) { - e.printStackTrace(); } } - @Test public void testCancelOddStyle() { + @Test public void testCancelOddStyle() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement sheet cancel odd") .watch(Print::println) @@ -306,12 +274,10 @@ public class StatementSheetTest extends SQLWorkbookTest { ) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testDiffTypeFromMetadata() { + @Test public void testDiffTypeFromMetadata() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test Statement different type from metadata") .watch(Print::println) @@ -322,20 +288,16 @@ public class StatementSheetTest extends SQLWorkbookTest { )) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } - @Test public void testFixWidth() { + @Test public void testFixWidth() throws SQLException, IOException { try (Connection con = getConnection()) { new Workbook("test statement fix width") .watch(Print::println) .addSheet(new StatementSheet(con, "select id, name, age from student limit 10").fixSize(10)) .saveAsCSV() .writeTo(getOutputTestPath()); - } catch (SQLException |IOException e) { - e.printStackTrace(); } } } diff --git a/src/test/java/org/ttzero/excel/reader/ExcelReaderSharedTest.java b/src/test/java/org/ttzero/excel/reader/ExcelReaderSharedTest.java index a5461383..41fefdbe 100644 --- a/src/test/java/org/ttzero/excel/reader/ExcelReaderSharedTest.java +++ b/src/test/java/org/ttzero/excel/reader/ExcelReaderSharedTest.java @@ -29,11 +29,9 @@ */ public class ExcelReaderSharedTest { - @Test public void testSharedRead() { + @Test public void testSharedRead() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("eec shared 100.xlsx"))) { reader.sheets().flatMap(Sheet::rows).forEach(row -> println(row.getRowNumber() + "| " + row.toString())); - } catch (IOException e) { - e.printStackTrace(); } } diff --git a/src/test/java/org/ttzero/excel/reader/ExcelReaderTest.java b/src/test/java/org/ttzero/excel/reader/ExcelReaderTest.java index d0cb915c..a3a9afc1 100644 --- a/src/test/java/org/ttzero/excel/reader/ExcelReaderTest.java +++ b/src/test/java/org/ttzero/excel/reader/ExcelReaderTest.java @@ -65,7 +65,7 @@ public static Path testResourceRoot() { : Paths.get(url.getFile()); } - @Test public void testReader() { + @Test public void testReader() throws IOException { File[] files = testResourceRoot().toFile().listFiles((dir, name) -> name.endsWith(".xlsx")); if (files != null) { for (File file : files) { @@ -74,7 +74,7 @@ public static Path testResourceRoot() { } } - @Test public void testMergedReader() { + @Test public void testMergedReader() throws IOException { File[] files = testResourceRoot().toFile().listFiles((dir, name) -> name.endsWith(".xlsx")); if (files != null) { for (File file : files) { @@ -83,7 +83,7 @@ public static Path testResourceRoot() { } } - @Test public void testFormulaReader() { + @Test public void testFormulaReader() throws IOException { File[] files = testResourceRoot().toFile().listFiles((dir, name) -> name.endsWith(".xlsx")); if (files != null) { for (File file : files) { @@ -92,7 +92,7 @@ public static Path testResourceRoot() { } } - @Test public void testColumnIndex() { + @Test public void testColumnIndex() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { Sheet sheet = reader.sheet(0); for (Iterator it = sheet.iterator(); it.hasNext();) { @@ -102,12 +102,10 @@ public static Path testResourceRoot() { + " | " + row.getLastColumnIndex() + " => " + row); } - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testReset() { + @Test public void testReset() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { Sheet sheet = reader.sheet(0); @@ -119,12 +117,10 @@ public static Path testResourceRoot() { sheet.rows().forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testForEach() { + @Test public void testForEach() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { Sheet sheet = reader.sheet(0); @@ -150,36 +146,28 @@ public static Path testResourceRoot() { } println(); } - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testToStandardObject() { + @Test public void testToStandardObject() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheets().flatMap(Sheet::dataRows).map(row -> row.too(StandardEntry.class)).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testToObject() { + @Test public void testToObject() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheets().flatMap(Sheet::dataRows).map(row -> row.too(Entry.class)).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testToAnnotationObject() { + @Test public void testToAnnotationObject() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheets().flatMap(Sheet::dataRows).map(row -> row.too(AnnotationEntry.class)).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testReaderByName() { + @Test public void testReaderByName() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheet(0).dataIterator().forEachRemaining(row -> { print(row.getInt("渠道ID")); print(" | "); @@ -190,12 +178,10 @@ public static Path testResourceRoot() { print(row.getChar("VIP")); println(); }); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testFilter() { + @Test public void testFilter() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { String[] games = reader.sheet(0) .dataRows() @@ -203,30 +189,24 @@ public static Path testResourceRoot() { .distinct() .toArray(String[]::new); print(Arrays.toString(games)); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testToCSV() { + @Test public void testToCSV() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheet(0).saveAsCSV(getOutputTestPath()); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void test_81() { + @Test public void test_81() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("#81.xlsx"))) { List list = reader.sheets().flatMap(Sheet::dataRows).map(row -> row.to(Customer.class)).collect(Collectors.toList()); for (Customer c : list) System.out.println(c); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testDimension() { + @Test public void testDimension() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("#81.xlsx"))) { Dimension dimension = reader.sheet(0).getDimension(); System.out.println(dimension); @@ -235,8 +215,6 @@ public static Path testResourceRoot() { assert dimension.lastRow == 6; assert dimension.firstColumn == 1; assert dimension.lastColumn == 2; - } catch (IOException e) { - e.printStackTrace(); } } @@ -250,7 +228,7 @@ public static Path testResourceRoot() { assert dimension.lastColumn == 3; } - @Test public void testFormula() { + @Test public void testFormula() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("formula.xlsx"))) { // Read value reader.sheets().flatMap(Sheet::rows).forEach(Print::println); @@ -272,8 +250,6 @@ public static Path testResourceRoot() { } }); } - } catch (IOException e) { - e.printStackTrace(); } } @@ -287,7 +263,7 @@ public static Path testResourceRoot() { print(cellRangeToLong("AA10")); } - @Test public void testClassBind() { + @Test public void testClassBind() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheet(0).bind(Entry.class).dataRows().forEach(row -> { // Use bind...get... @@ -295,12 +271,10 @@ public static Path testResourceRoot() { Entry entry = row.get(); System.out.println(entry.toString()); }); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testClassSharedBind() { + @Test public void testClassSharedBind() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheet(0).bind(Entry.class).dataRows().forEach(row -> { // Use bind...geet... @@ -308,24 +282,20 @@ public static Path testResourceRoot() { Entry entry = row.geet(); System.out.println(entry.toString()); }); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testHeaderString() { + @Test public void testHeaderString() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheets().flatMap(sheet -> { println("----------------" + sheet.getName() + "----------------"); println(sheet.getHeader()); return sheet.dataRows(); }).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testHeaderString2() { + @Test public void testHeaderString2() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { reader.sheets().flatMap(sheet -> { println("----------------" + sheet.getName() + "----------------"); @@ -333,8 +303,6 @@ public static Path testResourceRoot() { println(sheet.getHeader()); return sheet.dataRows(); }).forEach(row -> println((Entry) row.get())); - } catch (IOException e) { - e.printStackTrace(); } } @@ -373,7 +341,7 @@ public static Path testResourceRoot() { assert "B2:B8".equals(values[5]); } - @Test public void testMergeFunc() { + @Test public void testMergeFunc() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("formula.xlsx"))) { // reader.copyOnMergeCells().sheets().flatMap(s -> { // println("----------------" + s.getName() + "----------------"); @@ -381,63 +349,51 @@ public static Path testResourceRoot() { // return s.rows(); // }).forEach(Print::println); reader.copyOnMergeCells().sheets().flatMap(Sheet::rows).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testMergeExcel() { + @Test public void testMergeExcel() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("merge.xlsx"))) { reader.parseFormula().sheets().flatMap(Sheet::rows).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testMergeExcel2() { + @Test public void testMergeExcel2() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("#150.xlsx"))) { reader.sheets().flatMap(Sheet::rows).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } @Ignore - @Test public void testReaderLarge() { + @Test public void testReaderLarge() throws IOException { try (ExcelReader reader = ExcelReader.read(WorkbookTest.getOutputTestPath().resolve("large07.xlsx"))) { long n = reader.sheets().flatMap(Sheet::dataRows).map(row -> row.too(LargeData.class)).count(); assert n == 500000; - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testAllType() { + @Test public void testAllType() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("all type.xlsx"))) { reader.sheets().flatMap(Sheet::dataRows) .map(row -> row.too(ListObjectSheetTest.AllType.class)) .forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testBoxAllType() { + @Test public void testBoxAllType() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("all type.xlsx"))) { reader.sheets().flatMap(Sheet::dataRows) .map(row -> row.too(ListObjectSheetTest.BoxAllType.class)) .forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testNumber2ExcelFormula() { + @Test public void testNumber2ExcelFormula() throws IOException { testFormulaReader(testResourceRoot().resolve("Number2Excel.xlsx")); } - @Test public void testResetToEntry() { + @Test public void testResetToEntry() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("1.xlsx"))) { println("--------0--------"); reader.sheet(0).reset().rows().forEach(Print::println); @@ -469,16 +425,14 @@ public static Path testResourceRoot() { println("--------9--------"); reader.sheet(0).reset().rows().forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - private void testReader(Path path) { + private void testReader(Path path) throws IOException { testReader(path, VALUE_ONLY); } - private void testReader(Path path, int option) { + private void testReader(Path path, int option) throws IOException { println("----------" + path.getFileName() + "----------"); try (ExcelReader reader = ExcelReader.read(path, option)) { println(reader.getAppInfo()); @@ -488,13 +442,11 @@ private void testReader(Path path, int option) { .flatMap(Sheet::rows) .forEach(row -> println(row.getRowNumber() + "|: " + row.toString())); - } catch (IOException e) { - e.printStackTrace(); } } - private void testFormulaReader(Path path) { + private void testFormulaReader(Path path) throws IOException { println("----------" + path.getFileName() + "----------"); try (ExcelReader reader = ExcelReader.read(path, VALUE_AND_CALC)) { // Read formula @@ -508,12 +460,10 @@ private void testFormulaReader(Path path) { } } }); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testToObject2() { + @Test public void testToObject2() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("test-fixed-row.xlsx"))) { reader.sheet(0).rows().forEach(row -> { if (row.getRowNumber() == 1) { @@ -522,24 +472,20 @@ private void testFormulaReader(Path path) { assert "我是内容".equals(row.getString(0)); } }); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testReadEmptyCell() { + @Test public void testReadEmptyCell() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("#169.xlsx"))) { reader.sheets().peek(sheet -> println(sheet.getName() + ": " + sheet.getDimension())).flatMap(Sheet::rows).forEach(Print::println); reader.sheets().peek(sheet -> { sheet.reset(); println(sheet.getName() + ": " + sheet.getDimension()); }).flatMap(Sheet::rows).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void testReadDrawings() { + @Test public void testReadDrawings() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("drawing.xlsx"))) { reader.sheets().peek(sheet -> println(sheet.getName() + ": " + sheet.getDimension())).flatMap(Sheet::rows).forEach(Print::println); @@ -564,12 +510,10 @@ private void testFormulaReader(Path path) { } else assert !sheet.getName().equals("Sheet2") || pictures1.size() == 1; pictures1.forEach(Print::println); }); - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void test175() { + @Test public void test175() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("#175.xlsx"))) { reader.sheet(0).rows().filter(row -> row.getRowNumber() > 7 && !row.isEmpty()).forEach(row -> println(row.getDouble(4))); reader.sheet(0).reset(); @@ -581,8 +525,12 @@ private void testFormulaReader(Path path) { .map(row -> row.to(O.class)) .filter(Objects::nonNull) .forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); + } + } + + @Test public void test177() throws IOException { + try (ExcelReader reader = ExcelReader.read(Paths.get("D:\\wangguanquan3\\Documents\\JD\\office_dongdong\\wangguanquan3\\RecvFile\\采购单批量导入模板 -各种错误.xlsx"))) { + reader.sheets().flatMap(Sheet::dataRows).map(row -> row.to(Goods.class)).forEach(Print::println); } } @@ -1046,4 +994,102 @@ public void setStr25(String str25) { this.str25 = str25; } } + + public static class Goods { + @ExcelColumn("商品编码") + private String no; + @ExcelColumn("商品名称") + private String name; + @ExcelColumn("*品牌") + private String brand; + @ExcelColumn("*订货号") + private String buyNo; + @ExcelColumn("型号") + private String model; + @ExcelColumn("*单位") + private String unit; + @ExcelColumn("税率(不填默认为0)") + private BigDecimal taxRate; + @ExcelColumn("*含税单价(元)") + private BigDecimal price; + @ExcelColumn("*采购数量") + private BigDecimal count; + + public String getNo() { + return no; + } + + public void setNo(String no) { + this.no = no; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getBuyNo() { + return buyNo; + } + + public void setBuyNo(String buyNo) { + this.buyNo = buyNo; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + public BigDecimal getTaxRate() { + return taxRate; + } + + public void setTaxRate(BigDecimal taxRate) { + this.taxRate = taxRate; + } + + public BigDecimal getPrice() { + return price; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public BigDecimal getCount() { + return count; + } + + public void setCount(BigDecimal count) { + this.count = count; + } + + @Override + public String toString() { + return buyNo + " " + price + " " + count; + } + } } diff --git a/src/test/java/org/ttzero/excel/reader/IndexSharedStringTableTest.java b/src/test/java/org/ttzero/excel/reader/IndexSharedStringTableTest.java index 93159732..11453043 100644 --- a/src/test/java/org/ttzero/excel/reader/IndexSharedStringTableTest.java +++ b/src/test/java/org/ttzero/excel/reader/IndexSharedStringTableTest.java @@ -28,7 +28,7 @@ */ public class IndexSharedStringTableTest { - @Test public void test1() { + @Test public void test1() throws IOException { try (IndexSharedStringTable sst = new IndexSharedStringTable()) { sst.push('a'); sst.push('b'); @@ -47,12 +47,10 @@ public class IndexSharedStringTableTest { for (int i = 0; i < n; i++) { println(array[i]); } - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void test2() { + @Test public void test2() throws IOException { try (IndexSharedStringTable sst = new IndexSharedStringTable()) { int length = 10000; String[] buf = new String[length]; @@ -75,12 +73,10 @@ public class IndexSharedStringTableTest { for (int i = 0; i < size; i++) { assert _buf[i].equals(buf[fromIndex + i]); } - } catch (IOException e) { - e.printStackTrace(); } } - @Test public void test3() { + @Test public void test3() throws IOException { try (IndexSharedStringTable sst = new IndexSharedStringTable()) { long start = System.currentTimeMillis(); for (int i = 0; i < 10_000_000; i++) { @@ -94,8 +90,6 @@ public class IndexSharedStringTableTest { sst.get(i); } println(System.currentTimeMillis() - start); - } catch (IOException e) { - e.printStackTrace(); } } // diff --git a/src/test/java/org/ttzero/excel/reader/MultiStyleInCellTest.java b/src/test/java/org/ttzero/excel/reader/MultiStyleInCellTest.java index 8ccb4664..f3683527 100644 --- a/src/test/java/org/ttzero/excel/reader/MultiStyleInCellTest.java +++ b/src/test/java/org/ttzero/excel/reader/MultiStyleInCellTest.java @@ -27,12 +27,10 @@ * @author guanquan.wang at 2019-06-12 17:26 */ public class MultiStyleInCellTest { - @Test public void testMulti() { + @Test public void testMulti() throws IOException { try (ExcelReader reader = ExcelReader.read(testResourceRoot().resolve("multi-style In cell.xlsx"))) { reader.sheets().flatMap(Sheet::rows).forEach(Print::println); - } catch (IOException e) { - e.printStackTrace(); } } } diff --git a/src/test/resources/all type.xlsx b/src/test/resources/all type.xlsx index 2dd93dc69efb5790cbc4609becda79d164e18224..56e2d2faf28a0e03442f454e08f1fe9fed903518 100644 GIT binary patch literal 10561 zcmeHtg;yL~_H{!8!QHKKcPF?7ZyZ8!X@Xl5B)A6%P67l7!Gi^N2p-&n2Mdtk?)vMz zH#3>Xyzl!5W~$e!TD7{*?z;D$ea@+~uevf20Uv+_Kn4H+)Bq{Lpb6*A?5a!YihtKy>gKM-T=?;}mrJW{EjGv~0Sxu>Bgs5-jptW zB_TuK!t$dC{&Hqptgk~KUYYp?vAK|EuqHdH(xP5-vjz{ZE?rp~ltPCi={%Ylze2sC z`U<4D)jy8^!KlKTzT9H@rZ@N_3aav zl9etT_sP@Avs8H{FDCbvSeD}Yk6FrnOSDfW4rME_M>zCIQSkD}!wAJv1D}0S)n7Ef z_yjj8t+iVkR$eFgVJmSY&42oB{w9XRD`AzL@l=9dsQL5RPd>f&)TfuEI$GAkHs$8; zT_4kUnONGl9m}S*;@$eNgVTF8Y4~t2ct+%V=s%qLKda_D9LVtM5=7}N?mg%WiUNIH zgk`M%BuSunQt~luMjBxxAq61AdD?US$rBGJS37ejC%a$S?4O*0gQYYW%76D!qN%Ff z1q9#vu>O;J38A~$bHq!^92{FvW? zc&F{GA0u%QUgFvBiy%4;9S;GK&38*FgX9tMaCf%0i+9m5-$C&j#1nei-s21XxL~3u z3D3!=kv(Y`596~P=6bqWcV8S+u{jg_j`uw8r375X}Pu`*>5znD*$8&!C(JjkCp0lJ=@ z+N86;;wF9F&)hnH#5QoUZ?)FfGr0s?>Hl<@qX5h78JHW?0|5Xm*e_r%^Y6}*o2VK) z!G-;cQ;!DD9mJoa$|~xXqh%9mA69txLAF$Ii|=~pFm3L|5<{f3lI6&8M`jKaQ_pLT z(}Hz(L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c z?` z)ANqt@8{j$koR|r`7hurC-Ve+g^nLWnVnOeu+bMBn8H43lnau+nkukN^&h^P@U|&a zbn5aA(#7bGzFaZ52&%M0BS|ho)fHBYCmm1KcV7j%SY+6c2%}lp{6yoUO^*JixO$*x zJD&C!y-yK>e45wEGC&Rd!cD}1O_nnj$C;;O?^Pf#VKv$;+C(=5_{fN_sK+9ns}fZ9 zjV+IjM~sR`Pc0^fGzGZk?VoMs?l+*nB6)rnq`?S3IJQk89MToGl6}>9+ntnrn~CRIXZMrqIa(mr;=-+`STy}bdm*8) zAbw5zexE!&_Xq!O|Ih4CQRd$l_B{L4@Qe>wjG#DBW} z&X=B<*)Ibk{_XlFLHl!s-&?(B4)x1$pCa%tT>l?b>d#evPoRI7ee~(}|7jBcT;TUJ k^_*wFto`ZsX@UQeb&4{uPk9Rgf%x?L@N^yz2%qo%2aq6!3jhEB From 5e0744205f59b4737546ca958ef7085f2fabdb90 Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 17:53:39 +0800 Subject: [PATCH 7/8] Test case --- .../ttzero/excel/entity/SQLWorkbookTest.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/ttzero/excel/entity/SQLWorkbookTest.java b/src/test/java/org/ttzero/excel/entity/SQLWorkbookTest.java index 4f520cba..3cdf2ef2 100644 --- a/src/test/java/org/ttzero/excel/entity/SQLWorkbookTest.java +++ b/src/test/java/org/ttzero/excel/entity/SQLWorkbookTest.java @@ -18,6 +18,7 @@ package org.ttzero.excel.entity; import org.junit.Before; +import org.ttzero.excel.util.StringUtil; import java.io.IOException; import java.sql.Connection; @@ -25,13 +26,16 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.Types; import java.util.Properties; /** * @author guanquan.wang at 2019-04-28 21:50 */ public class SQLWorkbookTest extends WorkbookTest { - private static Properties pro; + private static final Properties pro; + private static String protocol; static { pro = new Properties(); try { @@ -46,10 +50,22 @@ public class SQLWorkbookTest extends WorkbookTest { e.printStackTrace(); System.exit(-1); } + + String url = pro.getProperty("dataSource.url"); + if (StringUtil.isNotEmpty(url)) { + int i1 = url.indexOf(':'), i2 = url.indexOf(':', ++i1); + if (i2 > i1 && i1 > 0) { + protocol = url.substring(i1, i2); + } + } + if (StringUtil.isEmpty(protocol)) { + throw new IllegalArgumentException("dataSource.url"); + } } protected Connection getConnection() throws SQLException { - return DriverManager.getConnection(pro.getProperty("dataSource.url")); + return DriverManager.getConnection(pro.getProperty("dataSource.url") + , pro.getProperty("dataSource.username"), pro.getProperty("dataSource.password")); } /** @@ -61,7 +77,7 @@ protected Connection getConnection() throws SQLException { ResultSet rs = null; try { con = getConnection(); - String student = "create table if not exists student(id integer primary key, name text, age integer)"; + String student = "create table if not exists student(id integer " + ("sqlite".equals(protocol) ? "" : "auto_increment") + " primary key, name text, age integer, create_date timestamp, update_date timestamp)"; ps = con.prepareStatement(student); ps.executeUpdate(); ps.close(); @@ -72,11 +88,21 @@ protected Connection getConnection() throws SQLException { if (!rs.next()) { ps.close(); con.setAutoCommit(false); - ps = con.prepareStatement("insert into student(name, age) values (?,?)"); + ps = con.prepareStatement("insert into student(name, age, create_date, update_date) values (?,?,?,?)"); int size = 10_000; for (int i = 0; i < size; i++) { ps.setString(1, getRandomString()); - ps.setInt(2, random.nextInt(15) + 5); + if (random.nextInt(1000) >= 975) { + ps.setNull(2, Types.INTEGER); + } else { + ps.setInt(2, random.nextInt(15) + 5); + } + ps.setTimestamp(3, new Timestamp(System.currentTimeMillis())); + if (random.nextInt(1000) >= 615) { + ps.setNull(4, Types.DATE); + } else { + ps.setTimestamp(4, new Timestamp(System.currentTimeMillis() - random.nextInt(9999999))); + } ps.addBatch(); } ps.executeBatch(); From 3e980e21e1a17141378b4bf4278dd251d9106f02 Mon Sep 17 00:00:00 2001 From: wangguanquan3 Date: Thu, 13 May 2021 17:54:15 +0800 Subject: [PATCH 8/8] Upgrade junit version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ae65fe57..2fffea57 100644 --- a/pom.xml +++ b/pom.xml @@ -43,7 +43,7 @@ junit junit - 4.13.1 + ${junit.version} test