EasyExcel文档链接与使用示例

文档链接

注解
https://blog.csdn.net/estelle_belle/article/details/134508223

官方文档地址
https://github.com/alibaba/easyexcel/tree/master?tab=readme-ov-file

使用示例

依赖版本

<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>easyexcel</artifactId>
        <version>3.3.1</version>
    </dependency>

业务层

   /**
     * 导出费用信息列表
     *
     * @param dto
     * @param response
     */
    @Override
    public void exportCostinfo(ExportCostinfoDTO dto, HttpServletResponse response) {
        List<Long> ids = dto.getIds();

        // 导出文件
        try {
            String fileName = new String("费用信息记录.xls".getBytes("GBK"), StandardCharsets.ISO_8859_1);
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            OutputStream outputStream = response.getOutputStream();


            try (ExcelWriter excelWriter = EasyExcel.write(outputStream, CostinfoListVO.class).registerWriteHandler(new AutoIncrementNumberRowWriteHandler()).build()) {
                WriteSheet writeSheet = EasyExcel.writerSheet("记录").build();

                if (ids != null && ids.size() > 0) {
                    //获取数据
                    List<CostinfoListVO> exportList = convertResult(dto, ids);
                    //导出 大数据量时可循环写入
                    excelWriter.write(exportList, writeSheet);
                } else {
                    dto.setPageNum(1L);
                    dto.setPageSize(1L);
                    PageParam<CostinfoDO, ListCostinfoDTO> page = new PageParam<>(dto);
                    List<CostinfoListVO> list = list(page, dto);
                    PageResult<CostinfoListVO> pageResult = new PageResult(list, page);
                    Long total = pageResult.getTotal();

                    long loop = total / 1000 + (total % 1000 != 0 ? 1 : 0);
                    for (long i = 0; i < loop; i++) {
                        dto.setPageNum(i + 1);
                        dto.setPageSize(Long.valueOf(1000));
                        List<CostinfoListVO> exportList = convertResult(dto, ids);
                        excelWriter.write(exportList, writeSheet);
                    }
                }
                //添加序号,添加空行数据
                excelWriter.write(Arrays.asList(new CostinfoListVO()), writeSheet);
                excelWriter.finish();
            }

            response.flushBuffer();
            // 写完数据关闭流
            outputStream.close();
        } catch (IOException e) {
            log.error("exportCostinfo 导出error! ", e);
        }
    }


  /**
     * 处理数据为展示值
     *
     * @param dto 数据来源
     * @param ids 选择的数据
     * @return
     */
    private List<CostinfoListVO> convertResult(ExportCostinfoDTO dto, List<Long> ids) {

        List<CostinfoListVO> exportList = new ArrayList<>();

        if (ids != null && ids.size() > 0) {
            //导出选择数据
            List<CostinfoListVO> idsList = costinfoMapper.exportIds(ids);
            for (int i = 0; i < idsList.size(); i++) {
                CostinfoListVO vo = idsList.get(i);
                if (ids.contains(Long.valueOf(vo.getId()))) {
                    try {
                        vo.setPayMethod(CosttypeEnumInterface.PayMethod.getMethod(StringUtils.defaultString(vo.getPayMethod())));
                        vo.setInvoiceType(CosttypeEnumInterface.InvoiceType.getInvoiceType(StringUtils.defaultString(vo.getInvoiceType())));
                        vo.setStatus(CosttypeEnumInterface.CosttypePayerAndReceiveEnum.getStatusName(Short.parseShort(vo.getCosttype()), Integer.parseInt(vo.getStatus())));
                    } catch (Exception e) {
                        log.error("枚举转换异常", e);
                    }

                    exportList.add(vo);
                }
            }
        } else {
            //导出全部数据
            PageParam<CostinfoDO, ListCostinfoDTO> page = new PageParam<>(dto);
            List<CostinfoListVO> list = list(page, dto);
            for (int i = 0; i < list.size(); i++) {
                CostinfoListVO vo = list.get(i);
                try {
                    vo.setPayMethod(CosttypeEnumInterface.PayMethod.getMethod(StringUtils.defaultString(vo.getPayMethod())));
                    vo.setInvoiceType(CosttypeEnumInterface.InvoiceType.getInvoiceType(StringUtils.defaultString(vo.getInvoiceType())));
                    vo.setStatus(CosttypeEnumInterface.CosttypePayerAndReceiveEnum.getStatusName(Short.parseShort(vo.getCosttype()), Integer.parseInt(vo.getStatus())));

                } catch (Exception e) {
                    log.error("枚举转换异常", e);
                }
                exportList.add(vo);
            }
        }
        return exportList;
    }

展示类

package net.cnki.editor.costcenter.pojo.vo.costinfo;

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import groovy.transform.EqualsAndHashCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 费用信息类别VO
 */
@Data
@EqualsAndHashCode
@ExcelIgnoreUnannotated //没有注解的列不导出
public class CostinfoListVO {
    @ApiModelProperty(value = "ID: 费用信息表id")
    @ExcelProperty(" 序号") //导出列标题
    private String id;

    @ApiModelProperty(value = "稿件id")
    private String paperId;

    @ApiModelProperty(value = "工作流环节实例id")
    private String taskInstId;

    @ApiModelProperty(value = "稿号")
    @ExcelProperty(" 稿号")
    private String paperNum;

    @ApiModelProperty(value = "稿件标题")
    @ExcelProperty(" 稿件标题")
    private String chineseTitle;

    @ApiModelProperty(value = "投稿作者")
    @ExcelProperty(" 投稿作者")
    private String authorName;

    @ApiModelProperty(value = "投稿作者ID")
    private String authorID;

