Java解析Excel之xlsmapper
Java解析Excel之xlsmapper
溪水涛 发表于2年前
Java解析Excel之xlsmapper
  • 发表于 2年前
  • 阅读 8
  • 收藏 0
  • 点赞 1
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

摘要: xlsmapper基于xmlbean,poi的封装,使用注解风格将Excel内容提取到java bean

项目地址:https://github.com/mygreen/xlsmapper


具体使用参照 test/data/*.xlxs示例



XlsMapper

XlsMapper is Java Library for mapping Excel sheets to POJO.

  • Licensee

Apache License verion 2.0

Setup

<dependency>
    <groupId>com.github.mygreen</groupId>
    <artifactId>xlsmapper</artifactId>
    <version>1.4</version>
</dependency>

Documentation

http://mygreen.github.io/xlsmapper/sphinx/howtouse.html

Getting Started

For example, here is one Excel sheet.

Sample Excel

Map this Excel sheet to POJO.

  • Map the sheet with annotation @XlsSheet .

  • Map the cell 'Date' with annotation @XlsLabelledCell .

  • Map the table list 'User List' with annotation @XlsHorizontalRecords .

// POJO for mapping sheet.@XlsSheet(name="List")public class UserSheet {    @XlsLabelledCell(label="Date", type=LabelledCellType.Right)    Date createDate;    @XlsHorizontalRecords(tableLabel="User List")    List<UserRecord> users;

}

And the following is the record class.

  • Properties of the record class is mapped to columns by @XlsColumn .

  • Can map to int and enum type.

// Record classpublic class UserRecord {    @XlsColumn(columnName="ID")    int no;    @XlsColumn(columnName="Class", merged=true)    String className;    @XlsColumn(columnName="Name")    String name;    @XlsColumn(columnName="Gender")    Gender gender;

}// enum for the gender.public enum Gender {
    male, female;
}

You can get the mapped POJO using XlsMapper#load() like following:

// Load sheet with mapping to POJO.XlsMapper xlsMapper = new XlsMapper();UserSheet sheet = xlsMapper.load(    new FileInputStream("example.xls"), // excel sheet.
    UserSheet.class                     // POJO class.
    );

How to saving the sheet.

For example with saving the sheet, using same sheet.

Here is the template Excel sheet.

Sample Excel

And the following is the record class.

  • Append the annotation @XlsDateConverter for setting Excel format pattern.

  • Append the attribute overRecord with @XlsHorizontalRecords.

@XlsSheet(name="List")public class UserSheet {    @XlsLabelledCell(label="Date", type=LabelledCellType.Right)    @XlsDateConverter(excelPattern="yyyy/m/d")    Date createDate;    @XlsHorizontalRecords(tableLabel="User List", overRecord=OverRecordOperate.Insert)    List<UserRecord> users;

}

You can save the Excel with POJO using XlsMapper#save() like following:

// Create sheet data.UserSheet sheet = new UserSheet();
sheet.date = new Date();List<UserRecord> users = new ArrayList<>();// Create record data.UserRecord record1 = new UserRecord();
record1.no = 1;
record1.className = "A";
record1.name = "Ichiro";
record1.gender = Gender.male;
users.add(record1);UserRecord record2 = new UserRecord();// ...users.add(record2);

sheet.users = users;// Save the Excel sheet.XlsMapper xlsMapper = new XlsMapper();
xlsMapper.save(    new FileInputStream("template.xls"), // for template excel file.
    new FileOutputStream("out.xls"),     // for output excel file.
    sheet                                // for created sheet data.
    );


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 3
码字总数 1929
×
溪水涛
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: