Exemplo n.º 1
0
 /**
  * 默认的字典输出适配器
  *
  * @param fieldValue
  * @param fieldName
  * @param cell
  * @throws AdapterException
  */
 public void outputDicCodeAdapter(
     DataBean dataBean, Object fieldValue, String fieldName, Cell cell) throws AdapterException {
   log.debug(
       "in DefaultOutputAdapter:outputDicCodeAdapter fieldName:{} fieldValue:{}",
       fieldName,
       fieldValue);
   OutputDicConfig config = dataBean.getOutputConfig(fieldName);
   String dicCode = config.dicCode();
   if (fieldValue == null) {
     log.debug("fieldValue is null return");
     cell.setCellValue("");
     return;
   } else {
     String byKey = dicCodePool.getByKey(dicCode, fieldValue.toString());
     if (byKey == null) {
       if (AdapterUtil.getAllMatch(config)) {
         throw new AdapterException(Message.DIC_ERROR, cell);
       } else {
         cell.setCellValue(fieldValue.toString());
       }
     } else {
       cell.setCellValue(byKey);
     }
   }
 }
Exemplo n.º 2
0
 /**
  * 导出时间适配器
  *
  * @param fieldValue
  * @param fieldName
  * @return
  * @throws AdapterException
  */
 public void outputDateAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
     throws AdapterException {
   log.debug(
       "in DefaultOutputAdapter:outputDateAdapter fieldName:{} fieldValue:{}",
       fieldName,
       fieldValue);
   Date date = null;
   if (fieldValue == null) {
     log.debug("fieldValue is null return");
     cell.setCellValue("");
     return;
   } else if (fieldValue instanceof Date) {
     log.debug("fieldValue instanceof Date ");
     date = (Date) fieldValue;
   } else if (fieldValue instanceof String) {
     log.debug("fieldValue instanceof String ");
     InputDateConfig config = dataBean.getInputConfig(fieldName);
     try {
       date = DateUtil.formatToDate((String) fieldValue, config.format());
     } catch (ParseException e) {
       throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
     }
   } else if (fieldValue instanceof Long) {
     log.debug("fieldValue instanceof Long ");
     date = new Date((Long) fieldValue);
   } else {
     throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
   }
   Workbook workbook = cell.getSheet().getWorkbook();
   OutputDateConfig outputConfig = dataBean.getOutputConfig(fieldName);
   CellStyle cellStyle = cell.getCellStyle();
   if (cellStyle == null) cellStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();
   cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(outputConfig.format()));
   cell.setCellStyle(cellStyle);
   cell.setCellValue(date);
 }
Exemplo n.º 3
0
 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);
 }