Beispiel #1
0
 /**
  * 读取内容到List,可以是List<Map>也可以是List<Object>
  *
  * @param sortedColumns 从第0列开始,数组中每个元素对应type中的一个字段,元素为空则忽略
  * @param type 读取数据存放的类型
  * @param <T> 泛型
  * @return
  */
 public <T> List<T> read(String[] sortedColumns, Class<? extends T> type) {
   List<T> list = new ArrayList();
   for (int i = startRow; i < tableBean.getRowCount(); i++) {
     Object bean =
         Map.class.isAssignableFrom(type) ? new HashMap() : Reflections.instance(type.getName());
     for (int j = 0; j < sortedColumns.length; j++) {
       String column = sortedColumns[j];
       if (ObjectHelper.isNotEmpty(column)) {
         CellBean cellBean = tableBean.getCellBean(i, j);
         if (cellBean != null) {
           Cell cell = cellBean.getCell();
           if (cell == null) {
             cell = sheet.getRow(i).createCell(j);
           }
           if (bean instanceof Map) {
             FieldUtils.setFieldValue(bean, "cell" + column, cell);
             if (ObjectHelper.isNotEmpty(cellBean.getContent())) {
               FieldUtils.setFieldValue(bean, column, cellBean.getContent());
             }
           } else {
             if (bean instanceof BaseExcelVo) {
               BaseExcelVo baseExcelVo = ((BaseExcelVo) bean);
               baseExcelVo.setCell(column, cell);
               baseExcelVo.setRow(i);
               baseExcelVo.setCol(j);
             }
             Field accessibleField = Reflections.getAccessibleField(bean, column);
             Class<?> fieldType = accessibleField.getType();
             Object value = cellBean.getContent();
             if (!fieldType.equals(String.class)) {
               value = ConverString.asType(fieldType, cellBean.getContent());
             }
             if (ObjectHelper.isNotEmpty(cellBean.getContent())) {
               FieldUtils.setFieldValue(bean, column, value);
             }
           }
         }
       }
     }
     list.add((T) bean);
   }
   return list;
 }
 public void outputIntAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
     throws AdapterException {
   log.debug(
       "in DefaultOutputAdapter:outputIntAdapter fieldName:{} fieldValue:{}",
       fieldName,
       fieldValue);
   if (ObjectHelper.isNullOrEmptyString(fieldValue)) return;
   Workbook workbook = cell.getSheet().getWorkbook();
   CellStyle cellStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();
   cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#"));
   cell.setCellValue(NumberUtils.format(fieldValue, 0));
   cell.setCellStyle(cellStyle);
 }
 public void outputNumericAdapter(
     DataBean dataBean, Object fieldValue, String fieldName, Cell cell) throws AdapterException {
   log.debug(
       "in DefaultOutputAdapter:outputNumericAdapter fieldName:{} fieldValue:{}",
       fieldName,
       fieldValue);
   if (ObjectHelper.isNullOrEmptyString(fieldValue)) return;
   OutputNumericConfig config = dataBean.getOutputConfig(fieldName);
   Workbook workbook = cell.getSheet().getWorkbook();
   CellStyle cellStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();
   StringBuilder format = new StringBuilder("0");
   for (int i = 0; i < config.floatCount(); i++) {
     if (i == 0) format.append(".");
     format.append("0");
   }
   cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(format.toString()));
   cell.setCellValue(NumberUtils.format(fieldValue, config.floatCount()));
   cell.setCellStyle(cellStyle);
 }