[XML] 纯文本查看 复制代码
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beat1</version>
</dependency>
<!--非必要包,可以简略掉get,set方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
[Java] 纯文本查看 复制代码
/**
*读取指定文件
*/
@Test
public void read(){
/*文件路径*/
String filePath = "D:\\user.xlsx";
try(InputStream fileStream=new FileInputStream(filePath) ) {
/*Sheet:表格对象,第一个参数代表表格的索引,即第几个表格,第二个参数是从第几行开始,默认第一行是0*/
Sheet sheet=new Sheet(1,0);
List<Object> read = EasyExcelFactory.read(fileStream, sheet);
for (Object object :
read) {
System.out.println(object);
}
} catch (Exception e) {
e.printStackTrace();
}
}
输出如下:[Java] 纯文本查看 复制代码
@Test
public void write() throws FileNotFoundException {
// 生成EXCEL并指定输出路径
OutputStream out = new FileOutputStream("D:\\out.xlsx");
ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
// 设置,第一个参数是第几个表格,第二个参数是从第几行开始
Sheet sheet = new Sheet(1, 0);
//设置表格名
sheet.setSheetName("sheet1");
// 设置标题
Table table = new Table(1);
List<List<String>> titles = new ArrayList<List<String>>();
titles.add(Arrays.asList("用户名"));
titles.add(Arrays.asList("年龄"));
titles.add(Arrays.asList("生日"));
titles.add(Arrays.asList("地址"));
table.setHead(titles);
// 查询数据导出即可 比如说一次性总共查询出100条数据
List<List<String>> userList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
userList.add(Arrays.asList( "小明" + i, String.valueOf(i), new Date().toString(),"幸福路"+i+"号"));
}
writer.write0(userList, sheet, table);
writer.finish();
}