Search in sources :

Example 1 with NumberCell

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;
}
Also used : NumberCell(jxl.NumberCell) DataSet(cn.cerc.jdb.core.DataSet) Workbook(jxl.Workbook) WritableWorkbook(jxl.write.WritableWorkbook) FileItem(org.apache.commons.fileupload.FileItem) WritableSheet(jxl.write.WritableSheet) Sheet(jxl.Sheet) Cell(jxl.Cell) NumberCell(jxl.NumberCell)

Aggregations

DataSet (cn.cerc.jdb.core.DataSet)1 Cell (jxl.Cell)1 NumberCell (jxl.NumberCell)1 Sheet (jxl.Sheet)1 Workbook (jxl.Workbook)1 WritableSheet (jxl.write.WritableSheet)1 WritableWorkbook (jxl.write.WritableWorkbook)1 FileItem (org.apache.commons.fileupload.FileItem)1