easyexcel 读取指定行数据_easyExcel: 能让你简单方便快速的读写excel,支持列表数据导入,列表数据分页导出,以及模版导出...

1.读取列表数据:

例子请看代码测试,路径: EASYEXCEL\test\com\easyexcel\readlisttest,注意文件路径

代码片段:

类:

@Excel(beginRow=2,inFilePath="d:\\students.xlsx")

public class Students {

@Cell(columnNum="2")

private String name;

@Cell(columnNum="c")//or@Cell(whichCell="C")

private int age;

}

使用:

ReadExcel re = new ReadListExcel<>();

List list = re.read(Students.class);

System.out.println(list);

2.写入列表数据:

a.一个是通用的实现,例子请看代码测试,码路径:EASYEXCEL\test\com\easyexcel\writelistTest 方法为:testCommon

代码片段:

类:

@Excel(beginRow=1,dataHeader="姓名:1,年龄:3,成绩:4",outFilePath="d:\\test.xls")

@Sheet(sheetSize=2,sheetName="测试")

public class Students {

@Cell(columnNum="1")

private String name;

@Cell(columnNum="c")

private int age;

@Cell(columnNum="4")

private double grade;

}

使用:

WriteExcel we = new WriteListExcel();

Map param = new HashMap();

List list = new ArrayList();

list.add(new Students("张三11111111111111111111111111111111111",12,85));

list.add(new Students("李四", 14, 90));

list.add(new Students("王五", 14, 90));

we.write(param, list);

b.另一个是根据自己需求再次调整行的样式,比如合并,例子请看代码测试,路径:EASYEXCEL\test\com\easyexcel\writelistTest 方法为:testCustom

代码片段:

类:

@Excel(beginRow=1,dataHeader="姓名:1,年龄:3,成绩:4",outFilePath="d:\\test.xls")

@Sheet(sheetSize=2,sheetName="测试")

public class Students {

@Cell(columnNum="1")

private String name;

@Cell(columnNum="c")

private int age;

@Cell(columnNum="4")

private double grade;

}

使用:

WriteListExcelHelp weh = new WriteListExcelHelp();

Workbook workbook = new HSSFWorkbook();

Sheet sheet = workbook.createSheet("test");

int beginNum = 1;

MyCellStyle cellStyle = new CommonCellStyle(workbook);

Map param = new HashMap();

List list = new ArrayList();

list.add(new Students("张三",12,85));

list.add(new Students("李四", 14, 90));

weh.generateHeader(sheet, beginNum, cellStyle, Students.class);

for(Students s : list){

Row row = weh.generateBody(sheet, beginNum++, cellStyle, s);

CellRangeAddress cra = new CellRangeAddress(beginNum-1, beginNum-1, 1, 2);

sheet.addMergedRegion(cra);

row.createCell(2).setCellStyle(cellStyle.createCommonTextCellStyle());

}

ExcelUtil.workbookToFile(workbook, "d:\\test1.xls");

注意事项目:

在写放时需要传下面几个参数:

myCellStyle:样式类,不传则为默认实现,样式定义一个样式接口MyCellStyle,如需改动请自己实现。

workbook:workbook实现类,生成excel的文件类型,默认为:hssfworkbook

outFilePath:生成文件路径,可以用注解,如果传,则以传入为主

inFilePath:读取路径,可以用注解,如果传,则以传入为主

beginRow:开始行,可以用注解,如果传,以传入为主

3.写入模版数据,即定义好模版,读取模版并在模版相应位置写入数据,二种实现方案:

a.用标签来定位cell,即在excel模版相应cell上写入标签,例子请看代码测试,路径:EASYEXCEL\test\com\easyexcel\writemodeloflaabletest,注意文件路径

代码片段:

类:

public class Students {

private String name;

private int age;

}

使用:

WriteExcel we = new WriteModelExcel<>();

Map param = new HashMap<>();

param.put("inFilePath", "d:\\model.xlsx");//读取文件的目录必须有,可以传也可以用注解配,如果传以传为主

param.put("outFilePath", "d:\\outmodel.xlsx");//生成路径必须有,可以传也可以用注解配,如果传以传为主

List list = new ArrayList();

list.add(new Students("张三",25));

we.write(param, list);

b.用注解来定位cell,即在类字段上用注解表时位置,如果有注解,以注解实现,例子请看代码测试,路径:EASYEXCEL\test\com\easyexcel\writemodelofannotationtest,注意文件路径

代码片段:

类:

@Excel(inFilePath="d:\\modelann.xlsx",outFilePath="d:\\outmodelann.xlsx")

@Sheet(sheetNum=1)

public class Students {

@Cell(rowNum=1,columnNum="b")

private String name;

@Cell(rowNum=3,columnNum="b")

private int age;

@Ingroe

private double grade;

}

使用:

WriteExcel we = new WriteModelExcel<>();

Map param = new HashMap<>();

List list = new ArrayList();

list.add(new Students("张三",25));

we.write(param, list);

你可能感兴趣的:(easyexcel,读取指定行数据)