Java Code Examples for com.ruoyi.common.annotation.Excel#dateFormat()

The following examples show how to use com.ruoyi.common.annotation.Excel#dateFormat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExcelUtil.java    From supplierShop with MIT License 5 votes vote down vote up
/**
 * 添加单元格
 */
public Cell addCell(Excel attr, Row row, T vo, Field field, int column)
{
    Cell cell = null;
    try
    {
        // 设置行高
        row.setHeight((short) (attr.height() * 20));
        // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
        if (attr.isExport())
        {
            // 创建cell
            cell = row.createCell(column);
            cell.setCellStyle(styles.get("data"));

            // 用于读取对象中的属性
            Object value = getTargetValue(vo, field, attr);
            String dateFormat = attr.dateFormat();
            String readConverterExp = attr.readConverterExp();
            if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value))
            {
                cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
            }
            else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value))
            {
                cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
            }
            else
            {
                // 设置列类型
                setCellVo(value, attr, cell);
            }
        }
    }
    catch (Exception e)
    {
        log.error("导出Excel失败{}", e);
    }
    return cell;
}
 
Example 2
Source File: ExcelUtil.java    From RuoYi with Apache License 2.0 5 votes vote down vote up
/**
 * 添加单元格
 */
private void addCell(Excel attr, Row row, T vo, Field field, int column, CellStyle cs){
    Cell cell;
    try{
        // 设置行高
        row.setHeight((short) (attr.height() * 20));
        // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
        if (attr.isExport()){
            // 创建cell
            cell = row.createCell(column);
            cell.setCellStyle(cs);

            // 用于读取对象中的属性
            Object value = getTargetValue(vo, field, attr);
            String dateFormat = attr.dateFormat();
            String readConverterExp = attr.readConverterExp();
            if (StrUtil.isNotEmpty(dateFormat) && ObjectUtil.isNotNull(value)){
                cell.setCellValue(DateUtil.format(((Date) value), dateFormat));
            }else if (StrUtil.isNotEmpty(readConverterExp) && ObjectUtil.isNotNull(value)){
                cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
            }else{
                cell.setCellType(CellType.STRING);
                // 如果数据存在就填入,不存在填入空格.
                cell.setCellValue(ObjectUtil.isNull(value) ? attr.defaultValue() : value + attr.suffix());
            }
        }
    }catch (Exception e){
        log.error("导出Excel失败%s", e);
    }
}
 
Example 3
Source File: ExcelUtil.java    From ruoyiplus with MIT License 4 votes vote down vote up
/**
 * 填充excel数据
 * 
 * @param index 序号
 * @param row 单元格行
 * @param cell 类型单元格
 */
public void fillExcelData(int index, Row row, Cell cell)
{
    int startNo = index * sheetSize;
    int endNo = Math.min(startNo + sheetSize, list.size());
    // 写入各条记录,每条记录对应excel表中的一行
    CellStyle cs = wb.createCellStyle();
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    for (int i = startNo; i < endNo; i++)
    {
        row = sheet.createRow(i + 1 - startNo);
        // 得到导出对象.
        T vo = (T) list.get(i);
        for (int j = 0; j < fields.size(); j++)
        {
            // 获得field.
            Field field = fields.get(j);
            // 设置实体类私有属性可访问
            field.setAccessible(true);
            Excel attr = field.getAnnotation(Excel.class);
            try
            {
                // 设置行高
                row.setHeight((short) (attr.height() * 20));
                // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
                if (attr.isExport())
                {
                    // 创建cell
                    cell = row.createCell(j);
                    cell.setCellStyle(cs);
                    if (vo == null)
                    {
                        // 如果数据存在就填入,不存在填入空格.
                        cell.setCellValue("");
                        continue;
                    }

                    // 用于读取对象中的属性
                    Object value = getTargetValue(vo, field, attr);
                    String dateFormat = attr.dateFormat();
                    String readConverterExp = attr.readConverterExp();
                    if (StringUtils.isNotEmpty(dateFormat))
                    {
                        cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
                    }
                    else if (StringUtils.isNotEmpty(readConverterExp))
                    {
                        cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
                    }
                    else
                    {
                        cell.setCellType(CellType.STRING);
                        // 如果数据存在就填入,不存在填入空格.
                        cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
                    }
                }
            }
            catch (Exception e)
            {
                log.error("导出Excel失败{}", e.getMessage());
            }
        }
    }
}