EasyExcel示例(阿里巴巴)基于Maven

首先感谢阿里巴巴提供了easyexcel工具类,github地址:https://github.com/alibaba/easyexcel

注意!!这里只是一个简单的示例,VC大法即可使用,对于复杂的execl导出可能会出现问题。

另Execl文件后缀为xlsx。

  1、环境搭建

    jar包依赖

 
 <dependency>
     <groupId>com.alibabagroupId>
     <artifactId>easyexcelartifactId>
     <version>1.1.2-beat1version>
 dependency>

    2、代码

public class MyExcel {
    //
    @Test
    public void simpleRead() {
        FileInputStream fileInput;
        try {
            fileInput = new FileInputStream("F://javaio文件目录//hss.xlsx");
            List read = EasyExcelFactory.read(fileInput, new Sheet(0,0));
            System.out.println(read);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }
    //
    @Test
    public void simpleWrite() {
        FileOutputStream fileOut;
        try {
            File file = new File("F://javaio文件目录//hss123.xlsx");
            if (file.exists()) {
                file.delete();
            }
            fileOut=new FileOutputStream("F://javaio文件目录//hss123.xlsx");
            ExcelWriter writer = EasyExcelFactory.getWriter(fileOut);
            Sheet sheet = new Sheet(1,0);
            Sheet sheet2 = new Sheet(1,0);
            sheet.setSheetName("HelloWord");
            List> data2 = new ArrayList<>();
            List list2 = new ArrayList<>();
            List list3 = new ArrayList<>();
            List list4 = new ArrayList<>();
            List list5 = new ArrayList<>();
            List> data = new ArrayList<>();
            List list1 = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                list1.add("123");
                list1.add("123");
                list1.add("123");
                list1.add("123");
                list1.add("123");
                data.add(list1);
            }
                list2.add("你好1");
                list3.add("你好2");
                list4.add("你好3");
                list5.add("你好4");
                data2.add(list2);
                data2.add(list3);
                data2.add(list4);
                data2.add(list5);
            sheet2.setHead(data2);
            writer.write0(null, sheet2);
            writer.write0(data, sheet);
            writer.finish();
            fileOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 
  
 

 

你可能感兴趣的:(EasyExcel示例(阿里巴巴)基于Maven)