poi和easyexcel
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>2.1.1</version>
</dependency>
poi
@Test
public void excel03Test() throws IOException {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("excel学习");
Row row1 = sheet.createRow(0);
Cell cell1 = row1.createCell(0);
cell1.setCellValue("第一天");
Cell cell11 = row1.createCell(1);
cell11.setCellValue(666);
Row row2 = sheet.createRow(1);
Cell cell2 = row2.createCell(0);
cell2.setCellValue("时间");
Cell cell22 = row2.createCell(1);
cell22.setCellValue(new Date());
FileOutputStream fileOutputStream = new FileOutputStream(PATH+"03.xls");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
@Test
public void excel07Test() throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("excel学习");
Row row1 = sheet.createRow(0);
Cell cell1 = row1.createCell(0);
cell1.setCellValue("第一天");
Cell cell11 = row1.createCell(1);
cell11.setCellValue(666);
Row row2 = sheet.createRow(1);
Cell cell2 = row2.createCell(0);
cell2.setCellValue("时间");
Cell cell22 = row2.createCell(1);
cell22.setCellValue(new Date());
FileOutputStream fileOutputStream = new FileOutputStream(PATH+"04.xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
}
easyexcel
public class DemoData {
@ExcelProperty(value = "学生编号",index = 0)
private Integer sno;
@ExcelProperty(value = "学生姓名",index = 1)
private String sname;
public Integer getSno() {
return sno;
}
public void setSno(Integer sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public String toString() {
return "DemoData{" +
"sno=" + sno +
", sname='" + sname + '\'' +
'}';
}
}
public class ExcelListener extends AnalysisEventListener<DemoData> {
@Override
public void invoke(DemoData demoData, AnalysisContext analysisContext) {
System.out.println("****"+demoData);
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头"+headMap);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
public class TestEasyExcel {
public static void main(String[] args) {
getRead();
}
public static void getWrite(){
String filename = "E:\\weite.xlsx";
List<DemoData> date = getDate();
EasyExcel.write(filename,DemoData.class).sheet("学生列表").doWrite(date);
}
public static void getRead(){
String filename = "E:\\weite.xlsx";
EasyExcel.read(filename,DemoData.class,new ExcelListener()).sheet().doRead();
}
private static List<DemoData> getDate(){
List<DemoData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
DemoData demoData = new DemoData();
demoData.setSno(i);
demoData.setSname("lury"+i);
list.add(demoData);
}
return list;
}
}
|