I'm coding a Excel import logic:
public class ExcelImporter {
//...ignore constructor and other methods ...
public <R> Builder<R> sheet(String sheetName, RowConsumer<R> rowConsumer) {
return new Builder<R>(sheetName, rowConsumer);
}
public class Builder<R> {
//...ignore other method
public <F> Builder header(String name, CellConsumer<R, F> cellConsumer) {
sheetReader.header(new DefaultHeader<>(name, cellConsumer));
return this;
}
public <F> Builder header(String name, Class<F> fieldType, CellConsumer<R, String> cellConsumer) {
return header(name,cellConsumer);
}
}
}
And on my test code I got compile error:
@Test
public void processSmallExcelWithConsumer() throws Exception {
try (InputStream is = getClass().getClassLoader().getResourceAsStream("工作簿1.xls")) {
ExcelImporter excelImporter = new ExcelImporter(is, "application/vnd.ms-excel")
.sheet("Sheet1", () -> new RowBean())
.header("姓名",String.class, (cell, row) -> row.setName(cell)) // no error
.header("性别",String.class, (cell, row) -> row.setSex(cell)) // "setSex" got error and `row` evaluate to `Object` ? why !?
.build();
setSex() can not compile,and that row evaluate to Object, I'm so confusion that it works well at first time but fails next time?
This is CellConsumer:
@FunctionalInterface
public interface CellConsumer<R,F> {
void read(F cell,R row);
}
And RowConsumer:
@FunctionalInterface
public interface RowConsumer<R> {
R newRow();
}
And error:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.797 s
[INFO] Finished at: 2017-07-28T23:56:26+08:00
[INFO] Final Memory: 17M/166M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:testCompile (default-testCompile) on project graceful-excel: Compilation failure
[ERROR] /home/terrason/workspace/maven/cnx/graceful-excel/src/test/java/cn/lenyar/excel/ExcelImporterTest.java:[81,66] 找不到符号
[ERROR] 符号: 方法 setSex(java.lang.Object)
[ERROR] 位置: 类型为java.lang.Object的变量 row
[ERROR] -> [Help 1]
Help me please!
CellConsumerinterface.setNameandsetSexon? And what isRowBean?