use of jxl.NumberCell in project summer-bean by cn-cerc.
the class ImportExcel method readFileData.
public DataSet readFileData(Record record) throws Exception {
FileItem file = this.getFile(record);
// 获取Excel文件对象
Workbook rwb = Workbook.getWorkbook(file.getInputStream());
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
Template template = this.getTemplate();
if (template.getColumns().size() != sheet.getColumns())
throw new RuntimeException(String.format("导入的文件:<b>%s</b>, 其总列数为 %d,而模版总列数为 %d 二者不一致,无法导入!", file.getName(), sheet.getColumns(), template.getColumns().size()));
DataSet ds = new DataSet();
for (int row = 0; row < sheet.getRows(); row++) {
if (row == 0) {
for (int col = 0; col < sheet.getColumns(); col++) {
Cell cell = sheet.getCell(col, row);
String value = cell.getContents();
String title = template.getColumns().get(col).getName();
if (!title.equals(value))
throw new RuntimeException(String.format("导入的文件:<b>%s</b>,其标题第 %d 列为【 %s】, 模版中为【%s】,二者不一致,无法导入!", file.getName(), col + 1, value, title));
}
} else {
ds.append();
for (int col = 0; col < sheet.getColumns(); col++) {
Cell cell = sheet.getCell(col, row);
String value = cell.getContents();
if (cell.getType() == CellType.NUMBER) {
NumberCell numberCell = (NumberCell) cell;
double d = numberCell.getValue();
value = formatFloat("0.######", d);
}
Column column = template.getColumns().get(col);
if (!column.validate(row, col, value)) {
ColumnValidateException err = new ColumnValidateException("其数据不符合模版要求,当前值为:" + value);
err.setTitle(column.getName());
err.setValue(value);
err.setCol(col);
err.setRow(row);
if (errorHandle == null || !errorHandle.process(err))
throw err;
}
ds.setField(column.getCode(), value);
}
if (readHandle != null && !readHandle.process(ds.getCurrent()))
break;
}
}
return ds;
}
Aggregations