java Excel操作相关工具类

导包

		<!-- 阿里巴巴Excel工具组件 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.0.4</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

工具类

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

/**
 * Excel操作相关工具类
 *
 * @author ycs
 */
public class YcsExcelUtils {
    /**
     * 将 Excel 文件流解析成二维行列 List
     *
     * @param inputStream 输入流
     * @return
     */
    public static List<List<String>> parseToContent(InputStream inputStream) {
        List<List<String>> result = new ArrayList<>();
        EasyExcel.read(inputStream, new ContentListener(result)).sheet().doRead();
        return result;
    }

    /**
     * 将 Excel 文件流解析成 List 对象
     *
     * @param inputStream 输入流
     * @param <T>         泛型,使用时传具体对象
     * @param type        具体对象.class
     * @return
     */
    public static <T> List<T> parseToObject(InputStream inputStream, Class<T> type) {
        List<T> result = new ArrayList<>();
        EasyExcel.read(inputStream, type, new ObjectListener(result)).sheet().doRead();
        return result;
    }

    /**
     * 将对象List 写到 Excel 输出流
     *
     * @param type         对象
     * @param data         对象List
     * @param outputStream 输出流
     * @param <T>
     */
    public static <T> void objectToExcel(Class<?> type, List<T> data, OutputStream outputStream) {
        EasyExcel.write(outputStream, type).sheet().doWrite(data);
    }

    /**
     * 将二维List 写到 Excel 输出流
     *
     * @param head         Excel头
     * @param data         内容
     * @param outputStream 输出流
     */
    public static void contentToExcel(List<List<String>> head, List<List<Object>> data, OutputStream outputStream) {
        EasyExcel.write(outputStream).head(head).sheet().doWrite(data);
    }
}

class ObjectListener<T> extends AnalysisEventListener<T> {

    private final List<T> result;

    public ObjectListener(List<T> result) {
        this.result = result;
    }

    @Override
    public void invoke(T t, AnalysisContext analysisContext) {
        result.add(t);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}

class ContentListener extends AnalysisEventListener<Map<Integer, String>> {
    private final List<List<String>> result;

    public ContentListener(List<List<String>> result) {
        this.result = result;
    }

    @Override
    public void invoke(Map<Integer, String> rowMap, AnalysisContext analysisContext) {
        Set<Integer> colIndexSet = rowMap.keySet();
        List<String> rowList = new ArrayList<>();
        for (int colIndex : colIndexSet) {
            rowList.add(rowMap.get(colIndex));
        }
        result.add(rowList);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}

测试

/**
 * @author ycs
 */
public class SelfTest {
    private File desktopDir;
    /**
     * 系统桌面路径
     */
    private String desktopPath;

    @Before
    public void desktopDirTest() {
        desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        desktopPath = desktopDir.getAbsolutePath();
    }

	@Test
    public void ExcelUtils() throws FileNotFoundException {
        InputStream inputStream = new FileInputStream(desktopPath + "/year.xlsx");
        List<List<String>> lists = YcsExcelUtils.parseToContent(inputStream);
        System.out.println(JSON.toJSONString(lists));
        List<ChannelInit> channelInits = YcsExcelUtils.parseToObject(inputStream, ChannelInit.class);
        System.out.println(JSON.toJSONString(channelInits));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜长思

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值