    @ApiModelProperty(value = "实际发生金额")
    @ExcelProperty(" 实际发生金额")
    private String realAmount;

    @ApiModelProperty(value = "结算方式: 结算方式 0 第三方转账 1 邮局汇款 2银行汇款 3现金支付 4内部转账 5其他")
    @ExcelProperty(" 结算方式")
    private String payMethod;

    @ApiModelProperty(value = "发票类型 0 普票 1 专票")
    @ExcelProperty(" 发票类型")
    private String invoiceType;

    @ApiModelProperty(value = "支付日期")
    @ExcelProperty(" 支付日期")
    private String payTime;

    @ApiModelProperty(value = "费用信息状态(不同费用类型状态不同)")
    @ExcelProperty(" 费用信息状态")
    private String status;

    @ApiModelProperty(value = "费用性质( 1 审稿费; 2 版面费; 3 作者稿费; 4 专家审稿费; 5 编辑稿费;)")
    private String costtype;

}

拦截器

package net.cnki.editor.costcenter.util.easyexcel;

import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.handler.RowWriteHandler;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.Map;

/**
 * easyexcel 导出自增序号处理
 */
public class AutoIncrementNumberRowWriteHandler implements RowWriteHandler {
    /*
     * 注: 导出模板类中需要设置序号列;
     * 注: 此版本easyexcel 在导出拦截时会丢失最后一行数据,所有需要在导出list中手动添加一行空数据;
     * */
    private int number = 1; // 序号计数器

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        try {
            int rowNum = row.getRowNum();
            if (rowNum > 1) {
                Sheet sheet = row.getSheet();
                Row row1 = sheet.getRow(number++);
                Cell cell = row1.getCell(0);
                cell.setCellValue(number - 1);
            }
        } catch (Exception e) {

        }

    }


}
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IMER); LL_TIM_EnableCounter(TIMER); while (LL_TIM_GetCounter(TIMER) < ticks) {} LL_TIM_DisableCounter(TIMER); } uint32_t measureDistance(void) { LL_GPIO_ResetOutputPin(GPIOA, TRIG_PIN); delayUs(2); LL_GPIO_SetOutputPin(GPIOA, TRIG_PIN); delayUs(10); LL_GPIO_ResetOutputPin(GPIO下面是一个使用EasyExcel导出多个sheet的示例代码: ```java public void exportExcelWithMultipleSheets(HttpA, TRIG_PIN); while (!LL_GPIO_IsInputPinSet(GPIOA, ECHO_PIN)) {} LL_TIM_EnableCounterServletResponse response) { try { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName(TIMER); while (LL_GPIO_IsInputPinSet(GPIOA, ECHO_PIN)) {} LL_TIM_DisableCounter(TIMER); = URLEncoder.encode("多个Sheet的Excel文件", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename uint32_t elapsed = LL_TIM_GetCounter(TIMER); float distance = (elapsed * 0.0343) / =" + fileName + ".xlsx"); OutputStream outputStream = response.getOutputStream(); ExcelWriter excelWriter = EasyExcel.write(outputStream).build(); 2.0; return (uint32_t)distance; } int main(void) { LL_GPIO_InitTypeDef GPIO_InitStruct = {0}; LL_TIM_InitTypeDef TIM_InitStruct = {0}; LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM // 写入第一个Sheet WriteSheet sheet1 = EasyExcel.writerSheet(0, "Sheet1").head(Student.class).2); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); GPIO_InitStruct.Pin = TRIG_PIN; build(); List<Student> students1 = getStudents1(); // 获取第一个Sheet的数据 excelWriter.write(students1, GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT; GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = ECHO_PIN; GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT; GPIO_InitStruct.Pull = LL sheet1); // 写入第二个Sheet WriteSheet sheet2 = EasyExcel.writerSheet(1, "Sheet2")._GPIO_PULL_NO; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); TIM_InitStruct.Prescaler = 0; TIM_InitStruct.Counterhead(Teacher.class).build(); List<Teacher> teachers = getTeachers(); // 获取第二个Sheet的数据 excelWriterMode = LL_TIM_COUNTERMODE_UP; TIM_InitStruct.Autoreload = 0xFFFFFFFF; LL_TIM_Init(TIMER, &TIM.write(teachers, sheet2); // 关闭ExcelWriter excelWriter.finish(); outputStream.flush(); outputStream.close(); _InitStruct); LL_TIM_EnableIT_UPDATE(TIMER); while (1) { uint32_t distance = measureDistance(); } } void TIM2_IRQHandler(void) { if (LL_TIM_IsActiveFlag_UPDATE(TIMER) == 1) { LL_TIM } catch (IOException e) { e.printStackTrace(); } } private List<Student> getStudents1() { // 获取第一个Sheet_ClearFlag_UPDATE(TIMER); } } ``` 3. 与第一个示例类似,但使用 HAL 库和 DMA 来的数据 // ... } private List<Teacher> getTeachers() { // 获取第二个Sheet的数据 // ... 读取定时器计数器的值。 在 CubeMX 中选择 STM32F103VCT6 芯片,然后按} ``` 代码中首先设置了响应头,然后创建一个ExcelWriter实例。接着分别创建了两照以下步骤进行配置: - 在 Pinout 标签页中,将 PA0 配置为 GPIO_Output,将 PA1 配个WriteSheet实例,分别用于写入两个Sheet的数据。最后通过ExcelWriter将数据写入Excel文件置为 GPIO_Input。 - 在 Configuration 标签页中,选择 SYSCLK 为 HSE,选择 HSE 值为 8 MHz,并关闭ExcelWriter。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值